在桌面浏览器中加载jQuery脚本,但不能在移动设备中加载 [英] Load jQuery script in desktop browser, but not mobile

查看:67
本文介绍了在桌面浏览器中加载jQuery脚本,但不能在移动设备中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在桌面浏览器而不是移动浏览器中加载特定的jQuery脚本/功能.目前,该脚本在移动浏览器上运行非常慢,但在台式机浏览器上运行良好.

I want to load a particular jQuery script/function in desktop browsers, but not mobile browsers. At the moment, the script runs very slow on mobile browsers, but works fine on desktop browsers.

该功能是一个平滑滚动插件,因此,当用户单击链接时,页面将平滑滚动到同一页面上的锚点.它不必在移动浏览器上平滑滚动-它太慢了,通常不起作用.

The function in question is a smooth scrolling plugin, so that when a user clicks a link, the page scrolls smoothly to the anchor on the same page. It is not necessary for it to scroll smoothly on mobile browsers - it is too slow, and often doesn't work.

我需要在下面的代码中添加些什么,以确保在台式机浏览器中仍然可以正常执行该代码,而在移动设备(尤其是iPad/iPhone/iPod)中仍可以正常执行该代码?

What would I need to add to my code below to ensure that it is still executed as normal in desktop browsers, but not mobile (particularly iPad/iPhone/iPod)?

  <script>!window.jQuery && document.write(unescape('%3Cscript src="js/lib/jquery/jquery.js"%3E%3C/script%3E'));</script>    
<script src="js/css_browser_selector.js"></script>
<script src="js/src/jquery.smooth-scroll.js">    
</script>
<script src="js/lib/jquery.ba-bbq.js"></script>
<script>
$(document).ready(function() {
  $('a[href*="#"]').live('click', function() {
    if ( this.hash ) {
      $.bbq.pushState( '#/' + this.hash.slice(1) );
      return false;
    }
  });

  $(window).bind('hashchange', function(event) {
    var tgt = location.hash.replace(/^#\/?/,'');
    if ( document.getElementById(tgt) ) {
      $.smoothScroll({scrollTarget: '#' + tgt});
    }
  });

    $(window).trigger('hashchange');
});
 </script>

推荐答案

jsFiddle演示

jsFiddle Demo

寻找touch的存在,如果它不存在,那么您很有可能会使用台式机:

Look for the existence of touch, if it is not present then you are more than likely dealing with a desktop:

编辑:显然不赞成使用我原来的try/catch方法( https://stackoverflow.com/a/4819886/1026459 )

edit: my original try/catch approach was apparently deprecated ( https://stackoverflow.com/a/4819886/1026459 )

$(document).ready(function() {
 var isDesktop = (function() {
  return !('ontouchstart' in window) // works on most browsers 
  || !('onmsgesturechange' in window); // works on ie10
 })();
 //edit, if you want to use this variable outside of this closure, or later use this:
 window.isDesktop = isDesktop;
 if( isDesktop ){ /* desktop things */ }
});

这篇关于在桌面浏览器中加载jQuery脚本,但不能在移动设备中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆