防止/停止发生自动锚链接 [英] Prevent/stop auto anchor link from occurring

查看:62
本文介绍了防止/停止发生自动锚链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用link.html#idX和< div id =idX/>时,我需要阻止浏览器中的自动滚动行为。

I need to prevent the automatic scroll-to behavior in the browser when using link.html#idX and <div id="idX"/>.

我试图解决的问题是我试图通过检测网址中的锚来尝试在页面加载上执行自定义滚动功能,但到目前为止还无法阻止自动滚动功能(特别是在Firefox中) )。

The problem I am trying to solve is where I'm trying to do a custom scroll-to functionality on page load by detecting the anchor in the url, but so far have not been able to prevent the automatic scrolling functionality (specifically in Firefox).

任何想法?我在$(window).load()处理程序上尝试过preventDefault(),它似乎不起作用。

Any ideas? I have tried preventDefault() on the $(window).load() handler, which did not seem to work.

让我重申这是针对非链接的在滚动的页面中单击;它适用于滚动页面加载的链接。想一想点击链接中带有#anchor的其他网站的链接。什么阻止自动滚动到id?

Let me reiterate this is for links that are not clicked within the page that scrolls; it is for links that scroll on page load. Think of clicking on a link from another website with an #anchor in the link. What prevents that autoscroll to the id?

每个人都明白我不是在寻找一种解决方法;我需要知道是否(以及如何)阻止在页面加载时自动滚动到#anchors。

Everyone understand I'm not looking for a workaround; I need to know if (and how) it's possible to prevent autoscrolling to #anchors on page load.

这不是真的问题的答案,只是一个简单的竞争条件风格的kluge。

This isn't really an answer to the question, just a simple race-condition-style kluge.

使用jQuery的scrollTo插件滚动回页面顶部,然后使用自定义内容恢复滚动。如果浏览器/电脑足够快,页面上没有闪光灯。

Use jQuery's scrollTo plugin to scroll back to the top of the page, then reanimate the scroll using something custom. If the browser/computer is quick enough, there's no "flash" on the page.

我觉得很脏,只是建议这个......

I feel dirty just suggesting this...

$(document).ready(function(){

    // fix the url#id scrollto "effect" (that can't be
    // aborted apparently in FF), by scrolling back
    // to the top of the page.
    $.scrollTo('body',0);

    otherAnimateStuffHappensNow();

});

信用转到 wombleton 指出来了。谢谢!

Credit goes to wombleton for pointing it out. Thanks!

推荐答案

这似乎是我可以用ID看到的唯一选项:

This seems the only option I can see with ids:

$(document).ready(function() {
  $.scrollTo('0px');
});

它不会自动滚动到班级。

It doesn't automatically scroll to classes.

因此,如果你识别出具有独特类别的div,你会在查找元素时失去一些速度,但会获得你所追求的行为。

So if you identify your divs with unique classes you will lose a bit of speed with looking up elements but gain the behaviour you're after.

(谢谢,顺便说一句,指出滚动到id的功能!从来不知道它存在。)

(Thanks, by the way, for pointing out the scroll-to-id feature! Never knew it existed.)

编辑:

这篇关于防止/停止发生自动锚链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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