jQuery-如果浏览器宽度超过960像素,则启动脚本



我使用这个脚本是为了在滚动时显示某些div,但在使用移动设备时,效果不太好。例如,在使用iPad时,如果我不把手指从屏幕上移开,或者直到页面停止滚动,DIV就永远不会出现。

这是我的:

$(document).ready(function()
{
    $(window).scroll( function(){
        $('.hidden').each( function(i){
            var bottom_of_object = $(this).position().top + $(this).outerHeight() / 4;
            var bottom_of_window = $(window).scrollTop() + $(window).height();
            if( bottom_of_window > bottom_of_object ){
                $(this).animate({'opacity':'1'},500);
            }
        }); 
    });
});

现在我只想在浏览器窗口大于960时启动这个脚本,这样它就不会在移动设备上工作。

谢谢。

使用.width()函数获取浏览器的宽度,并且只有在超过960:时才执行代码

$(document).ready(function() {
    $(window).scroll(function() {
        if($(window).width() > 960) {
            $('.hidden').each( function(i){
                var bottom_of_object = $(this).position().top + $(this).outerHeight() / 4;
                var bottom_of_window = $(window).scrollTop() + $(window).height();
                if( bottom_of_window > bottom_of_object ){
                    $(this).animate({'opacity':'1'},500);
                }
            }); 
        }
    });
});

您可以将函数包装在窗口宽度if($(window).width() >= 960) 的检查中

$(document).ready(function()
{
    if ($(window).width() >= 960) {
        $(window).scroll( function(){
            $('.hidden').each( function(i){
                var bottom_of_object = $(this).position().top + $(this).outerHeight() / 4;
                var bottom_of_window = $(window).scrollTop() + $(window).height();
                if( bottom_of_window > bottom_of_object ){
                    $(this).animate({'opacity':'1'},500);
                }
            });
        } 
    });
});

最新更新