2013-11-28 32 views
0

我正在使用此脚本,以便在滚动时会出现某些DIV,但使用移动设备时效果不佳。例如,当使用iPad时,如果我没有从屏幕上抬起手指或直到页面停止滚动,则DIV永远不会显示出来。jQuery - 如果浏览器宽度大于960,则启动脚本px

这是我有:

$(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大,以便它不会在移动设备上运行。

谢谢。

回答

0

使用.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); 
       } 
      }); 
     } 
    }); 
}); 
0

你可以只包住窗户的检查中映射宽度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); 
       } 
      }); 
     } 
    }); 
}); 
相关问题