Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  74] [ 2]  / answers: 1 / hits: 66999  / 11 Years ago, fri, september 13, 2013, 12:00:00

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>

More From » jquery

 Answers
34

Use the document object instead



$(document).scrollTop();

[#75720] Wednesday, September 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aleenamarinr

Total Points: 610
Total Questions: 109
Total Answers: 118

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;