jQuery LocalScroll + hashchange事件浏览器兼容性问题

发布于 2024-10-19 22:07:20 字数 1692 浏览 5 评论 0 原文

我正在构建一个使用 Ariel Fleslers jQuery LocalScroll 插件,我想做两件事:

1)单击链接时将滚动动画到锚点
2) 当按下浏览器的后退按钮时,滚动到之前访问过的锚点。

滚动发生在内容溢出的容器内。

第一个很简单。为了获得第二名,我做了相当多的谷歌搜索,但并没有真正找到解决方案,尽管我遇到了一些想要做类似事情的人的帖子。

我最终使用 Ben Almans jQuery hashchange 事件插件 与 LocalScroll 一起达到所需的结果,代码看起来像这样:

    $(document).ready(function(){
    $.localScroll({
        hash:true,
        target:'#container',
        duration:500
        }); 

$(window).hashchange( function(){
    var hash = location.hash;

    if (hash === ''){
    location.hash = '#main';
    }

    $.localScroll.hash({
    reset:false,
    hash:true,
    target:'#container',
    duration:500
    });  
    });
    $(window).hashchange();
    }); 

上面的代码,尽管我在编码方面有点黑客(没有双关语的意思),但它在 Firefox、Explorer 和 Opera 中实现了我想要的功能。但是,我在 Chrome 和 Safari 中遇到了不同的问题。

在 Chrome 中,基本上,当单击链接时,滚动会产生动画,但是当按下浏览器后退按钮时,它只会跳转到锚点,而不会设置过渡动画。但仔细检查后,似乎它偶尔会为后退按钮设置动画,但我无法理解为什么它只在某些时候执行此操作。

在 Safari 中,无论单击链接还是使用后退按钮,它都会跳转到锚点而不使用动画。

在我实现 hashchange 事件解决方案之前,动画在 Safari 中运行,代码如下所示:

 $(document).ready(function() {



$.localScroll({
hash:true,
target:'#container',
duration:500
});
});  

有谁知道 Chrome 和 Safari 中导致这些问题的原因吗?正如我所说,我是一名设计师,而不是编码员,这确实扩展了我的能力,因此请随意指出我在上面的代码中犯的任何错误,无论它们是否与当前的问题无关或不。

另外,感谢 Ariel 和 Ben 提供了这些很棒的插件。

编辑:好吧,这是另一件奇怪的事情,当我将网站上传到本地运行的测试服务器时,在 Safari 中滚动链接单击可以工作,尽管完全相同的代码在我运行它时不起作用-服务器。无论如何,当按下后退按钮时,Safari 和 Chrome 都不会触发动画,因此问题依然存在。

I'm building a site where I am using Ariel Fleslers jQuery LocalScroll plugin, which I want to do two things:

1) Animate a scroll to an anchor when a link is clicked
2) Scroll to the previously visited anchor when the browsers' back button is pressed.

The scrolling takes place inside a container where the content is overflowing.

Number one was straight forward enough. To acheive number two I did a fair bit of googling but didn't really find a solution, even though I came across a few posts by people who were looking to do something similar.

I ended up using Ben Almans jQuery hashchange event plugin together with LocalScroll to acheive the desired result, the code looks like this:

    $(document).ready(function(){
    $.localScroll({
        hash:true,
        target:'#container',
        duration:500
        }); 

$(window).hashchange( function(){
    var hash = location.hash;

    if (hash === ''){
    location.hash = '#main';
    }

    $.localScroll.hash({
    reset:false,
    hash:true,
    target:'#container',
    duration:500
    });  
    });
    $(window).hashchange();
    }); 

The above code, even though I'm somewhat of a hack (no pun intended) when it comes to coding, does what I want in Firefox, Explorer and Opera. However, I get different issues in Chrome and Safari.

In Chrome, basically, the scroll is animated when a link is clicked, but when the browsers back button is pressed it just jumps to the anchor without animating the transition. On a closer inspection though, it seems like it does animate the back button once in a while, but I can't get my head around why it only does it some of the time.

In Safari, it jumps to the anchor without animating regardless of whether a link is clicked or the back button is used.

The animation worked in Safari before I implemented the hashchange event solution, when the code looked like this:

 $(document).ready(function() {



$.localScroll({
hash:true,
target:'#container',
duration:500
});
});  

Does anyone have any ideas as to what is causing these issues in Chrome and Safari? As I said, I'm a designer, not a coder, and this is really stretching my abilities so feel free to point out any mistakes I've made in the code above regardless of whether they have nothing to do with the issue at hand or not.

Also, thanks to Ariel and Ben for these great plugins.

EDIT: Ok so here is another weird thing, when I uploaded the site to a test server that I run locally, scrolling on link click works in Safari, even though the exact same code doesn't work when I run it off-server. In any case, neither Safari nor Chrome triggers the animation when the back button is pressed so that problem remains.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2024-10-26 22:07:20

我对这两个插件都遇到了严重的问题;它们很难使用、缺乏功能并且存在缺陷。

请尝试以下组合:

它将让您的滚动方式更加流畅,支持 HTML5 History API 并适用于所有浏览器。

I had serious issues with both of those plugins; they are hard to use, feature lacking, and buggy.

Try this combination instead:

It'll handle your scrolling way smoother, support the HTML5 History API and work in all the browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文