简体   繁体   English

Chrome忽略了网址中的哈希

[英]Chrome ignoring hashes in URL

I've spent quite a while trying to find answers for this issue, but haven't had any success. 我花了相当长的时间试图找到这个问题的答案,但是还没有成功。 Basically I need to scroll the user to the contact portion of the website when they go to healthdollars.com/#contact . 基本上,当他们转到healthdollars.com/#contact时,我需要将用户滚动到网站的联系部分。 This works just fine in Safari, but in Chrome I haven't had any luck. 这在Safari中工作得很好,但在Chrome中我没有任何运气。 I've tried using jQuery/Javascript to force the browser to scroll down, but I haven't been able to. 我曾尝试使用jQuery / Javascript强制浏览器向下滚动,但我一直无法这样做。

Does anyone have any ideas? 有人有什么想法吗? It's driving me crazy - especially since it's such a simple thing to do. 这让我发疯-尤其是因为这样做很简单。

Not a full answer but in Chrome if you disable Javascript I believe you get the desired behavior. 不是一个完整的答案,但是在Chrome中,如果您禁用Javascript,我相信您会获得所需的行为。 This makes me believe that something in your JavaScript is preventing default browser behavior. 这使我相信您的JavaScript中的某些内容阻止了默认浏览器行为。

It looks to me like the target element doesn't exist when when page first loads. 在我看来,首次加载页面时目标元素不存在。 I don't have any problem if I navigate to the page and then add the hash. 如果导航到页面然后添加哈希,我没有任何问题。

if (window.location.hash.length && $(location.hash)) {
  window.scrollTo(0, $(location.hash).offset().top)
}

check for a hash, find the element's page offset, and scroll there (x, y). 检查哈希,找到元素的页面偏移量,然后在此处滚动(x,y)。

edit: I noticed that, in fact, the page starts at #contact, then scrolls back to the top. 编辑:我注意到,实际上,该页面从#contact开始,然后滚动回到顶部。 I agree with the other answerer that there's something on your page that's scrolling you to the top. 我同意其他答复者的观点,即您页面上的某些内容将您滚动到顶部。 I'd search for that before adding a hack. 在添加黑客之前,我会先进行搜索。

You can do this with JS, for example` if you have JQuery. 您可以使用JS进行此操作,例如,如果您有JQuery。

$(function(){
  // get the selector to scroll (#contact)
  var $to = $(window.location.hash);

  // jquery animate
  $('html'/* or body */).animate({ scrollTop: $to.offset().top });
});

The name attribute doesn't exists in HTML 5 so chrome looks to have made the name attribute obsolete when you use the DOCTYPE html. 名称属性在HTML 5中不存在,因此当您使用DOCTYPE html时,chrome看起来已经使名称属性过时了。

The other browsers have yet to catch up. 其他浏览器尚未赶上。

Change 更改

<a name="contact"></a>

to

<a id="contact"></a>

Maybe this workaround with vanilla javascript can be useful: 也许使用香草javascript的变通办法可能会有用:

// Get the HTMLElement that you want to scroll to. 
var element = document.querySelector('#contact');
// Stories the height of element in the page.
var elementHeight = element.scrollHeight;
// Get the HTMLElement that will fire the scroll on{event}.
var trigger = document.querySelector('[href="#contact"]');    
trigger.addEventListener('click', function (event) {
  // Hide the hash from URL.
  event.preventDefault();
  // Call the scrollTo(width, height) method of window, for example.
  window.scrollTo(0, elementHeight);
})

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

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