简体   繁体   English

防止/停止自动锚链接的发生

[英]Prevent/stop auto anchor link from occurring

I need to prevent the automatic scroll-to behavior in the browser when using link.html#idX and <div id="idX"/>.使用 link.html#idX 和 <div id="idX"/> 时,我需要防止浏览器中的自动滚动到行为。

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).我试图解决的问题是我试图通过检测 url 中的锚点来在页面加载时执行自定义滚动到功能,但到目前为止还无法阻止自动滚动功能(特别是在 Firefox 中) .

Any ideas?有任何想法吗? I have tried preventDefault() on the $(window).load() handler, which did not seem to work.我在 $(window).load() 处理程序上尝试了 preventDefault() ,这似乎不起作用。

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.考虑单击链接中带有#anchor 的另一个网站的链接。 What prevents that autoscroll to the id?是什么阻止了自动滚动到 id?

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.我需要知道是否(以及如何)可以防止在页面加载时自动滚动到#anchors。


NOTE笔记

This isn't really an answer to the question, just a simple race-condition-style kluge.这并不是问题的真正答案,只是一个简单的比赛条件风格的 kluge。

Use jQuery's scrollTo plugin to scroll back to the top of the page, then reanimate the scroll using something custom.使用 jQuery 的 scrollTo 插件滚动回页面顶部,然后使用自定义的东西重新激活滚动。 If the browser/computer is quick enough, there's no "flash" on the page.如果浏览器/计算机足够快,则页面上没有“flash”。

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();

});

Credit goes to wombleton for pointing it out.归功于 wombleton的指出。 Thanks!谢谢!

This seems the only option I can see with ids:似乎是我可以看到的唯一选项:

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

It doesn't automatically scroll to classes.它不会自动滚动到类。

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.因此,如果您使用独特的类来标识您的 div,您在查找元素时会失去一点速度,但会获得您所追求的行为。

(Thanks, by the way, for pointing out the scroll-to-id feature. Never knew it existed.) (顺便说一句,感谢您指出滚动到 ID 功能。从不知道它存在。)

EDIT:编辑:

  1. Scroll first to top (fast, no effects pls), and then call your scroll function.首先滚动到顶部(快速,无效果),然后调用您的滚动 function。 (I know its not so pretty) (我知道它不那么漂亮)
  2. or just use a prefix或者只使用前缀

I know this is an old thread but i found something without the need to scroll.我知道这是一个旧线程,但我发现了一些不需要滚动的东西。 Run this first before any other scripts.在任何其他脚本之前先运行它。 It puts an anchor before the first element on the page that prevents the scroll because it is on top of the page.它在页面上的第一个元素之前放置一个锚点,以防止滚动,因为它位于页面顶部。

function getAnchor(sUrl)
{
  if( typeof sUrl == 'string' )
  {
   var i = sUrl.indexOf( '#' );
   if( i >= 0 )
    { return sUrl.substr( i+1 ).replace(/ /g, ''); }
  }
  return '';
};

var s = getAnchor(window.location.href);
if( s.length > 0 )
 { $('<a name="'+s+'"/>').insertBefore($('body').first()); }

Cheers!干杯! Erwin Haantjes欧文·汉杰斯

This worked well for me:这对我来说效果很好:

1- put this on your css file 1-把这个放在你的 css 文件上

a[name] { position: absolute; top: 0px }

2- put this on your document.ready bind right before you start animating (if you're animating at all) 2-在你开始动画之前把它放在你的document.ready绑定上(如果你正在制作动画)

$("a[name]").css("position","relative");

Might need tweaking depending on your stylesheet/code but you get the idea.可能需要根据您的样式表/代码进行调整,但您明白了。

Credit to: http://cssbeauty.com/skillshare/discussion/1882/disable-anchor-jump/归功于:http://cssbeauty.com/skillshare/discussion/1882/disable-anchor-jump/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM