我有一个固定的导航栏,所以当点击导航栏中的某个元素时我需要一点偏移,为此我使用了这段代码(它对导航栏元素非常有效):
var offset = 68;
$('.navbar li a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
但我有两个按钮,它们也链接到网站的某些部分:
<div id="main">
<a href="denea.com"><img src="images/face.png" class="logo"></a>
<div id="promo">
<h2> So, your website doesn't look good anymore? <br><h3> We'll give it a second breath, then support you along the way.</h3></h2>
</div>
<div id="buttons">
<a href="#portfolio"><button id="portbut">
View portfolio
</button></a>
<a href="#contact"><button id="quotebut">
Get a quote
</button></a>
</div>
</div>
但是当我将他们的 id 放在我上面提到的 js 代码中时,它不起作用,在“移动”到网站的某个部分时不考虑偏移量。
我应该编辑什么以便将偏移量考虑到那些两个按钮?
最佳答案
如果有人需要它,答案是:
var offset = 68;
$('#buttons a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
关于javascript - 按钮的 Scrollspy 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108789/