Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery scroll to a px count from top and then set a div to be fixed from the top for the rest of the scroll

I am looking to change a div's css when i scroll to a certain point down the page, a certain amount of pixels from the top of the page.

On page load i would have a div positioned statically. Once I started to scroll down the page and i hit a point from the top (say 100px for demo purposes) i want to change that static div to become fixed like 20px from the top. Which would be done via the css() property of jquery. THis would allow it to stay at that fixed 20px all the way down the page.

What jquery property can i use to know when i hit that 100px mark. I want this to also revert once someone gets back to the top so that the div is put back to where it was when the page loaded and not 20px from the top.

Any ideas?

like image 799
estern Avatar asked Jan 04 '11 19:01

estern


People also ask

How do I fix a Div after scrolling to top?

Method 1: Using the sticky value of the position property It can be given a value of '0px' to make the element leave no space from the top of the viewport, or increased further to leave space from the top of the viewport.

How do you stick a div after scrolling?

To make an element sticky, use the following code: make sticky('#sticky-elem-id'); When an element becomes sticky, the code maintains the remaining content's position to prevent it from jumping into the gap left by the sticky element.

How do you make the element appear after a scroll?

How To Show An Element After Scrolling Halfway Down The Page (JS) If you have an element that is sticky on the page, such as a call to action button, toggle links visibility based on the scroll position of the page could be a simple way to make it pop out and get more interaction.

How get scroll top in jQuery?

jQuery scrollTop() Method The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.


2 Answers

You could use the scroll() event to run the code, and the scrollTop() method to see where you are.

Here's a demo: http://jsfiddle.net/EahRx/

(I used the values you provided, so there's a jump. I'm sure it will look better in your actual page.)

var fixed = false;

$(document).scroll(function() {
    if( $(this).scrollTop() >= 100 ) {
        if( !fixed ) {
            fixed = true;
            $('#myDiv').css({position:'fixed',top:20}); // Or set top:20px; in CSS
        }                                           // It won't matter when static
    } else {
        if( fixed ) {
            fixed = false;
            $('#myDiv').css({position:'static'});
        }
    }
});

The fixed variable prevents the .css() code from running more times than it needs to.

like image 126
user113716 Avatar answered Oct 01 '22 10:10

user113716


Try using scrollTop:

$(window).scroll(function(){
    if  ($(window).scrollTop() >= 100){
        //CSS changes go here
    }
});
like image 39
Carvin Avatar answered Oct 01 '22 11:10

Carvin