简体   繁体   English

单击引导导航选项卡不会滚动到选项卡窗格 - jQuery / Bootstrap

[英]Clicking a Bootstrap Nav Tab won't scroll to Tab Pane - jQuery / Bootstrap

I have some Bootstrap vertical tabs that I'm struggling with what I felt like would be a simple operation.我有一些 Bootstrap 垂直选项卡,我正在努力解决我觉得这是一个简单的操作。

Goal:目标:

On the click of Tab, I want to smooth-scroll to the corresponding pane.单击选项卡时,我想平滑滚动到相应的窗格。

What's Actually Happening:实际情况:

On click of the tab, the browser animates to the tab , not the tab pane.单击选项卡时,浏览器将动画到选项卡,而不是选项卡窗格。 I've tried passing in the paneId instead of the ID, but that doesn't work at all.我试过传入paneId而不是 ID,但这根本不起作用。 Any idea where I'm going wrong?知道我哪里出错了吗?

Codepen:代码笔:

Here's a Codepen: Codepen!这是一个 Codepen: Codepen!

jQuery: jQuery:

$("#tabs .nav-link").click(function (e) { //on click of nav tav, perform this:
    var id = $(e.target).prop("id"); //Grab ID of .nav-link clicked
    var paneId = id.replace("tab", "pane"); //Replace "tab" with "pane" and assign new var

    function navigateToElement(id) {
        $("html, body").animate(
            {
                scrollTop: $("#" + id).offset().top
            },
            300
        );
    }
    navigateToElement(id); //Tried with "paneId" instead of "id" to scroll to the pane, but doesn't work
});

Any idea where I'm going wrong?知道我哪里出错了吗?

This line:这一行:

var paneId = id.replace("tab", "pane"); //Replace "tab" with "pane" and assign new var

Is creating a variable like: v-pills-profile-pane and there isn't a div on the page with that id.正在创建一个变量,例如: v-pills-profile-pane并且页面上没有具有该 ID 的 div。 I did see a div with id="v-pills-profile" .我确实看到了一个id="v-pills-profile"的 div。 Change that last line to this and it should work:将最后一行更改为此,它应该可以工作:

var paneId = id.replace("-tab", "");

I just realized that it doesn't matter what pane I scroll to, as bootstrap will display the panes under #panes .我刚刚意识到滚动到哪个窗格并不重要,因为引导程序将在#panes下显示窗格。 As long as I can scroll to that, I'm good.只要我可以滚动到那个,我就很好。 This code worked:此代码有效:

$("#tabs .nav-link").click(function (e) {
    var target = $("#panes");

    $([document.documentElement, document.body]).animate(
      {
        scrollTop: $(target).offset().top - 20, // added a bit of offset
      },
      350
    );
  });

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

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