Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop div scrolling once it reaches another div

Basically I currently have a div which stays fixed and follows the user down as they scroll until it reaches a certain point. I can easily make it stop at a fixed pixel position as I have done in the below example, but because I'm a jQuery idiot I have no idea how to make it stop at a div instead.

Here's what I've 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's the example: jsFiddle

The reason I want it to stop once it reaches a second div is because with the fluid layout I'm using, the second div will be sitting at different points depending on the browser size. Defining a specific point for it to stop at won't work. Anyone got any ideas as to how I can get this to do what I want? Alternatively, is it possible for the fixed div to stop scrolling once it reaches a percentage of the way down? I've looked around but haven't found anything.

Thanks for any help.

like image 497
mattmouth Avatar asked Jun 12 '12 02:06

mattmouth


People also ask

How do I stop a div from scrolling?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How can I make a div fixed after scrolling a certain amount?

Method 1: Using the sticky value of the position property The 'sticky' value of the position property sets an element to use a 'relative' position unless it crosses a specific portion of the page, after which the 'fixed' position is used.

How do you stop an Onscroll event?

The window. onscroll event fires when the window has been scrolled. Overriding this function and setting it to a fixed position every time the scroll happens will effectively disable the scroll effect.


2 Answers

Is this what you were looking for?

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');

EDIT: about your request to not scroll until a certain point, just replace this:

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

with this:

if ($window.scrollTop() <= (bumperPos - thisHeight)) {
like image 91
MicronXD Avatar answered Sep 28 '22 01:09

MicronXD


Inspired by MicronXD's fiddle, but written to be more flexible when the DOM is getting pushed around a lot on document load (or other reasons), here's another similar solution I developed for my own usage:

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(); });
  }
});

Then you can run the function as such:

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

60 is the optional margin you want the floating element to have from the top of the screen when in position: fixed, expressed in pixels.

like image 30
Adal Avatar answered Sep 28 '22 03:09

Adal