I want to add/remove a class based on scroll position AND window width. The class should be added when the user scrolls vertically past X AND the window width is larger than Y. If BOTH conditions are not met, the class should be removed. If the user scrolls back to the top, the class should be removed as well.
Both the scroll position and window width values must be dynamic, so the values are measured continuously.
Basically, I need to combine the following functions that each work on their own.
Thanks in advance for any help!
The scroll function that works:
jQuery(function($){
var shrinkHeader = 200; //
// Add dynamic header class
$(window).scroll(function () {
var scroll = getCurrentScroll();
if ( scroll >= shrinkHeader) {
$('.site-header').addClass('shrink');
}
else {
$('.site-header').removeClass('shrink');
}
});
The width function that works:
jQuery(function($){
var shrinkHeader = 200;
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize >= 1151) {
$('.site-header').addClass('shrink');
}
else {
$('.site-header').removeClass('shrink');
}
}
checkWidth(); // Check window width on load
$(window).resize(checkWidth); // Check window width on resize