我有一个下拉 anchor ,它从整页视差图像底部的向下箭头链接到页面上的另一个部分。抛 anchor :
<section id="first" class="big-image parallax lander" style="background-image: url("https://wagstaffsandbx.wpengine.com/wp-content/uploads/2017/06/Homepage-Photo-1.jpg"); background-position: 50% -7.2px;" data-stellar-background-ratio="0.2" data-stellar-vertical-offset="50">
<a href="#welcome" class="arrow"><img class="down-arrow" src="https://wagstaffsandbx.wpengine.com/wp-content/uploads/2017/06/down-arrow.png" alt="down-arrow"></a>
</section>
去向:
<section id="welcome" class="text-block"><h1>welcome</h1><p>Pitchfork mumblecore stumptown, intelligentsia wolf put a bird on it man bun wayfarers organic actually sartorial. Sriracha disrupt kickstarter fingerstache selvage pour-over. Paleo ugh lumbersexual, kinfolk banjo banh mi meditation cliche 3 wolf moon single-origin coffee viral blog polaroid pop-up.</p>
</section>
单击箭头有效,但会平滑滚动到放置 anchor ,它滚动到正确的位置,然后无缘无故向下移动。
这是我用来实现平滑滚动的 jQuery。你会看到有一个 140px 的导航偏移量:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Add smooth scrolling to all links
$("a.arrow").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top -140
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
</script>
您可以在此处查看实际问题:https://wagstaffsandbx.wpengine.com/test-stellar/
联合国看网站:demo PW看网站:密码
有什么想法吗?
最佳答案
你的问题是这样引起的:
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
以这种方式改变location
会触发浏览器的相应 react 。
建议用以下内容替换它:
if (typeof (history.pushState) !== 'undefined') {
var obj = {title: $('head title').text(), URL: hash};
history.pushState(obj, obj.title, obj.URL);
}
关于javascript - 跳过/跳跃抛 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44465720/