-1

I would like to disable part of my jQuery script for mobile devices, heres the code I'd like to disable:

$('#inner-slide1').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide2').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide3').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide4').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

$('#inner-slide5').click(function(e) {
    e.preventDefault();
    dataslide = $(this).attr('data-slide');
    goToByScroll(dataslide);
});

Grateful for any suggestions.

Jérôme
  • 2,070
  • 15
  • 21
andbamnan
  • 1,285
  • 2
  • 11
  • 8

3 Answers3

8

You're repeating a function 5 times when you need only do it once if you just give those elements a class (I'm going for .inner-slide):

var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;

$('.inner-slide').click(function(e) {
    if(!isMobile) {
        e.preventDefault();
        dataslide = $(this).attr('data-slide');
        goToByScroll(dataslide);
    }
});

The if statement will only result to true on a mobile device (Android,webOS,iPhone,iPad,iPod,Blackberry)

And please note that

$(this).attr('data-slide');

Can also be written as:

$(this).data('slide');
George
  • 36,413
  • 9
  • 66
  • 103
0

Using the code from http://detectmobilebrowsers.com/ use the JS code to detect if mobile - very comprehensive detection... will require a bit of modification as their function will redirect... but should be easy to do :)

Also here is another good answer from here: disable script for mobiles

Community
  • 1
  • 1
Brian
  • 8,418
  • 2
  • 25
  • 32
0

You may try

MatchMedia.js

There is details on https://github.com/paulirish/matchMedia.js

You can load the plugins according to the certain viewport size.