I am new to jQuery and have successfully implemented this code which makes my header stick when the user has scrolled down 100px
However when using media queries, I have reduced the height to 80px at a max-width of 650px and therefore, for 650px and under, I need the jQuery to fire at 80px scroll not 100px.
Is there a way to combine media queries with jQuery?
My jQuery so far is this:
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() >= 100) {
jQuery('#masthead').addClass('fixed-header');
}
else {
jQuery('#masthead').removeClass('fixed-header');
}
});
CSS
.fixed-header {
position: fixed !important;
top: 0;
width: 100%;
}
I had a crack at it and tried this:
if ($(window).width() >= 651) {
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() >= 100) {
jQuery('#masthead').addClass('fixed-header');
}
else {
jQuery('#masthead').removeClass('fixed-header');
}
});
}
else {
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() >= 80) {
jQuery('#masthead').addClass('fixed-header');
}
else {
jQuery('#masthead').removeClass('fixed-header');
}
});
}
}
And
if ( pageWidth > 651 ) {
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() >= 100) {
jQuery('#masthead').addClass('fixed-header');
}
else {
jQuery('#masthead').removeClass('fixed-header');
}
});
}
else {
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() >= 80) {
jQuery('#masthead').addClass('fixed-header');
}
else {
jQuery('#masthead').removeClass('fixed-header');
}
});
}
}
But neither work.
**Edited
I have made a JSFiddle here https://jsfiddle.net/shereewalker/2r8Lcn7j/36/
I intend to ensure that the second header sticks when it reaches the top, which currently functions as I want. But when I reduce the height of the top header for 650px and below to 80px, there is a jump as the sticky position on the second header still applies itself at scrollTop 100px.
Any help would be much appreciated