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 trial
Alex Watts
8,396 PointsScroll effect jQuery.
Hello,
I recently created an effect for my navigation in jQuery (see below). It was able to successfully change the colour of its background, once it had reached a position of 300 pixels.
My question is, how can I improve this code? I am new to jQuery and hope that their is a more efficient way of writing what I did.
$(document).scroll(function(){
var nav = $("nav");
var a = $("nav a");
var position = nav.offset().top;
console.log(position);
if(position > 300) {
nav.css('background-color', 'rgba(0,0,0,0.85)');
a.css('color', '#fff');
} else if (position < 300 ) {
nav.css('background-color', '#ecf0f1');
a.css('color', '#000');
}
});
Thanks!!
Ashish Mehra
Courses Plus Student 340 PointsAshish Mehra
Courses Plus Student 340 Pointsonly thing you can do with this according to my knowledge creating variable like this
but if you want to make you code easy to understand you can create function