Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add bottom box shadow to the menu on scrollup and scrolldown

Tags:

html

jquery

css

I have a menu with this CSS properties:

#header {
  width: 100%;
  position: fixed;
  z-index: 9000;
  overflow: auto;
}

So based on the CSS properties above, this element (#header) will obviously remain on top regardless of the scrolling. What I'm trying to achieve is on scroll up and scroll down, a bottom box shadow should be added into that element (#header) and should be removed once it reaches the default location of that element (#header) which is obviously the top-most place of the page.

I'm open to any suggestion and recommendation.

like image 291
Juliver Galleto Avatar asked Jun 17 '13 18:06

Juliver Galleto


People also ask

How do you add a box shadow to the bottom?

Use the box-shadow Property to Set the Bottom Box Shadow in CSS. We can use the box-shadow property to set the shadow only at the bottom of the box. The box-shadow property sets the shadow of the selected element.

How do you apply box shadow only on the right side?

When creating a box-shadow on one side only, we have to focus on the last value, the spread radius. The spread radius decreases the overall size of the box-shadow both horizontally and vertically.

How do you put shadows on elements in CSS?

In CSS, shadows on the boxes of elements are created using the box-shadow property (if you want to add a shadow to the text itself, you need text-shadow ). The box-shadow property takes a number of values: The offset on the x-axis. The offset on the y-axis.


1 Answers

$(window).scroll(function() {     
    var scroll = $(window).scrollTop();
    if (scroll > 0) {
        $("#header").addClass("active");
    }
    else {
        $("#header").removeClass("active");
    }
});
body {
    height: 2000px;
    margin: 0;
}

body > #header{position:fixed;}

#header {
    width: 100%;
    position: fixed;
    z-index:9000;
    overflow: auto;
    background: #e6e6e6;
    text-align: center;
    padding: 10px 0;
    transition: all 0.5s linear;
}

#header.active {
     box-shadow: 0 0 10px rgba(0,0,0,0.4);   
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="header">HEADER</div>

JSFiddle version

Whenever the page is scrolled we save the current distance from the top of the document in a variable (scroll).

If the current position is greater than 0 we add the class active to #header.

If the current position is equal to 0 we remove the class.

like image 124
Turnip Avatar answered Oct 29 '22 19:10

Turnip