Home Web Front-end JS Tutorial JavaScript method to determine browsing device based on CSS Media Queries_javascript skills

JavaScript method to determine browsing device based on CSS Media Queries_javascript skills

May 16, 2016 am 08:59 AM
css javascript media

css section

first, create a new class for making judgments, and then use media queries to assign different values ​​to the z-index attribute of this class. this class is only used for javascript reading, so it needs to be moved out of the screen window and invisible to the viewer to avoid unexpected situations.

as a demonstration, the following code sets four device states: regular desktop version, small screen desktop version, tablet version, and mobile version.

/* default state */
.state-indicator {
  position: absolute;
  top: -999em;
  left: -999em;

  z-index: 1;
}

/* small desktop */
@media all and (max-width: 1200px) {
  .state-indicator {
    z-index: 2;
  }
}

/* tablet */
@media all and (max-width: 1024px) {
  .state-indicator {
    z-index: 3;
  }
}

/* mobile phone */
@media all and (max-width: 768px) {
  .state-indicator {
    z-index: 4;
  }
}

Copy after login

javascript judgment

css is already in place, so you need to use javascript to generate a temporary dom object, then set the corresponding class for it, and then read the z-index value of this object. the original writing method is as follows:

// create the state-indicator element
var indicator = document.createelement('div');
indicator.classname = 'state-indicator';
document.body.appendchild(indicator);

// create a method which returns device state
function getdevicestate() {
  return parseint(window.getcomputedstyle(indicator).getpropertyvalue('z-index'), 10);
}
getdevicestate() 函数返回的就是 z-index 的值,为了增强一下可读性,可以用 switch 函数来规范输出一下:

function getdevicestate() {
  switch(parseint(window.getcomputedstyle(indicator).getpropertyvalue('z-index'), 10)) {
    case 2:
      return 'small-desktop';
      break;
    case 3:
      return 'tablet';
      break;
    case 4:
      return 'phone';
      break;
    default:
      return 'desktop';
      break;
  }
}

Copy after login

in this way, you can use the following code to determine the device status, and then execute the corresponding javascript code:

if(getdevicestate() == 'tablet') {
  // 平板电脑下执行的 javascript 代码
}
Copy after login

if you are using jquery here, just use the following code:

$(function(){
  $('body').append('<div class="state-indicator"></div>');

  function getDeviceState() {
    switch(parseInt($('.state-indicator').css('z-index'),10)) {
      case 2:
        return 'small-desktop';
        break;
      case 3:
        return 'tablet';
        break;
      case 4:
        return 'phone';
        break;
      default:
        return 'desktop';
        break;
    }
  }

  console.log(getDeviceState());
  $('.state-indicator').remove();
});

Copy after login

first create, then obtain, and finally delete the node. the specific device will be output in your console, click here to view demo . you can try dragging the middle border and click run.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

How to wrap prompt in js How to wrap prompt in js May 01, 2024 am 06:24 AM

How to wrap prompt in js

See all articles