简体   繁体   English

如果(window.innerWidth <x)无法调整大小,则javascript仅运行功能

[英]javascript only run function if (window.innerWidth < x) not working on resize

I want a function to run only if (window.innerWidth < 800). 我希望仅在(window.innerWidth <800)时运行函数。
This should also work if the window gets resized. 如果调整窗口大小,这也应该起作用。 So, depending on the browser size it needs to toggle between running the function and doing nothing . 因此,根据浏览器的大小,需要在运行函数和不执行任何操作之间切换。

The Problem I experienced, is that the function is still running if you resize the window to more than 800. The function only does nothing if you refresh. 我遇到的问题是,如果将窗口的大小调整为800以上,该函数仍在运行。如果刷新,该函数将不执行任何操作。

How can I constantly check the Browser size and depend the function on it? 如何不断检查浏览器大小并依赖其功能?

The code below is just a very simple demo, the function will be more complex. 下面的代码只是一个非常简单的演示,功能将更加复杂。 So it won't be only about changing the color. 因此,不仅仅是改变颜色。

window.addEventListener("load", myFunction);
window.addEventListener("resize", myFunction);

function myFunction() {
  if ((window.innerWidth <= 800)) {
    document.body.style.backgroundColor = "pink";
  }
}

The Problem I experienced, is that the function is still running if you resize the window to more than 800. The function only does nothing if you refresh. 我遇到的问题是,如果将窗口的大小调整为800以上,该函数仍在运行。如果刷新,该函数将不执行任何操作。

The code you've shown only does something (setting a color in that example) if the window is <= 800px wide. 如果窗口的宽度小于或等于800像素,那么您显示的代码只会执行某些操作(在该示例中设置颜色)。 But it doesn't un do that something (resetting the color back to its original value) if the window is > 800px wide. 但是,如果窗口的宽度大于800像素,它并不会取消该操作(将颜色重置为原始值)。 It simply does nothing, and the earlier effect remains in place. 它只是不执行任何操作,并且更早的效果仍然存在。

If, like your color example, the thing you're doing endures , you need an else : 如果像您的颜色示例一样,您所做的事情可以忍受 ,则需要else

if (window.innerWidth <= 800) {
    // Set the <= 800 state
} else {
    // Set the > 800 state
}

If what you're doing is related to styling the body element, don't do it directly as in your example. 如果您正在做的事与body元素的样式有关,请不要像在示例中那样直接进行。 Instead, use a class . 而是使用class

If you don't need to support IE, then: 如果您不需要支持IE,则:

document.body.classList.toggle("narrow", window.innerWidth <= 800);

That will set narrow for <= 800 and clear it for > 800. 这将对<= 800设置narrow对于> = 800则清除。

If you need to support IE9+, then sadly you can't use the second argument to toggle , so: 如果您需要支持IE9 +,那么很遗憾,您不能使用第二个参数来toggle ,因此:

if (window.innerWidth <= 800) {
    document.body.classList.add("narrow")
} else {
    document.body.classList.remove("narrow");
}

If you need to support IE8, you'll need a classList polyfill (or act directly on className ). 如果需要支持IE8,则需要一个classList (或直接对className )。

There are two possible transitions here: 这里有两种可能的转换:

  1. From width > 800 to width < 800 从宽度> 800到宽度<800
  2. From width < 800 to width > 800 从宽度<800到宽度> 800

Your resize event handler is only checking for the first case. resize事件处理程序仅检查第一种情况。 This will handle both cases: 这将处理两种情况:

window.addEventListener("load", myFunction);
window.addEventListener("resize", myFunction);

function myFunction() {
  if (window.innerWidth <= 800) {
    document.body.style.backgroundColor = "pink";
  } else {
    document.body.style.backgroundColor = "yellow";
  }
}

Sidenote: this is a very inefficient way of doing this, because you're resetting the backgroundColor even if it's already set to "pink". 旁注:这是一种非常低效的方法,因为即使将backgroundColor设置为“粉红色”,也要重置它。 This is a more efficient solution: 这是一个更有效的解决方案:

window.addEventListener("load", myFunction);
window.addEventListener("resize", myFunction);

var bodyColor = "white";
function myFunction() {
  var currentColor = window.innerWidth <= 800 ? "pink" : "white";
  if (currentColor !== bodyColor) {
    document.body.style.backgroundColor = currentColor;
    bodyColor = currentColor;
  }
}

Codepen: https://codepen.io/pen?editors=1111 Codepen: https ://codepen.io/pen editors = 1111

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

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