【问题标题】:Hover function for accordion手风琴的悬停功能
【发布时间】:2010-04-15 18:19:35
【问题描述】:

到目前为止,你们一直在帮助我让这个小曲子正常工作。我还有一个要求:

这个标记:

          <div id="themes">
          <h2>Research Themes</h2>
            <ul>
              <li class="tier_1"><a class="enviro" href="">Learn about our approach to the <strong>environment</strong></a>
                <ul class="tier_2 hide">
                  <li><a href=""><em>How we are tying this all together</em></a></li> 
                  <li><a href="off.html"><strong>Project:</strong> Solor Powered Biofactories</a></li> 
                  <li><a href=""><strong>Project:</strong> Cleaning Water with Nature</a></li>
                  <li><a href=""><strong>Project:</strong> Higher Efficiency Solar Technology</a></li>
                </ul>
              </li>
              <li class="tier_1"><a class="health" href="">Learn about our approach to <strong>human health</strong></a>
                <ul class="tier_2 hide">
                  <li><a href="">Project name numero uno goes here</a></li> 
                  <li><a href="">Project name numero dos goes here</a></li>
                  <li><a href="">Project name numero tres goes here</a></li>
                </ul>
              </li>
              <li class="tier_1"><a class="defense" href="">Learn about our approach to <strong>national defense</strong></a>
                <ul class="tier_2 hide">
                  <li><a href="">Project name numero uno goes here</a></li> 
                  <li><a href="">Project name numero dos goes here</a></li>
                  <li><a href="">Project name numero tres goes here</a></li>
                </ul>
              </li>
            </ul>
          </div><!-- // end themes -->

还有这个 jQuery:

$(function(){
  $(".tier_1 > a").hover(function() {
    var currentList = jQuery(this).parents('li').find('.tier_2');
    $(currentList).slideToggle();
    jQuery(this).parents('ul').find('.tier_2').not(currentList).slideUp();
    return false;
  });
});

创建这个漂亮的“主题”滑块,您可以在此页面的右栏中看到:http://clients.pixelbleed.net/biodesign/

我有两个问题...当您点击 tier_2 ul 下的链接之一时,悬停会缩回上滑/下滑。当有人在嵌套的 li 盘旋时,我希望它保持滑出状态。所以幻灯片应该只发生在 tier_1 元素的悬停上。我还想在悬停时向 tier_1 链接上的 a 元素添加一个“活动”类。所以 [a class="enviro"..] 在悬停时会变成 [a class="enviro active"]。然后,当悬停其他 tier_1 项目之一时,将删除它。这样,当有人查看嵌套元素时,漂亮的颜色图标可以保持可见。

甚至不确定悬停所有可能的情况,但我想如果有人知道它会在这里的方式。

【问题讨论】:

  • 使用预制手风琴有什么问题? docs.jquery.com/UI/Accordion
  • @Justin - 这是个好主意,但有时您可能不想重新设置整个网站的主题以适应 jQuery UI,而只是想要类似的行为。
  • 使用 jQueryUI 不会强迫你使用他们的主题。您可以相应地设置手风琴的样式(没有双关语)。如果你不喜欢这样,那么你可以使用一个插件plugins.jquery.com/project/accordion

标签: jquery hover show-hide


【解决方案1】:

我认为您可能希望在主题 DIV 上有一个 mouseout 处理程序,它会向上滑动所有嵌套的 uls,并为每个 tier_1 锚点设置一个 mouseover 处理程序,以关闭其他嵌套的 uls 并滑动打开它嵌套 ul。这样,当您切换到不同的面板或完全退出它们的 div 时,您只会关闭面板。如果您希望主题 DIV 中的最后一个选择保持选中状态,则可以省略 mouseout

$(function(){
  $('div.themes').mouseout(function() {
       $('.tier_2:visible').slideUp();
       $(this).find('a.active').removeClass('active');
  });
  $(".tier_1 > a").mouseover(function() {
    var $this = $(this);
    $this.closest('div').find('a.active').not($this).removeClass('active');
    $this.addClass('active');
    var currentList = $this.parents('li').find('.tier_2'); 
    $(currentList).not(':visible').slideDown(); 
    $('.tier_2:visible').not(currentList).slideUp(); 
    return false; 
  }); 
});

【讨论】:

  • 您的解释听起来与我正在寻找的完全一样,但是这个 sn-p(仅在本地尝试过)与我正在使用的那个有相同的问题——不会让 tier_2 嵌套当我将它们悬停时可见的项目。有什么想法吗?
  • @Banderdash -- 需要将 slideToggle 替换为 slideDown,尽管 @Justin 关于使用手风琴的评论有其优点。
猜你喜欢
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2014-07-20
  • 2012-08-18
  • 1970-01-01
相关资源
最近更新 更多