The below code is designed to add the class .active to my div (#sidebar) when the user is at the top of the page. Also, if further down the page and the div is manually expanded (adding .active), the .active class will remain unless the user moves 100px up or down (.line is used as a marker to measure this).
I'm trying to adjust the below code to retain all of this functionality, but to disable it entirely when on mobile, i.e less than 414px.
Any suggestions?
/* Expand/Collapse sidebar based on position in page */
var relativeY = $("#sidebar").offset().top - $(".line").offset().top;
$(window).scroll(function(){
relativeY = $("#sidebar").offset().top - $(".line").offset().top;
console.log(relativeY);
if(relativeY > 100 || relativeY < -100 ) {
$("#sidebar").removeClass("active");
$("#sidebarToggle").removeClass("active");
$(".line").css("top","0")
}
else {
$("#sidebar").addClass("active");
$("#sidebarToggle").addClass("active");
}
});
EDIT
I have tried to implement ($window.width() > 414) below but it doesn't work?
/* Expand/Collapse sidebar based on position in page */
var relativeY = $("#sidebar").offset().top - $(".line").offset().top;
$(window).scroll(function(){
relativeY = $("#sidebar").offset().top - $(".line").offset().top;
console.log(relativeY);
if ($window.width() > 414) {
if (relativeY > 100 || relativeY < -100) {
$("#sidebar").removeClass("active");
$("#sidebarToggle").removeClass("active");
$(".line").css("top", "0")
} else {
$("#sidebar").addClass("active");
$("#sidebarToggle").addClass("active");
}
}
});