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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 08:59:512962browse

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;
  }
}

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;
  }
}

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 代码
}

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();
});

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:
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