jQuery函数在不应该运行时运行 [英] jQuery function running when it shouldn't

查看:76
本文介绍了jQuery函数在不应该运行时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数只需要在宽度小于特定值时运行。我已经用 if($(window).width()来完成这个工作,但是当宽度超过n时,函数也会运行。 / p>

if($(window).width()< n){} if 颜色变化(浅灰色为< 1000px,深灰色为> 1000px)。

由于函数也在运行,当它不应该我的页面布局中断单击选项卡,然后调整大小以查看此)。当函数不应该运行常规标签应显示,你可以看到,它们正确显示,直到你点击它们切换(这是该功能被炒鱿鱼,而不应该)。



任何人都可以帮助我?



代码



演示(滚动< and> 1000px to see)



PS:我知道这样做会更好用,尽管我只限于使用这个。

解决方案

如果(windowsize< 1000)您也需要知道当前状态。

pre $ if(is_large&&& windowsize <1000)
{
is_large = false;
//其余代码处理转换为小尺寸
}
else if(!is_large)
{
is_large = true;
//处理过渡到大尺寸的代码
}

您还需要处理在初始调用 checkWidth()之前设置 is_large 的页面加载情况。


I have a function which only needs to run when the width is less then a specific value. I have done this with the if ($(window).width() < n) { } but the function also runs when the width is more then n..

The if if ($(window).width() < n) { } works fine since the background color changes (light gray for < 1000px and dark gray for > 1000px).

Since the function is running also when it shouldn't the layout of my page breaks (click the tabs and then resize to see this). When the function should not run regular tabs should be displayed as you can see, they are displayed correctly until you click they toggle (which is the function getting fired whereas it shouldn't).

Anyone who can help me?

Code

Demo (scroll < and > 1000px to see)

PS: I am aware the would be of a better use for although I have been limited to using a for this.

解决方案

Instead of just if (windowsize < 1000) you also need to know the current status.

if (is_large && windowsize < 1000)
{
    is_large = false;
    // rest of code to handle transition to small size
}
else if (!is_large)
{
    is_large = true;
    // code to handle transition to large size
}

You also need to handle the case of setting is_large on page load before the initial call to checkWidth().

这篇关于jQuery函数在不应该运行时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆