简体   繁体   English

添加淡入/淡出 jQuery 悬停 addclass/remove

[英]add fade in/out jQuery hover addclass/remove

I have a list of menu items generated dynamically (wordpess menu).我有一个动态生成的菜单项列表(wordpess 菜单)。 I have the code set up now that depending on which menu item is hovered over, it adds a css class "Proj1, proj2, etc" to an image box ".show".我现在已经设置了代码,根据悬停在哪个菜单项上,它会向图像框“.show”添加一个 css 类“Proj1、proj2 等”。 the Proj1 Proj2 classes each have a different background image so effectively the image box switches the image depending on which menu item is hovered over. Proj1 Proj2 类每个都有不同的背景图像,因此图像框会根据悬停在哪个菜单项上有效地切换图像。 All good there.那里一切都很好。

What I can't seem to do is find a way to make the images fade in and out when being switched.我似乎无法做的是找到一种方法,使图像在切换时淡入淡出。

I've been pulling my hair out for weeks, please help me Obi-Wan Kenobi我已经拔头发好几个星期了,请帮帮我 Obi-Wan Kenobi

var image=["proj1","proj2","proj3","proj4","proj5","proj6","proj7","proj8","proj9","proj10","proj11","proj13","proj14","proj15","proj16","proj17","proj18","proj19","proj20","proj21","proj22","proj23","proj24","proj25","proj26","proj27","proj28","proj29","proj30","proj31","proj32" ];

$(".list-group li").hover(function(){
  var value=  $(this).index();
  hoverContent(value);
});
function hoverContent(value){
    $("#list-content div").removeClass('show');
    $("#list-content div:nth-child("+value+")").addClass("show");
    $("#bg").removeClass().fadeOut();
    $("#bg").addClass(image[value]).fadeIn();
}

html is using wordpress php html 正在使用 wordpress php

    <div id="bg" class="content">
        <div id="list-content">
           <div class="show"> </div>
        </div>
    </div>

    <div class="menuItems">
        <ul class="list-group">
            <li><a href="www.link.html">Menu Item 1</a></li>
            <li><a href="www.link.html">Menu Item 1</a></li>
            <li><a href="www.link.html">Menu Item 1</a></li>
            <li><a href="www.link.html">Menu Item 1</a></li>
            <li><a href="www.link.html">Menu Item 1</a></li>
        </ul>
    </div>  

Sounds like you want to use the callback on fadeOut to execute changes when they finish their animations.听起来您想在它们完成动画时使用fadeOut的回调来执行更改。

You can also avoid looking up array indices by storing extra information on your <li> elements in data-* attributes您还可以通过在data-*属性中存储<li>元素的额外信息来避免查找数组索引

<li data-proj="proj1"> <!-- 👈 note the data attribute and value -->
  <a href="http://www.link.html">Menu Item 1</a>
</li>
$(".list-group li").hover(function() {
  const li = $(this)
  const proj = li.data("proj")
  const index = li.index()

  $("#list-content div")
    .removeClass("show")
    .eq(index).addClass("show")

  $("#bg").fadeOut("fast", function() {
    $(this).removeClass().addClass(proj).fadeIn()
  })
}

@Shikkediel wrote an elegant solution: @Shikkediel 写了一个优雅的解决方案:

https://jsfiddle.net/n62bqpym https://jsfiddle.net/n62bqpym

var image = ['proj1', 'proj2', 'proj3', 'proj4', 'proj5'];

$('.list-group li').hover(function() {
  hoverContent($(this).index());
});

function hoverContent(value) {
  $('#list-content div').removeClass('show').eq(value).addClass('show');
  $('#bg').attr('class', image[value]);
}

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

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