简体   繁体   English

windows.resize函数问题

[英]windows.resize function issue

Here is my code 这是我的代码

$(document).ready(function(){
    $(window).resize(function() {
        if ($(window).width() > 980) {
            $('.info-container a').toggle(function() {
                $(this)
                    .closest('li')
                    .find('.work-info')
                    .fadeIn();
                return false;
            }, function() {
                $(this)
                    .closest('li')
                    .find('.work-info')
                    .fadeOut();
                return false;
            });
        }
        else {
            $('.info-container a').unbind('click'); 
        }
    });
});

I want show the hidden div on click, when browser width > 980px. 当浏览器宽度> 980px时,我想在单击时显示隐藏的div。

when I open page - code does not work as long as the width of the window will not change. 当我打开页面时-只要窗口的宽度不变,代码就无法工作。 After that, it works fine. 在那之后,它工作正常。

Here is my code in JSFIDDLE but it doesn't work there... 这是我在JSFIDDLE中的代码,但是在这里不起作用...

It is because your functions are in $(window).resize(); 这是因为您的函数位于$(window).resize();中。 function. 功能。 If you want for it to work, you need to launch at click event and resize event. 如果要使其正常工作,则需要在单击事件中启动并调整大小事件。

You can achieve this by wrapping your functions separately, and launching them at document.click and at window.resize. 您可以通过分别包装函数,然后在document.click和window.resize中启动它们来实现此目的。

$(document).ready(function(){
    var hide = function(){
        if ($(window).width() > 980) {
            $('.info-container a').toggle(function() {
                $(this)
                    .closest('li')
                    .find('.work-info')
                    .fadeIn();
                return false;
            }, function() {
                $(this)
                    .closest('li')
                    .find('.work-info')
                    .fadeOut();
                return false;
            });
        }
        else {
            $('.info-container a').unbind('click'); 
        }
    };
    $(document).click(hide);
    $(window).resize(hide);
)};

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

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