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

WBOY
Release: 2016-05-16 08:59:51
Original
2926 people have browsed it

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template