I have a simple javascript scroll function that fixes my sidebar navigation and works fine on it's own, but I need to adjust it so different page types have different scroll events. Example: pages with banners need to have the fixed sidebar at a different scroll point that pages without a sidebar.
The indicator of a different page will be that the sidebar for a page without a banner will have a parent div that a page with a banner will not have. I am using a wordpress theme so my customization on this is limited.
Below is my example HTML and JS, but you can also see it at : http://jsfiddle.net/61L4ac2j/
HTML:
<div id="mk-page-id-1" class="theme-page-wrapper mk-main-wrapper">
<aside id="mk-sidebar" class="mk-builtin test">
<div class="sidebar-wrapper" style="min-height:600px;">
Page without banner example
</div>
</aside>
</div>
<div id="mk-page-id-2" class="theme-page-wrapper">
</div>
<div class="theme-page-wrapper">
<aside id="mk-sidebar" class="mk-builtin">
<div class="sidebar-wrapper" style="min-height:600px;">
Page with banner example
</div>
</aside>
</div>
JS:
$(function() {
var div = $(".sidebar-wrapper");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (($("sidebar-wrapper").parent().parent("mk-main-wrapper")) & (scroll > 147)) {
div.css( "background", "yellow" );
} else if (scroll > 263) {
div.css( "background", "green" );
} else {
div.css( "background", "blue" );
}
});
});
For simplification purposes, I used background color, but my actual JS will use "addclass". See JS below:
$(function() {
var div = $(".sidebar-wrapper");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if ($("sidebar-wrapper").parent().parent("mk-main-wrapper") & scroll > 147) {
div.addClass("fixed-product-nav");
} else if (scroll > 263) {
div.addClass("fixed-product-nav");
} else {
div.removeClass("fixed-product-nav");
}
});
});
The issue is I cannot get the first IF Statement true, which you can see at the JSFiddle. It looks like something with the parent elements.
I feel like I'm close and just missing something.
Thanks!