If window is less than 768px wide, don't fire function

耗尽温柔 提交于 2019-12-08 07:28:56

问题


If the window width on page load AND resize is less than 768px, I don't want to fire the showCover() function. With the below code, even when the window is less than 768px, it's still being fired.

    function ipsThemeViewer() {

        jQuery(window).resize(function() {
           if ( jQuery(window).width() < 768 ) return false;
           showCover();
        }).resize();

    }

    function showCover() {
        jQuery('#ipsThemeViewerScreen').hover(function () {
            var t = jQuery(this);
            jQuery('.cover').stop().fadeIn('fast');
        }, function () {
            var t = jQuery(this);
            jQuery('.cover').stop().fadeOut('fast');
        });
    }

回答1:


I would go the other way around:

jQuery(function($) {  // DOM READY AND SECURE $ ALIAS

    var winIsSmall;

    function testWinSize(){
        winIsSmall= $(window).width() < 768; // BOOLEAN
    }

    $(window).on("load resize", testWinSize);

    $('#ipsThemeViewerScreen').hover(function () {            
        if(winIsSmall){
            // need something here?
        }else{
            $('.cover').stop().fadeToggle('fast');
        }
    });

});


来源:https://stackoverflow.com/questions/20941597/if-window-is-less-than-768px-wide-dont-fire-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!