Posted in jquery
149
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

add a class or remove it based on window scroll location Demo

View Demo Full Screen View Demo New Tab

add a class or remove it based on window scroll location Code

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');
  }
});

Related Tags

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms