You could just use CSS without any javascript.
(您可以只使用CSS而无需任何JavaScript。)
Give your anchor a class:
(给您的锚课程:)
<a class="anchor" id="top"></a>
You can then position the anchor an offset higher or lower than where it actually appears on the page, by making it a block element and relatively positioning it.
(然后,可以通过将锚定为一个块元素并将其相对定位,来将锚定的位置比页面上实际显示的位置高或低。)
-250px will position the anchor up 250px (-250px将锚定位置上移250px)
a.anchor {
display: block;
position: relative;
top: -250px;
visibility: hidden;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…