现在我正在建立一个咨询网站,我正在使用Bootstrap,网站只包含一个页面,为了更加实用,我将一些按钮放在导航栏中固定顶部和目标那就是当你点击按钮,页面滚动到主题,但我有一个问题,当我点击两次它似乎有一个错误,当我在一个特定的主题,我想改变点击另一个按钮,页面滚动错误,像几个像素更高(?????)。我真的不知道发生了什么,我尝试了每一个js修复,没有任何帮助我。
这是我的代码:
$(document).ready(function() {
setBindings();
});
function setBindings() {
$("nav a").click(function(e) {
//e.preventDefault();
e.preventDefault();
var sectionID = "conteudo-" + e.currentTarget.id;
$("html body").animate({
scrollTop: $("#" + sectionID).offset().top
}, 1000)
})
}

html,
body,
.capa {
height: 100%;
overflow: auto;
width: 100%;
}
body {
background-attachment: fixed;
font-family: Helvetica, Arial, sans-serif;
overflow-x: hidden;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-fixed-top navbar-inverse navbar-transparente ">
<div class="container">
<!-- header -->
<div class="navbar-header">
<!-- botao toggle -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#barra-navegacao">
<span class="sr-only">alternar navegação</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#conteudo-sobre" class="js-scroll-trigger">
<img src="logopng.png" width="100" height="30" style="margin-top: 10px">
</a>
</div>
<!-- navbar -->
<div class="collapse navbar-collapse" id="barra-navegacao">
<ul class="nav navbar-nav navbar-right">
<li><a id="home" href="#conteudo-home">Home</a></li>
<li><a id="sobre" href="#conteudo-sobre">Sobre</a></li>
<li><a id="servicos" href="#conteudo-servicos">Serviços</a></li>
<li><a id="contato" href="#conteudo-contato">Contato</a></li>
<li class="divisor" role="separator"></li>
<li><a id="msc" href="#conteudo-contato">Mande seu currículo</a></li>
</ul>
</div>
</div>
<!-- /container -->
</nav>
<!-- /nav -->
<div class="capa " id="conteudo-home">
<div class="texto-capa layer">
<img src="logopng.png" width="450" height="150">
<h4>
<hr align="center" width="100" size="1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pha</h4>
<a href="#conteudo-sobre" class="btn btn-custom btn-branco btn-lg">Saiba mais</a>
</div>
</div>
<section id="conteudo-sobre">
<div class="container">
<div class="row text-center">
<div style="border:1px solid red">
<h2>Sobre nós<hr align="center" width="70%" size="1"></h2>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
</div>
</div>
</section>
<section id="conteudo-servicos">
<div class="container">
<div class="row text-center">
<div style="border:1px solid red">
<h2>Serviços<hr align="center" width="70%" size="1"></h2>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
</div>
</div>
</section>
<section id="conteudo-contato" >
<div class="container">
<div class="row text-center">
<div style="border:1px solid red">
<h2>Serviços<hr align="center" width="70%" size="1"></h2>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
<h3>Lorem ipsum </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae pharetra mi. Quisque sit amet mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nibh felis, ultricies vitae nisi in, euismod accumsan diam. Proin congue tempor pharetra. Vestibulum gravida sed velit non efficitur. Vivamus blandit, neque id dignissim ultrices, sapien augue vestibulum velit, a sollicitudin justo dolor ut elit. Donec ultricies euismod mi, id venenatis justo porttitor vitae. Duis elit nisi, faucibus eu velit non, suscipit pellentesque lacus.</p>
</div>
</div>
</section>
&#13;
答案 0 :(得分:0)
非常有趣的行为......我花了一段时间才弄清楚,但罪魁祸首是css ..删除css你给它的高度和宽度100%,它工作正常。
<style>
body {
background-attachment: fixed;
font-family: Helvetica, Arial, sans-serif;
overflow-x: hidden;
}
</style>
$(document).ready(function() {
$("nav").find("a").click(function(e) {
e.preventDefault();
var section = $(this).attr("href");
$('html, body').animate({
scrollTop: $(section).offset().top
}, 2000);
});
});