I have been working on implementing jQuery that can respond in sync with my CSS media queries on my website.
Currently, I am facing an issue where certain objects slide onto the webpage at specific scroll points. To ensure that these objects remain responsive, I need to adjust the scroll points using media queries.
Here is a snippet of the code I am currently using:
jQuery(window).scroll(function () {
//media query scroll point @ min 768, max 995
if (document.documentElement.clientWidth <= 995 && document.documentElement.clientWidth >= 768) {
if (jQuery(this).scrollTop() > 400) {
if (jQuery('.rightSlideB').hasClass('visible') === false) {
jQuery('.rightSlideB').stop().animate({
right: '0px'
}, function () {
jQuery('.rightSlideB').addClass('visible');
});
}
}
} //end media query
//default scroll point
if (jQuery(this).scrollTop() > 250) {
if (jQuery('.rightSlideB').hasClass('visible') === false) {
jQuery('.rightSlideB').stop().animate({
right: '0px'
}, function () {
jQuery('.rightSlideB').addClass('visible');
});
}
} //end default scroll point
}); //end function
The CSS media query that dictates how my content responds looks like this:
@media only screen and (min-width: 768px) and (max-width: 995px) {}
Despite my efforts, the jQuery effects are not being achieved, and I acknowledge that my code is not optimized as there is a repetitive block of code:
if (jQuery('.rightSlideB').hasClass('visible') === false) {
jQuery('.rightSlideB').stop().animate({
right: '0px'
}, function () {
jQuery('.rightSlideB').addClass('visible');
});
}
This repetitive code segment is present in every media query. How can I streamline this section to avoid redundancy since it is used across multiple queries?
EDIT: Would it be recommended to place this repeating block into a function and then call it whenever needed?
I would appreciate any insights or guidance on what could be missing from my implementation. Thank you.