Setting CSS value limits of the window scrolling animation

I have a <div id='map'> map that slides as the user scrolls down. However, it seems to let the map scroll forever, never letting the user actually reach the bottom of the page (there is a footer).

What I am trying to do is get the <div> to stop scrolling when it reaches the end of another dynamically-sized (height is variable) <div>. These two <div>s are side-by-side and in the same row.

Here is the JavaScript code I'm using to make the right div move with the user's scroll:

$(function() {

    var $sidebar   = $("#map"),
        $window    = $(window),
        offset     = $sidebar.offset(),
        topPadding = 15;

    $window.scroll(function() {
        if ($window.scrollTop() > {
                marginTop: $window.scrollTop() - + topPadding
        else {
                marginTop: 0



You can not use animate() method with scroll function it will create conflict because scroll value will change always and jQuery can not infinite repeat same animation. Stop won't rescue this issue either, or i cound't manage to do it.

And i suggest to use variation in if statements.

Here is not working animate sample with animate usage.

Here is same example working with css method.

$(window).scroll(function() {
    var scrollVal = $(this).scrollTop();
    if ( scrollVal > {
           'margin-top': (($window.scrollTop() - + topPadding) + 'px'
                       //added +'px' here to prevent old internet explorer bugs
    } else {

Note: if you want to expand your if statements with else if, don't use else, it will create conflict too. Bealive me i am really expreienced with this issue.


You can change your approuch to calculations of the limits too. I presume you want to fix the nav and your html is like this:

<div class="wrapper">
    <div class="header">header</div>
    <span id="subnav">hold this</span>

and jQuery calculations should be like this: Here is working jsFiddle.

$(document).ready(function() {
   $(window).scroll(function() {
       var headerH = $('.header').outerHeight(true);
      //this will calculate header's full height, with borders, margins, paddings
       var scrollVal = $(this).scrollTop();
        if ( scrollVal > headerH ) {
        //when scroll value reach to your selector
            $('#subnav').css({'position':'fixed','top' :'0px'});
        } else {


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.