Stop scrolling a div when it reaches another div

Basically, I currently have a div that remains fixed and follows the user when they scroll until they reach a certain point. I can easily stop it at a fixed pixel position, as I did in the example below, but since I'm a jQuery idiot, I have no idea how to make it stop on a div instead.

Here is what I have used so far:

var windw = this;

$.fn.followTo = function ( pos ) {
     var $this = this,
        $window = $(windw);

$window.scroll(function(e){
    if ($window.scrollTop() > pos) {
        $this.css({
            position: 'absolute',
            top: pos
        });
    } else {
        $this.css({
            position: 'fixed',
            top: 0
        });
    }
});
};

$('#one').followTo(400);

Here is an example: jsFiddle

, , , div, , , , div . . - , , ? , div , ? , .

.

+5
2

, ?

http://jsfiddle.net/Tgm6Y/1447/

var windw = this;

$.fn.followTo = function ( elem ) {
    var $this = this,
        $window = $(windw),
        $bumper = $(elem),
        bumperPos = $bumper.offset().top,
        thisHeight = $this.outerHeight(),
        setPosition = function(){
            if ($window.scrollTop() > (bumperPos - thisHeight)) {
                $this.css({
                    position: 'absolute',
                    top: (bumperPos - thisHeight)
                });
            } else {
                $this.css({
                    position: 'fixed',
                    top: 0
                });
            }
        };
    $window.resize(function()
    {
        bumperPos = pos.offset().top;
        thisHeight = $this.outerHeight();
        setPosition();
    });
    $window.scroll(setPosition);
    setPosition();
};

$('#one').followTo('#two');

: , :

if ($window.scrollTop() > (bumperPos - thisHeight)) {

:

if ($window.scrollTop() <= (bumperPos - thisHeight)) {
+8

MicronXD, , , DOM ( ), , :

jQuery.fn.extend({
  followTo: function (elem, marginTop) {
    var $this = $(this);
    var $initialOffset = $this.offset().top;
    setPosition = function() {
      if ( $(window).scrollTop() > $initialOffset ) {
        if ( elem.offset().top > ( $(window).scrollTop() + $this.outerHeight() + marginTop ) ) {
          $this.css({ position: 'fixed', top: marginTop });
        }
        if ( elem.offset().top <= ( $(window).scrollTop() + $this.outerHeight() + marginTop ) ) {
          $this.css({ position: 'absolute', top: elem.offset().top - $this.outerHeight() });
        }
      }
      if ( $(window).scrollTop() <= $initialOffset ) {
        $this.css({ position: 'relative', top: 0 });
      }
    }
    $(window).resize( function(){ setPosition(); });
    $(window).scroll( function(){ setPosition(); });
  }
});

:

$('#div-to-move').followTo( $('#div-to-stop-at'), 60);

60 - , , , : , .

+2

All Articles