js教程

JS判断网站宽度改变样式或触发事件教程

我的站长站 2022-10-15 人阅读

oWidth为获取的网站宽度,然后判断oWidth宽度数值是否小于某分辨率,如果成立就可以加载改变样式文件或触发事件等等,可以自由发挥了。

<script type="text/javascript">    
 var oWidth = document.documentElement.clientWidth || document.body.clientWidth;    
 if (oWidth < 1024) {}    
 else {    
   window.location.href = "https://www.wdzzz.com/";    
 }    
 window.addEventListener("orientationchange", function () {    
   var oWidth = document.documentElement.clientWidth || document.body.clientWidth;    
   if (oWidth < 1024) {}    
   else {    
     window.location.href = "https://www.wdzzz.com/";      
   }    
 }, false);    
 window.addEventListener("resize", function () {    
   var oWidth = document.documentElement.clientWidth || document.body.clientWidth;    
   if (oWidth < 1024) {}    
   else {    
     window.location.href = "https://www.wdzzz.com/";     
   }    
 }, false);    
</script>