Given a script that makes the sidebar sticky when the user scrolls to a certain point (on desktop), this currently creates an issue where the footer doesn't show at the bottom on mobile/tablet devices, but gives the footer an infinitely scrollable blank space. How can I target the script to only work on desktop browsers?
I've tried targeting the desktop with a variation of navigator.userAgent.match == "", but this is disabling the functionality.
When the user is coming from a mobile/tablet, the footer should just show up on the page at the bottom as if the javascript function wasn't loaded.
$(function() {
var $sidebar = $(".related-articles"),
$window = $(window),
offset = $sidebar.offset(),
topPadding = 15;
$window.scroll(function() {
if ($window.scrollTop() > offset.top) {
$sidebar.stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
}, 1);
} else {
$sidebar.stop().animate({
marginTop: 0
}, 1);
}
});
});
UPDATE: adding $(window).width() > 767 && to the first if(); statement fixed the issue.
matchMediaif(window.innerWidth > 640)should exclude most mobile devices.