这是我在网页上使用的代码,
<li><a href="/explore/#Sound">Sound</a></li>
(出现在所有页面上的菜单中)
<a id="Sound"><a>
我想在页面上添加链接,但只有在Chrome浏览器中,当我想要滚动到标签时,标签上的内容才能显示。但这些锚点适用于IE和Firefox浏览器。有什么建议吗?
这是我在网页上使用的代码,
<li><a href="/explore/#Sound">Sound</a></li>
(出现在所有页面上的菜单中)
<a id="Sound"><a>
我想在页面上添加链接,但只有在Chrome浏览器中,当我想要滚动到标签时,标签上的内容才能显示。但这些锚点适用于IE和Firefox浏览器。有什么建议吗?
结果表明这是一些版本的Chrome中的错误,为需要的任何人发布解决方法!:)
$(document).ready(function () {
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (window.location.hash && isChrome) {
setTimeout(function () {
var hash = window.location.hash;
window.location.hash = "";
window.location.hash = hash;
}, 300);
}
});
:)
- Praveen Kumar Purushothamanwindow.location.replace(hash)
解决了这个问题。 - dariush我尝试了之前发布的解决方法,但对我无效。经过几天的搜索,我终于找到了一个完美的解决方案,所以觉得值得分享:
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
如果你像我一样从外部网站找到 SPA 网站的锚点链接无法工作。是的,浏览器加载页面的速度太快了,它在加载页面时找不到你的锚点。
为了解决这个问题,在你的 SPA 生命周期中(React 中的 useEffect
和 Vue 中的 mounted
),添加一些代码来检查 URL 锚点并自己滚动到相应位置。
以下是使用 React 的示例:
useEffect(()=> {
if(document.location.hash === '#some-anchor') {
setTimeout(()=> {
document
.querySelector("#some-anchor")
.scrollIntoView({ behavior: "smooth", block: "start" })
}, 300)
}
}, [])
我也遇到了这个问题(使用链接进行同一页导航),解决方案非常简单(虽然让人沮丧难以想出)。希望这可以帮助你 - 我还检查了IE,Firefox和Chrome,它可以跨浏览器正常工作(截至2019年5月22日)。
你的链接应该像这样:
<a href="pagename.html##anchorname">Word as link you want people to click</a>
你的锚点应该长成这样:
<a name="#anchorname">Spot you want to appear at top of page once link is clicked</a>
loading='lazy'
和decoding='async'
,或者为它们设置width
和height
属性。对我来说,这方法非常有效。<li><a href="/explore/##Sound">Sound</a></li>
<a id="#Sound"><a>
这在所有版本的Chrome浏览器中都可以很好地工作!
我已经在我的浏览器上测试过了。
不确定这是否有所帮助,但我意识到我的锚点在IE中可以工作,但在Firefox或Chrome中无法工作。最终我添加了##到我的锚点中,这解决了问题。
例如: a href="##policy">目的和政策声明
而不是:
a href="#policy">目的和政策声明
angular.module('myapp').run(function($transitions, $state, $document, $timeout) {
var finishHook = $transitions.onSuccess({}, function() { // Wait for the complete routing path to settle
$document.ready(function() { // On DOM ready - check whether we have an anchor and Chrome
var hash;
var params;
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
finishHook(); // Deregister the hook; the problem happens only on initial load
if ('#' in $state.params && isChrome) {
hash = $state.params['#']; // Save the anchor
params = _.omit($state.params, ['id', '#']);
$timeout(function() {
// Transition to the no-anchor state
$state.go('.', params, { reload: false, notify: false, location: 'replace' });
$timeout(function() {
// Transition back to anchor again
$state.go('.', _.assign($state.params, { '#': hash }), { reload: false, notify: false, location: 'replace' });
}, 0);
}, 0);
}
});
}, {invokeLimit: 1});
});
我花了一些时间找到了适当的解决方案来解决这个问题。在我的情况下,这与浏览器版本和链接本身无关,而是由于页面在Next.js中没有完全加载。因此,加载时的水合作用还没有到达我的id,这就是为什么它不在页面上,浏览器也不知道它的存在。因此,它也无法滚动到那里。
在我这种情况下,最简单的解决方案是将id =“some-id”从section组件移动到索引页面本身,并使用div将section组件包裹起来并使用相同的id:
<div id="some-id">
<Video />
</div>
它能够工作是因为页面组件在页面加载时加载,但深度嵌套的组件稍后才被注入。
<html>
<body>
<li>
<a href="#Sound">Sound</a>
</li>
<a id="Sound" href="www.google.com">I AM CALLED</a>
</body>
</html>
使用这段代码,它将调用ID值为"sound"的锚点标签