gpt4 book ai didi

jquery - 在桌面浏览器中加载 jQuery 脚本,但在移动设备上不加载

转载 作者:行者123 更新时间:2023-12-03 22:25:33 24 4
gpt4 key购买 nike

我想在桌面浏览器中加载特定的 jQuery 脚本/函数,但不在移动浏览器中。目前,该脚本在移动浏览器上运行速度非常慢,但在桌面浏览器上运行良好。

该功能是一个平滑滚动插件,因此当用户单击链接时,页面会平滑滚动到同一页面上的 anchor 。它没有必要在移动浏览器上平滑滚动 - 它太慢,而且经常不起作用。

我需要在下面的代码中添加什么,以确保它仍然在桌面浏览器中正常执行,但在移动设备(特别是 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 Demo

查找touch是否存在,如果不存在,那么您很可能正在处理桌面:

编辑:我原来的 try/catch 方法显然已被弃用 ( 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 - 在桌面浏览器中加载 jQuery 脚本,但在移动设备上不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com