简体   繁体   English

显示标签后滚动顶部

[英]Scroll top after showing a tab

I have Bootstrap tabs on my page, but when I click on it, the tabs are hidden. 我的页面上有Bootstrap选项卡,但是当我单击它时,这些选项卡被隐藏了。 Is there a way to scroll the position up so the tabs can be seen? 有没有一种方法可以向上滚动位置以便可以看到选项卡? Nothing I have found helped me. 我找不到任何帮助。 I can't change positions of the tabs or use hidden anchors - the tabs are the anchor. 我无法更改标签的位置或使用隐藏的锚点-标签是锚点。

<ul class="nav nav-tabs" role="tablist">
    <li class="active bio" role="presentation"><a href="#bio" role="tab" data-toggle="tab">biografie</a></li>
    <li class="films hidden-sm hidden-md hidden-lg" role="presentation"><a href="#films" aria-controls="films" role="tab" data-toggle="tab">filmografie</a></li>
    <li class="trivia hidden-xs" role="presentation"><a href="#trivia" role="tab" data-toggle="tab">zajímavosti</a></li>
    <li class="awards hidden-xs" role="presentation"><a href="#awards" role="tab" data-toggle="tab">ocenění</a></li>
    <li class="videos hidden-xs" role="presentation"><a href="#videos" role="tab" data-toggle="tab">videa</a></li>
    <li class="gallery hidden-xs" role="presentation"><a href="#gallery" role="tab" data-toggle="tab">galerie</a></li>
    <li class="contact hidden-xs" role="presentation"><a href="#contact" role="tab" data-toggle="tab">kontakt</a></li>
</ul>

This is the JS I use to get to the anchors: 这是我用来定位锚点的JS:

  $("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) {
    var id = $(e.target).attr("href").substr(1);
    window.location.hash = id;
  });

  var hash = window.location.hash;
  $('.nav-tabs a[href="' + hash + '"]').tab('show');

Page with tabs: http://testovani-frameworku.wz.cz/jennifer-lawrence.html#awards 带有标签的页面: http : //testovani-frameworku.wz.cz/jennifer-lawrence.html#awards

If you want to go to the very top, you could do it a couple ways 如果您想走到最顶端,可以采取几种方法

window.scrollTo(0, 0);

or 要么

$("mySelector").animate({ scrollTop: 0 }, "fast");

or 要么

$(window).scrollTop(0);

It just depends what type of effect you want. 这仅取决于您想要哪种效果。 Could just set the scrollTop to 0 (which is very top) if you don't want animation effect. 如果您不希望动画效果,可以将scrollTop设置为0(非常高)。 Could just use the vanilla JS window.scrollTo(x,y) 可以只使用普通的JS window.scrollTo(x,y)

Hope this helps! 希望这可以帮助!

So this is how I've solved it. 所以这就是我解决的方法。 I've added "-tab" at the end of a tab's ID and the original ID has the link. 我在选项卡ID的末尾添加了“ -tab”,原始ID具有链接。

$("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) {
    var id = $(e.target).attr("href").substr(1);
    id = id.substring(0, id.length-4);
    window.location.hash = id;    
});

var hash = (window.location.hash);
$('.nav-tabs a[href="' + hash + '-tab"]').tab('show');   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM