用href=#whereiwantscroll在html或css中滚动偏移量超过20px



看看问题:屏幕截图

我添加了一个带有自动滚动按钮的粘性div,使用:<a class="petite-police-tel" href="#BISHOKUS">BISHOKUS</a>

在名为BISHOKUS的div中:<div id="BISHOKUS"...

但我添加了一个粘性,所以它与div的顶部不对齐。

所以,我的问题是:如何移动几个像素就像这样:屏幕截图2如果它在JS上,请帮我把它插入代码中,因为我对它不太了解


看看我的代码:

<div class="sticky">

<li style="padding: 5px; margin: 10px; display: inline-block; cursor: pointer;">

<a class="petite-police-tel" href="#BISHOKUS" style="margin-top: 20px;">BISHOKUS</a>

</li>

看看我在div中的代码:

<div id="BISHOKUS" class="cases-progr" style="position: static;background-color: #202225; padding: 25px; margin-top: 20px; margin-bottom: 10px; border-radius: 1.25rem;">

<h1 style="margin-top: center;">Commandes Bishokus</h1>

</div>

id="BISHOKUS"div上,给它以下css:

/* The offset value should be the height of the sticky div */
margin-top: -50px;
padding-top: 50px;

TL;DR实际情况是,我们将整个div向上移动50像素(带负边距(,然后将div内容再次向下移动50像素,带正填充。这使得滚动停止在div的起始位置,但内容偏移了50px,因此我们有效地进行了滚动偏移。

请注意,div现在比原来高了50px,所以其他样式可能会让它变得奇怪,例如,如果它有背景色,它看起来会出错,所以你需要解决这个问题。

编辑:在您的情况下,我相信新的div更容易解决您的问题。在BISHOKUSdiv周围创建一个新的包装器div,并将id移到该包装器上,并对其进行适当的样式设置。

(此外,我使用了50px的偏移量,因为我认为20px还不够,但您可以判断,只需记住将marginpadding的值更改为相同的值(:

// Move the id property up to the wrapper div
<div id="BISHOKUS" style="margin-top: -50px;padding-top: 50px">
<div class="cases-progr" style="position: static;background-color: #202225; padding: 25px; margin-top: 20px; margin-bottom: 10px; border-radius: 1.25rem;">
<h1 style="margin-top: center;">Commandes Bishokus</h1>
</div>
</div>

我希望您创建一个新div的原因是,您已经在使用margin,并且要正确设置margin(只有一个div(,您必须计算一个新的margin-top(这并不难:20px+50px=70px(,但如果您需要多个链接而不是一个链接,则使用包装器div会使其更易于重用

最新更新