0

Hello, I am trying to remove this Javascript code from mobile, as it is affecting it.

window.onscroll = function() {myFunction()};
var header = document.getElementById("header");
var sticky = header.offsetTop;
function myFunction() {
  if (window.pageYOffset > sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}

I have added this code inside the head but it's still not working

    function loadJs(isMobile) {
        if (!isMobile) {
            var fileref = document.createElement('script');
            fileref.setAttribute("type", "text/javascript");
            fileref.setAttribute("src", "mymenu.js");
            document.head.appendChild(fileref)
        }
    }
    var mobile = false;
    function checkMobile() {
       loadJs(mobile);
    }
    checkMobile();
Rita
  • 77
  • 6
  • 1
    Simply set `window.onscroll = null;`, if a mobile was detected. – Teemu Jan 20 '20 at 06:27
  • I have tried this but it disabled the sticky function on desktop too... – Rita Jan 20 '20 at 07:07
  • 1
    Then your `isMobile` value is not correct, or rather it looks like it's not correctly used. `if (isMobile) {window.onscroll = null;}` should do, if the value of `isMobile` is truthy only in mobile devices. – Teemu Jan 20 '20 at 07:16
  • 1
    If you're specifically looking for how to detect a mobile browser, see https://stackoverflow.com/q/3514784/1169519 – Teemu Jan 20 '20 at 07:40

0 Answers0