Posted in jquery
5
1:45 am, June 30, 2020

add a class or remove it based on window scroll location

if you inspect the header div you will see a class added to it when scrolled down, i have added additional margins to it so you can still see it even when scrolled.

then you add a fixed property to make it stick

HTML

<div class='header'>This is the .header div</div>

CSS

.header {
background:#000;
color:#FFF;
padding:10px;
}
.header-stick {
font-weight:bold;
}

Javascript

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('.header').addClass('header-stick');
  } else {
    $('.header').removeClass('header-stick');
  }
});
This is the .header div

View Statistics
This Week
59
This Month
133
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.
Unknown