gpt4 book ai didi

javascript - 无法让scrollTop()在Chrome和Firefox中同时工作

转载 作者:行者123 更新时间:2023-12-03 21:40:06 25 4
gpt4 key购买 nike

我无法让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;
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>

最佳答案

改用文档对象

$(document).scrollTop();

关于javascript - 无法让scrollTop()在Chrome和Firefox中同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18778020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com