0

I have the following code:

    $( window ).resize(function() {
        if (matchMedia('only screen and (min-width: 992px)').matches) {
            $('#second').parallax();
            $('.sketches-1').parallax();
            $('#fifth').parallaxfifth();
        }
        else{
            //...
        }
   }); 

I wish to remove the parallax function on mobile devices, but how can I achieve this?

1 Answer 1

1

Use:

var parallax = function() {
    if (matchMedia('only screen and (min-width: 992px)').matches) {
        $('#second').parallax();
        $('.sketches-1').parallax();
        $('#fifth').parallaxfifth();
    }
    else{
        //...
    }
};
$(window).resize(parallax); 
//Some code here...
$(window).off('resize', parallax);

If you don't want to use this effect on mobile simply use:

function isMobile() {
  /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

if (!isMobile()) {
  $(window).resize(parallax); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to unbind only the parallax function when mobile, but bind it again if I'm not mobile, can I get this with your code? I was hoping I could do this on the else
If it is mobile don't bind, why you should unbind?
I'm trying to test some functionality and it involves resizing my window, if I do that, I will have parallax function on mobile too (if I resize a couple times), which I can't afford. (btw I can't reload)
Don't reload. In my answer above there are two solutions. Just use off as in the example in the answer :-).

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.