我在我的网站顶部使用一个高约20像素的静态栏。当我点击锚链接时,文本的一部分会消失在顶部栏后面(对于那些不知道的人,维基百科的导航就是这样工作的。点击标题,浏览器会滚动到该位置)。是否有办法防止这种情况发生?我没法使用iFrame。我唯一能想到的就是每次滚动一点来把它滚回去,但还有其他方法吗?有没有一些CSS设置可以操纵页面主体或其他东西?
<a class="anchor"></a>
您可以通过将锚点元素设置为块级元素,并进行相对定位,使其在页面上以比实际位置更高或更低的偏移量进行定位。-250px 将把锚点上移 250px。
a.anchor{display: block; position: relative; top: -250px; visibility: hidden;}
通过Jan的方式查看如何偏移HTML锚点以适应固定的标题
display: block
是问题所在。我尝试过类似的事情,但浏览器会忽略相对偏移,最终我使用了 a[name] {position: absolute; height: 150px; margin-top: -150px;}
。 - rivscroll-margin-top: 20px;
scroll-margin-top: 20px;
仅在滚动上下文环境中起作用,实际效果为添加一个空白边距。例如,在元素上使用scroll-margin-top: 20px;
将使单击该元素的锚标签时,页面滚动到该元素上方20像素处。
不支持 IE。Safari版本11-14使用非标准名称:scroll-snap-margin-top
(但功能与预期相同)。Safari 14.1+使用标准名称。
nav{
position: fixed;
top:0;
left:0;
background: black;
color: white;
width: 100%;
}
#after-nav{
margin-top: 25px;
}
#section-1{
width: 100%;
background: green;
height: 500px;
scroll-margin-top: 20px;
}
#section-2{
width: 100%;
background: blue;
height: 500px;
scroll-margin-top: 20px;
}
ul{
margin-top: 0;
}
<nav>
Nav bar: 20px (as in OP)
</nav>
<div id="after-nav">
Table of contents:
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#section-2">Section 2</a></li>
</ul>
<div id="section-1">Section 1</div>
<div id="section-2">Section 2</div>
</div>
另外一种方法是添加边框:
div{
height: 650px;
background:#ccc;
/*the magic happens here*/
border-top:42px solid #fff;
}
ul{
top: 0;
width: 100%;
height:20px;
position: fixed;
background: deeppink;
margin:0;
padding:10px;
}
li{
float:left;
list-style:none;
padding-left:10px;
}
div:first-of-type{
margin-top:0;
}
<!-- content to be placed inside <body>…</body> -->
<ul>
<li><a href="#s1">link 1</a>
<li><a href="#s2">link 2</a>
<li><a href="#s3">link 3</a>
<li><a href="#s4">link 4</a>
</ul>
<div id="s1" class="first">1</div>
<div id="s2">2</div>
<div id="s3">3</div>
<div id="s4">4</div>
然而,并非总是适用。
针对javascript的解决方案,您可以使用附加到锚点元素的点击事件,滚动调整像素量如下:
document.querySelector("a").addEventListener("click",function(e){
// dynamically determining the height of your navbar
let navbar = document.querySelector("nav");
let navbarheight = parseInt(window.getComputedStyle(navbar).height,10);
// show 5 pixels of previous section just for illustration purposes
let scrollHeight = document.querySelector(e.target.hash).offsetTop - navbarheight - 5;
/* scrolling to the element taking the height of the static bar into account*/
window.scroll(0,scrollHeight);
/*properly updating the window location*/
window.location.hash = e.target.hash;
/* do not execute default action*/
e.preventDefault();
});
nav{
position:fixed;
top:0;
left:0;
right:0;
height:40px;
text-align:center;
background:#bada55;
margin:0;
}
a{
display:block;
padding-top:40px;
}
#section1{
height:800px;
background:repeating-linear-gradient(45deg,#606dbc55,#606dbc55 10px,#46529855 10px,#46529855 20px);
}
#section2{
height:800px;
background:repeating-linear-gradient(-45deg,#22222255,#22222255 10px,#66666655 10px,#66666655 20px);
}
<nav>static header</nav>
<a href="#section2">jump to section 2</a>
<div id="section1">Section 1</div>
<div id="section2">Section 2</div>
:target {padding-top: 20px;}
可以工作,如果你链接到一个块级元素(我假设你这样做了,因为你的问题中提到了div)。然而,当你手动滚动后,它可能看起来不太好。示例http://dabblet.com/gist/3121729
尽管如此,我认为使用一点JavaScript来解决这个问题会更好。$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
var $trget = $(target);
// Example: your header is 70px tall.
var newTop = $trget.offset().top - 70;
$('html, body').animate ({
scrollTop: newTop
}, 500, 'swing', function () {
window.location.hash = target;
});
});
html, body
的原因是为了浏览器兼容性。https://dev59.com/r2Ik5IYBdhLWcg3wadcD#19738288 - PJ Brunet在查看了以上所有答案后,能够解决我的滚动锚点问题的是以下代码:
代码:
#idname {
scroll-margin-top: 20px;
}
感谢那些提出这个答案的人。您可以显然更改边距的像素数。我将其从20px
更改为40px
,现在它完美运行。
尝试使用window.scrollBy(xnum,ynum);
xnum:在x轴(水平)上要滚动的像素数 ynum:在y轴(垂直)上要滚动的像素数
例如:http://www.w3schools.com/js/tryit.asp?filename=try_dom_window_scrollby