【问题标题】:Show/hide divs with data attribute on hover在悬停时显示/隐藏具有数据属性的 div
【发布时间】:2014-02-09 22:09:00
【问题描述】:

我正在使用 jQuery Isotope 插件并试图实现这一点:当用户将鼠标悬停在特定网格上时,该网格中的内容会发生变化。

这是我目前所拥有的:http://jsfiddle.net/wD64G/12/

HTML 代码:

<div class="item" data-target="firstbox">
   <div class="visible" >
       I should disappear upon hover.
   </div>
   <div id="firstbox" class="padding not-visible">
       Hello, I should only appear when you hover over the grid.</div>
   </div>

<div class="item" data-target="secondbox">
   <div class="visible" >
        I should disappear upon hover.
   </div>
   <div id="secondbox" class="not-visible">
       Hello, I should only appear when you hover over the grid.</div>           
   </div>
</div>

JS:

$('.not-visible').hide();

$( ".item" ).mouseover(function() {
    var target = "#" + $(this).data("target");
    $(".not-visible").not(target).hide();
    $(".visible").hide();
    $(target).show();
});

$( ".item" ).mouseout(function() {
    var target = "#" + $(this).data("target");
    $(target).hide();
    $(".visible").show();
});

这里的问题是所有 div 都将被赋予“可见”类,因此将根据我拥有的代码隐藏。我对 jQuery 很陌生,我很难仅引用特定的 div。我是否必须使用另一个数据属性来引用它?提前致谢!

【问题讨论】:

    标签: jquery jquery-isotope custom-data-attribute


    【解决方案1】:

    你需要定位悬停的.item的内容

    $('.not-visible').hide();
    
    $(".item").hover(function () {
        $(this).find('.visible').hide();
        $(this).find('.not-visible').show();
    }, function () {
        $(this).find('.visible').show();
        $(this).find('.not-visible').hide();
    });
    

    演示:Fiddle

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2014-06-12
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多