0

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.

4
  • 1
    Keyword: matchMedia Commented Mar 23, 2016 at 14:14
  • 1
    It sounds like you're trying to fix the wrong problem, which is "why does the footer get infinitely scrollable blank space on smaller devices?". Commented Mar 23, 2016 at 14:19
  • if(window.innerWidth > 640) should exclude most mobile devices. Commented Mar 23, 2016 at 14:21
  • @IanKemp, that is another issues I'm having. When using device switcher in Chrome, the footer becomes infinitely scrollable when switching to mobile device, unless you refresh the page. Any ideas on how to avoid that? Commented Mar 24, 2016 at 13:22

1 Answer 1

2

I think you're looking for something like this:

if ( $(window).width() > 739) {     
 //Code here for any desktop
}
else {
   //Code here for mobile
}

You can find device width sizes here:

http://mydevice.io/devices/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.