简体   繁体   English

如何禁用具有内部窗口宽度的 JavaScript

[英]How to disable a JavaScript, with inner window width

The page I am working on has a tilt effect, which affects the orientation of the site body.我正在处理的页面具有倾斜效果,这会影响站点正文的方向。 I want to turn this off when the page reaches a mobile viewport;当页面到达移动视口时,我想关闭它; I have looked and tried a few thing but can't seem to get the effect I want.我已经查看并尝试了一些东西,但似乎无法获得我想要的效果。 The code below is what i am using.下面的代码是我正在使用的。 This script runs separate from my main JS code.该脚本与我的主要 JS 代码分开运行。

window.addEventListener("mousemove",function(e) {

  var width = window.innerWidth;
  var height = window.innerHeight;
  var clientHeight = document.body.clientHeight;
  var skew = {};
  skew.y = (20 * ((e.x / width) - 0.5));
  skew.x = -(20 * ((e.y / height) - 0.5));

  document.body.style.webkitTransform = "perspective("+clientHeight+"px) rotateX("+skew.x+"deg) rotateY("+skew.y+"deg)";

});

CSS changes won't remove themselves, so you'll have to remove the transform manually if the browser window is under your desired size. CSS 更改不会自行删除,因此如果浏览器窗口低于您所需的大小,您必须手动删除转换。

Check on window resize,检查窗口调整大小,

window.addEventListener('resize', function() {
  if (window.innerWidth < 568) {
    document.body.style.webkitTransform = '';
  }
});

And also make sure not to reapply it if the user moves their mouse.如果用户移动鼠标,还要确保不要重新应用它。

// This is the function you already have
window.addEventListener('mousemove', function(e) {
  if (window.innerWidth < 568) {
    return;
  }
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM