无法使scrollTop()在Chrome&火狐 [英] Can't get scrollTop() to work in both Chrome & Firefox

查看:132
本文介绍了无法使scrollTop()在Chrome&火狐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使scrollTop()方法在Firefox和Chrome中都能正常工作。我使用了 $('body,html')。scrollTop(); ,但是在Chrome中不起作用。只有 $('body')。scrollTop(); 可以在Chrome中使用。任何想法将不胜感激。下面是我的代码。

 <!DOCTYPE html> 
< html>
< head>
< title> Demo< / title>
< style type =text / css>
body {
height:2000px;
}

#light {
display:block;
位置:固定;
top:50%;
剩下:50%;
margin-left:-400px;
margin-top:-200px;
width:800px;
height:400px;
background-color:blue;
z-index:1002;
溢出:auto;
}
< / style>
< / head>

< body>
< div id =light>
< / div>

<! - 在本例中使用了google jQuery链接以方便使用 - >
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js>< / script>
< script type =text / javascript>
$(document).ready(function(){
$(window).scroll(function(){
var offset = $('body,html')。scrollTop();
var view = $(window).height();
var total = $(document).height();
var percent = 1-(offset /(total - view)) ;
var widthFactor = 800 * percent;
var marginFactor = - (400 * percent)
$ b $ if(percent> 0.33){
$(#light ).css({width:widthFactor,
margin-left:marginFactor});
};
});
});
< / script>
< / body>
< / html>


解决方案

使用文档对象

  $(document).scrollTop(); 


I am having trouble getting the scrollTop() method to work in both Firefox and Chrome. I used $('body, html').scrollTop(); however, it doesn't work in Chrome. Only $('body').scrollTop(); works in Chrome. Any thoughts would be greatly appreciated. Below is my code.

<!DOCTYPE html>
<html>
<head>
  <title>Demo</title>
  <style type="text/css">
  body {
    height: 2000px;
  }

  #light {
    display: block;
    position: fixed;
    top: 50%;
    left: 50%;
    margin-left: -400px;
    margin-top: -200px;
    width: 800px;
    height: 400px;
    background-color: blue;
    z-index:1002;
    overflow: auto;
  }
</style>
</head>

<body>
  <div id="light">
  </div>

<!-- Used the google jQuery link for ease of use in this example   -->
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $(window).scroll(function () {
        var offset = $('body, html').scrollTop();
        var view = $(window).height();
        var total = $(document).height();
        var percent = 1-(offset / (total - view));
        var widthFactor = 800*percent;
        var marginFactor = -(400*percent)

        if(percent > 0.33){
          $("#light").css({ "width" : widthFactor,
                      "margin-left" : marginFactor});
        };
      });
    });
  </script>
</body>
</html>

解决方案

Use the document object instead

$(document).scrollTop();

这篇关于无法使scrollTop()在Chrome&amp;火狐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆