Javascript 滚动按钮的偏移量
我有一个固定的导航栏,所以当单击导航栏中的某个元素时,我需要一点偏移量,为此我使用了以下代码(它对导航栏元素非常有效): 但我有两个按钮,它们也链接到网站的某些部分:Javascript 滚动按钮的偏移量,javascript,jquery,html,css,twitter-bootstrap,Javascript,Jquery,Html,Css,Twitter Bootstrap,我有一个固定的导航栏,所以当单击导航栏中的某个元素时,我需要一点偏移量,为此我使用了以下代码(它对导航栏元素非常有效): 但我有两个按钮,它们也链接到网站的某些部分: <div id="main"> <a href="denea.com"><img src="images/face.png" class="logo"></a> <div id="promo"> <h2> So, your w
<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>
那么,你的网站看起来不再好了<我们会给它第二次呼吸,然后一路支持你。
但是当我在上面提到的js代码中放置他们的id时,它不起作用,当“移动”到网站的某个部分时,偏移量没有被考虑在内
我应该编辑什么,以便将这两个按钮的偏移量考虑在内?如果有人需要它,答案是:
var offset = 68;
$('#buttons a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
您是否也尝试过给这些div一个类名,然后在js代码中使用该类名?例如@JakeTaylor谢谢,但问题是它被包装在一个标签中
var offset = 68;
$('#buttons a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});