简体   繁体   English

为什么Jquery .show()在bootstrap手风琴折叠中不显示锚点div?

[英]Why won't Jquery .show() display anchor div within bootstrap accordion collapse?

I am fairly new to web dev. 我是Web开发人员的新手。 I've spent several days working on this (embarrassing) and researched everything I can think of. 我花了几天的时间对此进行了研究(尴尬),并研究了我能想到的一切。

I'm using Rails 5, bootstrap 3 and jquery 2.1.3. 我正在使用Rails 5,bootstrap 3和jquery 2.1.3。

I have a bootstrap accordion. 我有一个手风琴。 Within the various collapse content divs I want links to content that resides in the same accordion but a different collapse panel. 在各种折叠内容div中,我希望链接到位于相同手风琴但位于不同折叠面板中的内容。

In the current collapse panel I have a link like this: 在当前的合拢面板中,我有一个像这样的链接:

<a class="self-link" href="#buried_sub_anchor">Buried Sub Anchor</a>

In a different collapse panel I have a content div like this: 在另一个折叠面板中,我有一个像这样的内容div:

<div class="anchor" id="buried_sub_anchor">

My jquery code to handle the click is: 我的jQuery代码来处理单击是:

  $('.self-link').on('click', function() {expandCollapseWithHash(this.hash); });

  function expandCollapseWithHash(hash) {
    var $collapses = $(hash).parents('.collapse');
    if ($collapses.length > 0) {
      var $collapse = $collapses.first()
      $collapse.collapse('show');
    }
  }

When the .collapse('show') is called I'm expecting bootstrap to magically close the current panel and open the target. 当调用.collapse('show')时,我期望引导程序神奇地关闭当前面板并打开目标。 Then, once that transition is done I'm expecting the 'shown' event to fire, which I handle like this: 然后,一旦完成转换,我就希望触发“显示”事件,我将这样处理:

  $('.collapse').on('shown.bs.collapse', function() {
    if (location.hash) {
      $(location.hash).show();
    }
  });

I'm expecting the .show() call to jump the page right to the anchored div. 我期望.show()调用将页面直接跳转到锚定div。 But no dice. 但是没有骰子。

To summarize, when I click on the link I want: 总而言之,当我单击所需链接时:

  1. The current panel to .collapse('hide') .collapse('hide')的当前面板
  2. The target panel to .collapse('show') .collapse('show')的目标面板
  3. The page to jump to the anchored div in the target panel 跳转到目标面板中锚定div的页面

Instead, always: 相反,始终:

  1. The current panel doesn't change (ie it stays shown) 当前面板不变(即保持显示状态)
  2. The target panel does show 目标面板确实显示
  3. The page jumps to a new location but nowhere near the desired anchor div section 该页面跳转到新位置,但不在所需的锚点div部分附近

My questions are: 我的问题是:

  1. Why doesn't the current panel collapse? 为什么当前面板没有折叠? I would expect the bootstrap accordion functionality to do this as a result of the $collapse.collapse('show') call. 我希望$collapse.collapse('show')调用的结果是引导手风琴功能能够做到这一点。
  2. Why doesn't the page scroll to the linked content? 页面为什么不滚动到链接的内容?

Here is a fiddle . 这是一个小提琴 To reproduce: 复制:

  1. click on the "More Details" section 点击“更多详细信息”部分
  2. scroll all the way down 一直向下滚动
  3. click the "Buried sub anchor" link 点击“埋入子锚”链接

You have to force a scrollTop to do this. 您必须强制使用scrollTop来执行此操作。
I like to make it within an animate() , which makes it smoother. 我喜欢在animate()中制作它,使它更平滑。

Here is the only changes I've made to your Fiddle: 这是我对小提琴所做的唯一更改:

$('.collapse').on('shown.bs.collapse', function() {
    if (location.hash) {
        $(location.hash).show();
    }

    var thisId = $(this).attr("id");
    var thisOffsetTop = $("#" + thisId).offset().top;
    var headerHeight = $(".navbar").height();
    var myTitleHeight = $(".container h1").first().height();

    console.log(thisId);
    console.log("thisOffsetTop: " + thisOffsetTop);
    console.log("headerHeight: " + headerHeight);
    console.log("MyTitleHeight: " + myTitleHeight);

    // Animation to scrollTo the right position
    $('html, body').animate({
        scrollTop: thisOffsetTop - headerHeight - myTitleHeight
    });
});

I left all relevant console.log()... 我离开了所有相关的console.log()...
See the updated Fiddle . 请参阅更新的Fiddle

Well, I stuck at it and finally have a solution. 好吧,我坚持下去,终于有了解决方案。 The only thing I was doing wrong was this line $(location.hash).show(); 我唯一做错的是这行$(location.hash).show(); needed to instead be location.href = location.hash; 需要改为location.href = location.hash; .

That makes it so it jumps to show the desired div at the very top of the page just below the header. 这样一来,它就会跳转到页面顶部紧靠标题下方显示所需的div。

Here's the updated fiddle . 这是更新的小提琴

Within, click the "More Details" panel header, then click the link within that panel (ie "Buried Sub Anchor"). 在其中,单击“更多详细信息”面板标题,然后单击该面板中的链接(即“埋入子锚”)。 That will expand the "Miscellaneous" panel header and jump the page right to the "Buried Sub Anchor text" div. 这将展开“其他”面板标题,并将页面右跳到“埋入子锚文本” div。

Along the journey I picked up something else of value. 在旅途中,我得到了一些有价值的东西。 If the above-mentioned code change was all I made (ie $(location.hash).show(); -> location.href = location.hash; ), when clicking on the link it would expand the target panel and jump to where you want to go. 如果上述代码更改是我所做的(即$(location.hash).show(); -> location.href = location.hash; ),则在单击链接时,它将展开目标面板并跳转到你想去哪里。 But it would also leave the panel expanded / shown that you just left as well. 但这也会使面板展开/显示您也刚刚离开。

The reason was not obvious to me and is fixed with the following code: 原因对我来说并不明显,并通过以下代码解决了:

$('#accordion .panel-collapse').collapse({ 
  parent: '#accordion', 
  toggle: false 
});

I found that code on this github page . 我在github页面上找到了该代码。 It initializes the individual accordion collapse elements because 它初始化各个手风琴折叠元素,因为

the data api...lazy instantiates, something which .collapse('show') doesn't take into account. 数据api ...惰性实例化,而.collapse('show')则没有考虑。

So now in the updated fiddle when you click the link it closes the leaving panel, opens the target panel and jumps the page right to the target div, just the way I wanted. 因此,现在在更新的小提琴中,当您单击链接时,它会关闭离开面板,打开目标面板并将页面直接跳到目标div,这正是我想要的方式。

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

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