简体   繁体   English

仅当屏幕尺寸为一定宽度时才希望该功能起作用

[英]Only want the function to work when the screen size is a certain width

I have this landing page that has a left navigation when it the screen with 1024 or larger but when the screen goes below 1024 the left navigation appears on the top of the main content of the page and the "Walmart Leverage becomes a button with on click event for the rest of the navigation to come down. the code works until I put the if statement to detect what size the screen is. Probably missing something to the code. Below is the link for the page. 当屏幕为1024或更大的屏幕时,我的登录页面具有左侧导航,但是当屏幕低于1024时,左侧导航出现在页面主要内容的顶部,并且“沃尔玛杠杆成为单击后的按钮”事件,直到其余的导航停止下来,代码才起作用,直到我放置if语句以检测屏幕的大小为止。代码可能丢失了某些内容。下面是页面的链接。

http://dl.dropboxusercontent.com/u/2399619/walmartmilitary/talentbrew-LeverageHome.html http://dl.dropboxusercontent.com/u/2399619/walmartmilitary/talentbrew-LeverageHome.html

This is the code for the jQuery 这是jQuery的代码

$(window).resize(function() {
if( $(this).width() < 1024) {       

var $showSubBtn = $("#sn-walmartleverage");

    $showSubBtn.click(function(){

            if($(this).hasClass("active")) {            
                $(this).parent().next().hide();
                $(this).removeClass("active");

            } else {            
                $(this).addClass("active");
                $(this).parent().next().show();         
            }           
            return false;           
        });         
    }   
});

Any help would be much appreciated. 任何帮助将非常感激。

Thanks 谢谢

You can't put the click event handler inside the window resize event handler, you should just check the window size when the click happens 您不能将click事件处理程序放在窗口调整大小事件处理程序内,您应该只在单击发生时检查窗口大小

$("#sn-walmartleverage").on('click', function(e){
    e.preventDefault();
    if ($(window).width() > 1024) {
        $(this).toggleClass('active').parent().next().toggle();
    }
});

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

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