views:

221

answers:

2

I'm got a site that has a long list of tweets, and as you scroll down the right column follows you down, showing stats on the tweets. (See it in action at http://www.grapevinegame.com . Click 'memorise', then 'skip' to get to the list page. Works in Safari and Chrome).

I'm using jQuery to update the top-margin of the right column, increasing it as I scroll down. It seems to be working fine in webkit-based browsers, but doesn't budge in Firefox. Heres the code, the right column element is a div with id = "distance".

// Listen for scroll function
            $(window).scroll(function () {

                    // Calculating the position of the scrollbar
                    var doc = $("body"), 
                    scrollPosition = $("body").scrollTop(),
                    pageSize = $("body").height(),
                    windowSize = $(window).height(),
                    fullScroll = (pageSize) - windowSize;
                    percentageScrolled = (scrollPosition / fullScroll);

                    var entries = $("#whispers-list > li").length;

                    // Set position of distance counter
                    $('div#distance').css('margin-top', ($("#whispers-list").height()+$("#latest-whisper").height()+33)*percentageScrolled);

                    // Update distance counter
                        $('#distance-travelled').text(Math.round(distanceTravelled*(1-percentageScrolled)));            
                        $('#whispers-list li').each(function(index) {

                                //highlight adjacent whispers                               
                                if ($('#whispers-list li:nth-child('+(index+1)+')').offset().top >= $('#distance').offset().top && $('#whispers-list li:nth-child('+(index+1)+')').offset().top <= $('#distance').offset().top + $('#distance').height()) {
                                    // alert("yup");
                                    $('#whispers-list li:nth-child('+(index+1)+') ul').fadeTo(1, 1);
                                } else {
                                    $('#whispers-list li:nth-child('+(index+1)+') ul').fadeTo(1, 0.5);  
                                }
                              });               

                    });

Appreciate any help or advice!

A: 

I am trying to trace back the distanceTravelled to definition and cant see where its defined. can you paste all the code

XGreen
The code is all in the site's source (it's all on one page). Do you want me to paste the whole thing?
Chris Armstrong
+1  A: 

Updated!

It seems that $("body").scrollTop() is always returning 0 which I assume is the problem.

But when I tried $("html").scrollTop() it seems to be returning the correct scrollTop.

Try changing the scrollPosition line to this:

scrollPosition = $("html").scrollTop(), 
Erikk Ross
thanks, tried both there now but didn't seem to make any difference
Chris Armstrong
the offset value is only really being used to highlight the adjacent li's anyway, and only seems to work with the ()
Chris Armstrong
Gotcha, ok after I looked at it some more I think the issue is with using $("body").scrollTop()...try changing to $("html").scrollTop(), that seems to return the correct value for me in FF.
Erikk Ross
Hmm, that's gotten it moving in FF (although its jumping up and down a lot), but now its not working in Chrome or Safari
Chris Armstrong
You might need to add some code to check the browser type and use $("html") for FF and $("body") for chrome and safari.
Erikk Ross