/* 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;
}
}
// 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;
}
}
if(getDeviceState() == 'tablet') {
// 平板电脑下执行的 JavaScript 代码
}
$(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();
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有