Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping fixed position scrolling at a certain point?

I have an element that is position:fixed and so scrolls with the page how i want it to however. when the user scrolls up I want the element to stop scrolling at a certain point, say when it is 250px from the top of the page, is this possible? Any help or advice would be helpful thanks!

I had a feeling that I would need to use jquery to do so. I tried getting the scrolling or location of the where the user is but got really confused, is there any jquery solutions?

like image 987
Louise McComiskey Avatar asked May 05 '11 19:05

Louise McComiskey


People also ask

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

The element is set to 'fixed' or 'relative' depending upon whether the user has scrolled past the element. The element to be stuck is first identified and its current position on the page is calculated. This is done by adding the position of the element relative to the viewport with the current scroll position.

How do you control the scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.


2 Answers

Do you mean sort of like this?

http://jsfiddle.net/b43hj/

$(window).scroll(function(){     $("#theFixed").css("top", Math.max(0, 250 - $(this).scrollTop())); }); 

$(window).scroll(function(){      $("#theFixed").css("top", Math.max(0, 100 - $(this).scrollTop()));  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    <div id="theFixed" style="position:fixed;top:100px;background-color:red">SOMETHING</div>    <!-- random filler to allow for scrolling -->  STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>STUFF <BR>
like image 158
James Montagne Avatar answered Oct 07 '22 15:10

James Montagne


Here's a quick jQuery plugin I just wrote that can do what you require:

$.fn.followTo = function (pos) {     var $this = this,         $window = $(window);      $window.scroll(function (e) {         if ($window.scrollTop() > pos) {             $this.css({                 position: 'absolute',                 top: pos             });         } else {             $this.css({                 position: 'fixed',                 top: 0             });         }     }); };  $('#yourDiv').followTo(250); 

See working example →

like image 43
mVChr Avatar answered Oct 07 '22 14:10

mVChr