Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing an elements css position after scrolling down the page

I am messing around with some jquery trying to get to grips with it.

I have a ul nav which has a absolute position set but after I scroll the page down by 200 pixels i would like that to switch to position fixed so that the nav always stays on the page.

How would I do this?

below is the example I am working on

http://satbulsara.com/tests/

like image 901
sat Avatar asked Feb 05 '11 15:02

sat


People also ask

How do you make a div fixed position while scrolling?

This is done by adding the position of the element relative to the viewport with the current scroll position. The getBoundingClientRect() method returns a DOMReact object of which the 'top' value could be used to get the position relative to the viewport.

How do I move a scrolling element?

To Move Elements On Page Scroll It Takes Only Three Steps:- scrollTop() > 100) { $("#left_right_div"). animate({marginLeft:'100px'},900); $("#right_left_img"). animate({marginRight:'100px'},900); } if ($(window). scrollTop() > 700) { $("#fade_div").


2 Answers

Thanks to everyone for being so quick to help

this is what i wanted

$(document).ready(function() {
    var theLoc = $('ul').position().top;
    $(window).scroll(function() {
        if(theLoc >= $(window).scrollTop()) {
            if($('ul').hasClass('fixed')) {
                $('ul').removeClass('fixed');
            }
        } else { 
            if(!$('ul').hasClass('fixed')) {
                $('ul').addClass('fixed');
            }
        }
    });
});

Thanks!!!!!

like image 111
sat Avatar answered Oct 15 '22 07:10

sat


I wrote this jQuery plugin to do just that.

like image 45
Chris Heald Avatar answered Oct 15 '22 08:10

Chris Heald