最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Only execute JS function on desktop computers (parallax plugin) - Stack Overflow

programmeradmin2浏览0评论

Is there any way I can restrict the execution (if statement) of javascript so that it only applies to desktop puters, It needs to be off when the user is on a mobile/tablet device. Below is the code, however obviously it applies for any device at the moment when the document is ready.

<script type="text/javascript">
$(document).ready(function(){
    main_parallax();
    main_scrolling();
    sauces_slider();
});    
</script>

Only the main_parallax(); function should be omitted when on a mobile/tablet device.

Is there any way I can restrict the execution (if statement) of javascript so that it only applies to desktop puters, It needs to be off when the user is on a mobile/tablet device. Below is the code, however obviously it applies for any device at the moment when the document is ready.

<script type="text/javascript">
$(document).ready(function(){
    main_parallax();
    main_scrolling();
    sauces_slider();
});    
</script>

Only the main_parallax(); function should be omitted when on a mobile/tablet device.

Share Improve this question edited Dec 23, 2012 at 18:46 ThiefMaster 319k85 gold badges605 silver badges645 bronze badges asked Dec 23, 2012 at 18:39 Muhammed BhikhaMuhammed Bhikha 5,0199 gold badges37 silver badges47 bronze badges 5
  • 2 You Could Take a Look at the Useragent – Moritz Roessler Commented Dec 23, 2012 at 18:40
  • stackoverflow./questions/4117555/… – adeneo Commented Dec 23, 2012 at 18:40
  • Why does the effect need to be off on all mobile devices? Many tablets are strong enough to do all sorts of plex graphical things. Isn't this rather an issue of display resolution? – Pekka Commented Dec 23, 2012 at 18:49
  • You might want to look at Modernizr, which makes it easy to test for "touch" events. There's no really solid way, no matter what library you use, to detect whether a device is a "mobile" device. – Pointy Commented Dec 23, 2012 at 18:49
  • the parallax doesn't work properly on any device apart from desktop – Muhammed Bhikha Commented Dec 23, 2012 at 18:53
Add a ment  | 

1 Answer 1

Reset to default 6

One method is using the user agent, which you could do like this:

$(document).ready(function(){
    if (/Android|BlackBerry|iPhone|iPad|iPod|webOS/i.test(navigator.userAgent) === false) {
        main_parallax(); //Run main_parallax() not a mobile device
    }​

    main_scrolling();
    sauces_slider();
});

I would remend using feature detection instead, try looking into: Modernizr

发布评论

评论列表(0)

  1. 暂无评论