Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGabriel Ward
20,222 Pointsadding box-shadow on scroll
I'm trying to add a box-shadow to the bottom of my header on scroll of window. I can't see where I'm going wrong. Any help is greatly appreciated. I have the following html
<header class="main-header">
<nav>
<?php
$defaults = array(
'container' => false,
'theme_location' => 'gabes-menu',
'menu_class' => 'main-nav'
);
wp_nav_menu( $defaults );
?>
</nav>
</header>
the following css
main-header {
position: relative;
padding-top: 10px;
padding-bottom: 10px;
margin-bottom: 10px;
height: 50px;
background: white;
/*background: #003399;*/
}
.box-shadow {
-webkit-box-shadow: 0 1px 3px #bbb; /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
-moz-box-shadow: 0 1px 3px #bbb; /* Firefox 3.5 - 3.6 */
box-shadow: 0 1px 3px #bbb; /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */
}
and the following jQuery
jQuery(document).ready(function($) {
var tb = $('main-header');
tbs = "box-shadow";
$(window).scroll(function() {
if($(this).scrollTop()) {
tb.addClass(tbs);
} else {
tb.removeClass(tbs);
}
});
});
1 Answer
Codin - Codesmite
8,600 Points var $document = $(document),
$element = $('.main-header'),
className = 'box-shadow';
$document.scroll(function() {
if ($document.scrollTop() >= 50) {
// Change 50 to the value you require
// for the event to trigger
$element.addClass(className);
} else {
$element.removeClass(className);
}
});
Should achieve what you are looking for.
Codepen Example here: http://codepen.io/anon/pen/BobQZK (I have set the header to fixed for demonstration so it remains in view while the page scrolls).
(Also in your css you have forgot to define "main-header" as a class with ".")
Karl Taylor
4,043 PointsKarl Taylor
4,043 PointsYou can also add a transition to the CSS by using CSS3 Transitions -
http://codepen.io/anon/pen/avMJPV
Gabriel Ward
20,222 PointsGabriel Ward
20,222 Pointsthank you Ashley.