Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change css header after scrolling

Tags:

jquery

css

header

I need some help with my JQuery.

I would like to change the css of the header after scrolling, but can't get it work. The only thing that must change in the css is margin to 65px and delete the logo.

Here is the code

#menuheader {
background: -webkit-gradient(linear, center top, center bottom, from(#fff),to(#ccc));
background-image: linear-gradient(#fff, #ccc);
box-shadow: 3px 3px 3px 3px #333;
height: 40px;
position: relative;
margin: 165px auto 0;
z-index: 10;}

Many thanks in advance. Jason

like image 856
Jssonline Avatar asked Aug 18 '13 17:08

Jssonline


People also ask

How do I fix scrolling header in CSS?

By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.

How do I make my header follow the scroll?

If your header row locates on the top of the worksheet, please click View > Freeze Panes > Freeze Top Rows directly. See screenshot. Now the header row is frozen. And it will follow the worksheet up and down while scrolling the worksheet.

How do you keep the header static on top when scrolling?

A fixed header (also known as a sticky header) is a smart navigation tool that causes the header of a website to remain at the top of the page as a user scrolls up and down. In other words, the header and site navigation are always on the top of the scrolled page.


2 Answers

Assuming you want to change the CSS after scrolling beyond a certain point, and then revert the CSS once you've scrolled back to a certain point, with jQuery:

$(window).scroll(function() {

    //After scrolling 100px from the top...
    if ( $(window).scrollTop() >= 100 ) {
        $('#logo').css('display', 'none');
        $('#menuheader').css('margin', '65px auto 0');

    //Otherwise remove inline styles and thereby revert to original stying
    } else {
        $('#logo, #menuheader').attr('style', '');

    }
});

Then all you need to do is swap out 100 with whatever point (how many pixels down from the top, while scrolling) you want the CSS to be swapped at.

http://jsfiddle.net/dJh8w/4/

like image 90
stuajc Avatar answered Oct 04 '22 01:10

stuajc


Plese clarify your question. Do you just need the jquery to change margin? or some sort of scroll handler? Here is code for changing margin: JSFiddle :

 $('#menuheader').attr("style", "margin:100px auto 0");
like image 22
poodle Avatar answered Oct 04 '22 02:10

poodle