如果检测浏览器具有平滑滚动功能已 [英] Detect if browser has smooth scrolling feature already

查看:121
本文介绍了如果检测浏览器具有平滑滚动功能已的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加平滑滚动到哈希链接时使用这块JavaScript的一个矿点。

I have added smooth scrolling to a site of mine using this piece of JavaScript when clicking on hash links.

$('a[href*=#]')
    .click(onAnchorClick);

function onAnchorClick(event)
{
    return ! scrollTo(this.hash);
}


function scrollTo(target)
{
    var e = $(target);
    var y = e.exists() ? e.offset().top : 0;

    if(y == 0 && target != '#top')
        return false;

    if(Math.max($('html').scrollTop(), $('body').scrollTop()) != y)
        $('html,body')
            .animate({scrollTop: y}, 500, function() { location.hash = target; } );
    else
        location.hash = target;

    return true;
}

$.fn.exists = function()
{
    return this.length > 0 ? this : false;
}

在桌面浏览器的工作太棒了,看起来罚款至少在iOS设备正常工作。然而,我的WinPhone 8设备上它是垃圾。滚动得一塌糊涂,甚至最终没有在它应该。所以我决定不通过启用一个有如果(!/的Windows Phone 8 \\ .0 / .TEST(navigator.userAgent的))

Works fantastic in desktop browsers and looks to work fine on at least iOS devices as well. However, on my WinPhone 8 device it was garbage. Scrolling was a mess and didn't even end up where it should. So I decided to not enable it there through an if( ! /Windows Phone 8\.0/.test(navigator.userAgent)).

现在它工作得很好,而且似乎对WinPhone浏览器实际上是默认平滑滚动,这是伟大的。

Now it works well, and seems the browser on the WinPhone actually is smooth scrolling by default, which is great.

但它当然是有点哑有一个平滑滚动脚本积极的,如果浏览器在默认情况下已经这样做了。有没有一种方法,如果浏览器已经启用了平滑滚动功能我能察觉?

But it is of course a bit dumb to have a smooth scroll script active if the browser already does this by default. Is there a way I can detect if a browser already has a smooth scrolling feature enabled?

推荐答案

是,也不是。不幸的是,这些类型的东西没有标准。但也有变通,即就是你已经在做:浏览器嗅探

Yes and no. Unfortunately there are no standards for these types of things. However there are work arounds, one of which you are already doing: browser sniffing.

基本上,这是我们所先进的这种检测去,因为某些浏览器还不甚至支持平滑滚动正式或不实验发展(如铬)。除非大部分是在同一页上的标准将不会被设置。不仅如此,但它也归结为GPU硬件能力,因为一些设备和计算机使用平滑滚动和动画奋斗。所以从技术上来说,即使浏览器确实支持平滑滚动,谁又能说设备或台式机上运行它可以渲染速度不够快,即使显示的效果。这是另一个瓶颈。

Basically, that's about as advanced as this kind of detection goes because some browsers don't yet even support smooth scrolling officially or without experimental developments (like Chromium). And standards won't be set unless the majority are on the same page. Not only that but it also comes down to GPU hardware abilities as some devices and computers struggle with smooth scrolling and animations. So technically speaking, even if a browser did support smooth scrolling, who's to say the device or desktop running it can render fast enough to even display the effect. That's another bottle neck.

我相信有一天,未来会有更多的需要浏览器的功能规范,如这更好的提高用户交互,但直到那一天,你正在做的是正确的。

I believe someday in the future there will be more of a need for browser feature specifications such as this to better improve user interactions, but until that day you're doing it right.

这篇关于如果检测浏览器具有平滑滚动功能已的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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