简体   繁体   中英

Using certain javascript code on mobile devices only?

I am trying to code a way that only allows this particular javascript code to run on screen sizes that are less than 900 pixels. This code is used to set up a simple left and right navigation button on mobile devices. However, when I try and use matchMedia it totally messes up my code but does not throw any errors. How would you approach this issues?

   var flavorScroll = (function() {

        let widthMatch = window.matchMedia("(min-width: 901px)");


    if (widthMatch.matches) {
        var flavorBox1 = document.body.querySelector('#flavor-box-1');
        var flavorBox2 = document.body.querySelector('#flavor-box-2');
        var flavorBox3 = document.body.querySelector('#flavor-box-3');
        var flavorBox4 = document.body.querySelector('#flavor-box-4');

        var buttonRight = document.body.querySelector('#flavorButtonRight');
        var buttonLeft = document.body.querySelector('#flavorButtonLeft');

        var step = 1;

        leftButton.style.visibility = 'hidden';

        function flavorDisplayer(currentStep){
            if(currentStep === 1) {
                flavorBox1.style.display = 'block';

                flavorBox2.style.display = 'none';

                leftButton.style.visibility = 'hidden';
            } else if(currentStep === 2) {
                flavorBox2.style.display = 'block';

                flavorBox1.style.display = 'none';
                flavorBox3.style.display = 'none';

                leftButton.style.visibility = 'visible'; 
            } else if(currentStep === 3) {
                flavorBox3.style.display = 'block';

                flavorBox2.style.display = 'none';
                flavorBox4.style.display = 'none';

                rightButton.style.visibility = 'visible';
            } else if(currentStep === 4) {
                flavorBox4.style.display = 'block';

                flavorBox3.style.display = 'none';

                rightButton.style.visibility = 'hidden';
            }
        }





        buttonRight.addEventListener('click', function() {
            step += 1;

            flavorDisplayer(step);
        });

        buttonLeft.addEventListener('click', function() {
            step -= 1;

            flavorDisplayer(step);
        });

    } else {

        }

})();

This is a handy util I use...

var breakpoint = {
  is(s) {
    const size = s.trim()
    const sizes = {
      xsmall: "480px",
      small: "576px",
      medium: "780px",
      large: "992px",
      xlarge: "1200px",
    }

    if (sizes.hasOwnProperty(size)) {
      // return mq(`only screen and (min-width: ${sizes[size]})`)
      return window.matchMedia(`only screen and (min-width: ${sizes[size]})`).matches
    }

    throw new ReferenceError(`The size ${size} is not a valid breakpoint: ${JSON.stringify(sizes)}`)
  },
}

Then you can wrap your function in an if statement ie

if (breakpoint.is("medium")) {
...
}

Use screen.width to get the current size of the screen the user is working on. Then use that value to limit when the code is executed.

    var scrn = screen.width
    if (scrn < 2000) { console.log(1); };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM