[英]Scroll to the top of the page in the Bootstrap tab
I create a tab with bootstrap 4 like this:我使用 bootstrap 4 创建一个标签,如下所示:
<section class="main-section lightblue " id="wstabs">
<a class="anchor" id="description"></a>
<div class="bg-dark nav-tabs fixed-top" id="wstabs2" data-toggle="description">
<div class="container">
<ul class="nav tabs-marker tabs-dark bg-dark" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="text" data-toggle="tab" href="#text-tab" role="tab" aria-controls="text" aria-selected="true">tab1<span class="marker"></span></a>
</li>
<li class="nav-item">
<a class="nav-link" id="feature" data-toggle="tab" href="#feature-tab" role="tab" aria-controls="feature" aria-selected="false">tab2<span class="marker"></span></a>
</li>
</ul>
</div>
</div>
<div class="container" >
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="text-tab" role="tabpanel" aria-labelledby="text-tab">
1
<div class="tab-pane fade" id="feature-tab" role="tabpanel" aria-labelledby="feature-tab">
2
</div>
</div>
</div>
</section>
It works.有用。
Now I fixed the header tab to top.现在我将 header 选项卡固定到顶部。
But I have a problem.但我有一个问题。
For example, when I scroll down and click on tab2, it works but I also want it scrolls to top in this line: (the begin of tabs)例如,当我向下滚动并单击 tab2 时,它可以工作,但我也希望它滚动到这一行的顶部:(选项卡的开头)
<a class="anchor" id="description"></a>
How can do that?怎么能这样做? I can not define two anchors
href
links for <a>
tag.我无法为
<a>
标签定义两个锚点href
链接。
You could attach event listeners to each of your html elements which have a class of nav link and then trigger a scroll up function to bring the view to the top您可以将事件侦听器附加到每个 html 元素上
document.querySelectorAll('.nav-link').forEach(linkItem => {
linkItem.addEventListener('click', _ => {
window.scrollTo(0, 0)
})
})
Here is a fiddle proof: https://jsfiddle.net/jgLewspb/这是一个小提琴证明: https://jsfiddle.net/jgLewspb/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.