0

我有一组产品类别。每个类别都有一些产品图片。我创建了一个父 div,它有一个溢出:隐藏的 CSS 值。我想实现鼠标悬停效果。一旦您将鼠标悬停在我想显示相关类别产品图像的类别上。我尝试了很多但仍然没有得到完美的解决方案这是一个小片段:这是我的Fiddle Demo

<div class="menu" id="1" style="width:200px;">
                <a href="#">category 1</a>
            </div>
<div  class="flyout hidden" id="12">&nbsp;
<div class="list">
  <ul>
    <li><span class="box">1</span></li>
    <li><span class="box">2</span></li>
    <li><span class="box">3</span></li>
    <li><span class="box">4</span></li>
    <li><span class="box">5</span></li>
    <li><span class="box">6</span></li>
    <li><span class="box">7</span></li>
    <li><span class="box">8</span></li>
    <li><span class="box">9</span></li>
  </ul>
</div>
<button class="prev">prev</button>
<button class="more">more</button>
</div><br><br>

<div class="menu" id="2" style="width:200px;">
                <a href="#">category 2</a>
            </div>
<div class="flyout hidden" id="11" >&nbsp;
<div class="list">
  <ul>
    <li><span class="box">11</span></li>
    <li><span class="box">12</span></li>
    <li><span class="box">13</span></li>
    <li><span class="box">14</span></li>
    <li><span class="box">15</span></li>
    <li><span class="box">16</span></li>
    <li><span class="box">17</span></li>
    <li><span class="box">18</span></li>
    <li><span class="box">19</span></li>
  </ul>
</div>
<button class="prev">prev</button>
<button class="more">more</button>
</div>

//脚本

$(".more").click(function(){

  $(".list").animate({scrollLeft: "+=330"}, 300, "swing");
});

$(".prev").click(function(){
  $(".list").animate({scrollLeft: "-=330"}, 300, "swing");
});
$(".menu").hover(function(){
    var gal_id =$(this).next().attr('id');
    //alert(gal_id);
    var id='#'+gal_id;

    $(id).removeClass('hidden');
},function(){
    $(id).addClass('hidden');
});

//CSS

    body {
  margin: 5px;
}
.list {
  overflow: hidden;
  height:110px;
 width: 353px;
 margin-left: -38px;
}
ul {
  overflow: hidden;
  width:2000px;
}
li {
  float: left;
}
.box {
  width: 100px;
  height: 100px;
  background: #33cccc;
  margin: 5px;
  display: block;
}

.flyout{
position:absolute;
width:320px;
height:250px;
    margin-left:20px;
background:red;
overflow: hidden;
z-index:10000;

}

.hidden{
visibility: hidden;

}
4

2 回答 2

4

您不必使用id选择器,因为$(this).next()引用的是目标元素。

在您的代码中,问题是该变量id是 mouseenter 回调的本地变量,它在 mouseleve 回调中未定义

$(".menu").hover(function () {
    var $next = $(this).next();
    $next.removeClass('hidden');
    clearTimeout($next.data('hovertimer'));
}, function () {
    var $next = $(this).next(), timer;
    timer = setTimeout(function(){
        $next.addClass('hidden');
    }, 200);
    $next.data('hovertimer', timer);
});

$('.flyout').hover(function () {
    clearTimeout($(this).data('hovertimer'));
}, function () {
    $(this).addClass('hidden');
})

演示:小提琴

于 2013-10-14T10:38:04.900 回答
2

你也可以用 css 做到这一点:

HTML:

<div class="menu" id="1" style="width:200px;"> <a href="#">category 1</a>
  <div  class="flyout" id="12">&nbsp;
    <div class="list">
      <ul>
        <li><span class="box">1</span></li>
        <li><span class="box">2</span></li>
        <li><span class="box">3</span></li>
        <li><span class="box">4</span></li>
        <li><span class="box">5</span></li>
        <li><span class="box">6</span></li>
        <li><span class="box">7</span></li>
        <li><span class="box">8</span></li>
        <li><span class="box">9</span></li>
      </ul>
    </div>
    <button class="prev">prev</button>
    <button class="more">more</button>
  </div>
</div>
<br>
</br>
<div class="menu" id="2" style="width:200px;"> <a href="#">category 2</a>
  <div class="flyout" id="11" >&nbsp;
    <div class="list">
      <ul>
        <li><span class="box">11</span></li>
        <li><span class="box">12</span></li>
        <li><span class="box">13</span></li>
        <li><span class="box">14</span></li>
        <li><span class="box">15</span></li>
        <li><span class="box">16</span></li>
        <li><span class="box">17</span></li>
        <li><span class="box">18</span></li>
        <li><span class="box">19</span></li>
      </ul>
    </div>
    <button class="prev">prev</button>
    <button class="more">more</button>
  </div>
</div>

CSS:

body {
    margin: 5px;
}
.list {
    overflow: hidden;
    height:110px;
    width: 353px;
    margin-left: -38px;
}
ul {
    overflow: hidden;
    width:2000px;
}
li {
    float: left;
}
.box {
    width: 100px;
    height: 100px;
    background: #33cccc;
    margin: 5px;
    display: block;
}
.flyout {
    position:absolute;
    width:320px;
    height:250px;
    margin-left:20px;
    background:red;
    overflow: hidden;
    display:none;
    z-index:10000;
}
.hidden {
    visibility: hidden;
}
div.menu:hover div.flyout {
    display:block;
}

脚本:

$(".more").click(function(){

  $(".list").animate({scrollLeft: "+=330"}, 300, "swing");
});

$(".prev").click(function(){
  $(".list").animate({scrollLeft: "-=330"}, 300, "swing");
});

jsFiddle 在这里

于 2013-10-14T10:44:46.383 回答