M
M
Mikhail Lebedev2021-07-12 20:23:36
JavaScript
Mikhail Lebedev, 2021-07-12 20:23:36

Add/remove class after 200px scroll?

var oldScrollY = 0;
var div = document.querySelector('.site-header');

window.onscroll = function() {
  var scrolled = window.pageYOffset || document.documentElement.scrollTop;
  var dY = scrolled - oldScrollY;
  
  if ( dY > 0 ){
    div.className = 'site-header site-header-top';
  } else {
    div.className = 'site-header site-header-bottom';
  }
  
  oldScrollY = scrolled;
}


This code adds a class immediately when scrolling, thereby breaking the layout. How to make it start its work (add/delete throughout the entire page, but after 200 top pixels?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2021-07-13
@ThunderCat

Hanging an event on onscroll is a bad habit and outdated practice. Today there is an Intersection Observer for this .

S
Stepan, 2013-11-24
@L3n1n

The problem is that /?abrakadabra123 there is such a page, and it is the DirectoryIndex page defined in .htaccess.

S
svd71_1, 2013-11-24
@svd71_1

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !/cms/urlmodifier.php$
RewriteRule ^(.*)$ /cms/urlmodifier.php?param=$1 [L,QSA]

further in urlmodifier.php include the necessary page after parsing the request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question