Jonny Cerveza Jonny Cerveza - 1 year ago 65
Javascript Question

Can't get scrollTop() to work in both Chrome & Firefox

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
works in Chrome. Any thoughts would be greatly appreciated. Below is my code.

<!DOCTYPE html>
<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;
overflow: auto;

<div id="light">

<!-- Used the google jQuery link for ease of use in this example -->
<script type="text/javascript" src=""></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});

Answer Source

Use the document object instead