列表悬停显示图像jquery

时间:2016-10-24 13:09:43

标签: jquery mouseover

我需要做的是当列表项鼠标悬停改变图像(其他div)具有相同的索引时,它必须在单击时保持不变。当鼠标离开它必须是点击之前或哪个活动。我做了一些事但没有工作。

<div class="all-wrap">
    <div class="left-side">
        <ul>
            <li>Tittle 1</li>
            <li>Tittle 2</li>
            <li>Tittle 3</li>
            <li>Tittle 4</li>
            <li>Tittle 5</li>
        </ul>
    </div>
    <div class="right-side">
        <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg" class="active">
        <img src="http://blog.nateeul.com/wp-content/uploads/2011/01/Lorem-Ipsum3-01-EDITED.jpg">
        <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg">
        <img src="http://blog.nateeul.com/wp-content/uploads/2011/01/Lorem-Ipsum3-01-EDITED.jpg">
        <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg">
    </div>
 </div>

$('.left-side ul li').hover(
function() {
    $('.right-side img').eq($(this).index()).addClass("active");
      $(this).click(funciton(){
       $('.right-side img').eq($(this).index()).addClass("active");
      };
}, function() {
    $('.right-side img').eq($(this).index()).removeClass("active");
});

https://jsfiddle.net/tolga748/xju1hj9x/

3 个答案:

答案 0 :(得分:1)

$(function(){
    var index = 0;
    $('.left-side ul li').hover(function() {
        $('.right-side img').eq($(this).index()).addClass("active");
        $('.right-side img').eq($(this).index()).siblings("img").removeClass("active");
    }, function() {
        $('.right-side img').eq($(this).index()).removeClass("active");
        $('.right-side img').eq(index).addClass("active");
    });

    $('.left-side ul li').click(function(){
        index = $(this).index();
        var element = $('.right-side img').eq($(this).index());
        element.addClass("active");
        element.siblings('img').removeClass("active");
    });
});

用这个替换你的jquery。你很高兴:)

答案 1 :(得分:1)

请查看下面的代码段。

$(".left-side ul li").hover(function(){
  if(!$('.right-side img').eq($(this).index()).hasClass('not-remove')){
    $('.right-side img').eq($(this).index()).addClass("active");  
  }
},function(){
  if(!$('.right-side img').eq($(this).index()).hasClass('not-remove')){
    $('.right-side img').eq($(this).index()).removeClass("active");
  }
});

$('.left-side ul li').click(function() {
  $('.right-side img').eq($(this).index()).addClass("not-remove").addClass("active");
});
.all-wrap
{
    width: 100%;
    position: absolute;
    float: left;
}
.left-side
{
    width: 50%;
    float: left;
    position: relative;
}
.left-side ul li
{
    cursor: pointer;
    margin-bottom:10px;
}
.left-side ul li:hover
{
  color:red;
}
.right-side
{
    width: 50%;
    float: left;
    position: relative;
}
.right-side img
{
    width: 100%;
    float: left;
    display:none;
}
.right-side img.active
{
  display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="all-wrap">
  <div class="left-side">
    <ul>
      <li>Tittle 1</li>
      <li>Tittle 2</li>
      <li>Tittle 3</li>
      <li>Tittle 4</li>
      <li>Tittle 5</li>
    </ul>
  </div>
  <div class="right-side">
    <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg" class="active">
    <img src="http://blog.nateeul.com/wp-content/uploads/2011/01/Lorem-Ipsum3-01-EDITED.jpg">
    <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg">
    <img src="http://blog.nateeul.com/wp-content/uploads/2011/01/Lorem-Ipsum3-01-EDITED.jpg">
    <img src="http://blog.dominictrumfio.com/wp-content/uploads/2015/08/Lorem-Ipsum-2.jpg">
  </div>
</div>

答案 2 :(得分:0)

您可以这样做:

https://jsfiddle.net/tejashsoni111/xju1hj9x/1/

$('.left-side ul li').hover(function(){
    $('.right-side img').removeClass('active');
    var index = $(this).index();
    $('.right-side img').eq(index).addClass("active");
});