简体   繁体   English

Bootstrap Collapse Accordion Hover 不一致

[英]Bootstrap Collapse Accordion Hover is not consistent

I'm working on creating a accordion that collapses/opens when the user hover's their mouse over the title of the accordion.我正在创建一个当用户将鼠标悬停在手风琴标题上时折叠/打开的手风琴。 The code I have so far works to some degree.到目前为止,我的代码在某种程度上有效。 The problem is that the accordion always opens when the mouse enters but is sometimes really inconsistent in closing (especially if the user moves their mouse very fast).问题是当鼠标进入时手风琴总是打开但有时在关闭时确实不一致(特别是如果用户移动鼠标非常快)。

Here is a link to the website http://infotree.co.uk/ (the accordion is on the left) to visualize the problem - move mouse fast over the left accordion.这是网站http://infotree.co.uk/的链接(手风琴在左侧)以可视化问题 - 在左侧手风琴上快速移动鼠标。

And here is my code for just one of the accordion tabs in the html doc:这是我的 html 文档中手风琴选项卡之一的代码:

<div class="panel panel-default">
        <div class="panel-heading" role="tab">
          <h4 class="panel-title accordionTitles1" id="headOne1"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne1">Search</a></h4>
        </div>
        <div id="collapseOne1" class="panel-collapse collapse in">
          <div class="panel-body">Search to find specific content to learn about.</div>
        </div>
</div>

And the java script to go with it:以及与之配套的java脚本:

$(document).ready(function() {
 $("#headOne1").hover(function() {
    $('#collapseOne1').collapse('show');
 }, function() {
    $('#collapseOne1').collapse('hide');
 }
 );
});   

This question has been answered before: Bootstrap Collapse accordion on hover这个问题之前已经回答过: Bootstrap 折叠手风琴悬停

If you don't want/need the fancy animation, you could also use pure CSS: https://jsfiddle.net/vvu5ozh1/4/ With CSS transitions you could even do the animation, but that would be a bit more complicated.如果你不想要/不需要花哨的动画,你也可以使用纯 CSS: https : //jsfiddle.net/vvu5ozh1/4/使用 CSS 过渡你甚至可以做动画,但这会更复杂一些。

<div class="panel">
<div class="title">
Title1
</div>
<div class="content">
COntent1
</div>
</div>

<div class="panel">
<div class="title">
Title2
</div>
<div class="content">
COntent2
</div>
</div>

.panel:hover .content {
  display:block;
}

.content {
  display: none;
}

Going through the previous accordion question you mentioned ( Bootstrap Collapse accordion on hover ) I found one person's answer relating to the problem I was having which made me realize the exact cause.通过您提到的上一个手风琴问题( 悬停时 Bootstrap 折叠手风琴),我找到了一个与我遇到的问题相关的答案,这让我意识到了确切原因。 The problem is to do with the animation timing, so if you leave the collapse area BEFORE the collapse animation is finish jquery never runs the collapse function.问题与动画时间有关,因此如果在折叠动画完成之前离开折叠区域,jquery 永远不会运行折叠功能。 The solution is to use queue and dequeue methods to make sure all the functions run properly and in the correct order.解决方案是使用 queue 和 dequeue 方法来确保所有功能正常运行并以正确的顺序运行。

Here is the HTML code for one tab:这是一个选项卡的 HTML 代码:

<div class="panel panel-default">
      <a href="http://www.google.com" class="sidebarButtons" id="sidebarButton1">Search</a>
      <div id="sidebarContent1" class="panel-collapse collapse">
        <div class="panel-body">Search to find specific content to learn about.</div>
      </div>
    </div>

And the java script for the respective tab:以及相应选项卡的 java 脚本:

$(document).ready(function() {
var button1 = $("#sidebarButton1");
var content1 = $("#sidebarContent1");
button1.mouseenter(function() {     
    content1.queue('collapsequeue',function(){
        content1.collapse('show');
    });
    if (!content1.hasClass("collapsing")) {
        content1.dequeue("collapsequeue");
    }
});
button1.mouseleave(function() { 
    content1.queue('collapsequeue',function(){
        content1.collapse('hide');
    });
    if (!content1.hasClass("collapsing")) {
        content1.dequeue("collapsequeue");
    }
});
content1.on("shown.bs.collapse hidden.bs.collapse", function(){
    content1.dequeue("collapsequeue");
});
});

The .queue() names a queue AS WELL as adds functions to a queue, .dequeue() simply RUNS the queue. .queue() 命名队列以及向队列添加函数,.dequeue() 只是运行队列。 The code isn't completely perfect as it goes against DRY coding (much like the response I found in Bootstrap Collapse accordion on hover ) - this is because I am not able to use the href tag in a element since I need that so that I can link to different webpages rather than the div element containing the hidden content.该代码并不完全完美,因为它与 DRY 编码背道而驰(很像我在悬停时 Bootstrap Collapse 手风琴中找到的响应) - 这是因为我无法在元素中使用 href 标签,因为我需要这样我可以链接到不同的网页,而不是包含隐藏内容的 div 元素。

Any idea on making the code shorter/efficient?关于使代码更短/高效的任何想法? I have to repeat the JS for every tab and I feel like there is probably a better way to do this that what I have come up with.我必须为每个选项卡重复 JS,我觉得可能有比我想出的更好的方法来做到这一点。

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

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