我想在桌面浏览器中加载特定的jQuery脚本/函数,但不是移动浏览器.目前,脚本在移动浏览器上运行速度非常慢,但在桌面浏览器上运行良好.
有问题的功能是一个平滑的滚动插件,因此当用户点击链接时,页面会平滑地滚动到同一页面上的锚点.它没有必要在移动浏览器上平滑滚动 - 它太慢,而且通常不起作用.
我需要在下面的代码中添加什么来确保它仍然在桌面浏览器中正常执行,而不是移动(特别是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>
Run Code Online (Sandbox Code Playgroud)
Tra*_*s J 11
寻找存在touch,如果它不存在那么你很可能处理桌面:
编辑:我原来的try/catch方法显然已被弃用(/sf/answers/337392051/)
$(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 */ }
});
Run Code Online (Sandbox Code Playgroud)