[英]jQuery hover fade p tag out and fade another in
所以我在使它起作用方面遇到問題。 我有3個ID為#mod1,#mod2,#mod3的模塊。 當您將鼠標懸停在這些標簽上時,我希望它們淡出可見的P標簽,然后淡入另一個標簽。
<ul id="homeModules">
<li id="mod1"><a href="/portfolio/">VIEW OUR GALLERY</a></li>
<li id="mod2"><a href="/about/">MEET SARAH</a></li>
<li id="mod3"><a href="/become-a-client/">BECOME A CLIENT</a></li>
</ul>
<p class="homeTags" id="homeTag1">Lorem ipsum dolor sit amet.</p>
<p class="homeTags" id="homeTag2">The Google Fonts API will.</p>
<p class="homeTags" id="homeTag3">Check out more advanced techniques.</p>
#homeTag2, #homeTag3 {
display: none;
}
$('#mod1').hover(function(){
$('#homeTag2,#homeTag3').fadeOut(250);
setTimeout(function(){
$('#homeTag1').fadeIn(250);
}, 500);
});
$('#mod2').hover(function(){
$('#homeTag1,#homeTag3').fadeOut(250);
setTimeout(function(){
$('#homeTag2').fadeIn(250);
}, 500);
});
$('#mod3').hover(function(){
$('#homeTag1,#homeTag2').fadeOut(250);
setTimeout(function(){
$('#homeTag3').fadeIn(250);
}, 500);
});
您太復雜了,可以改用這種方法:
HTML-從li的ID中刪除了ID
<ul id="homeModules">
<li data-target="#homeTag1"><a href="/portfolio/">VIEW OUR GALLERY</a></li>
<li data-target="#homeTag2"><a href="/about/">MEET SARAH</a></li>
<li data-target="#homeTag3"><a href="/become-a-client/">BECOME A CLIENT</a></li>
</ul>
JS
var $homeTags = $('.homeTags');
$homeTags.filter(':first').show(); //Show the first one
$('#homeModules > li').hover(function(){
var $target = $($(this).data('target')); //Get the target reading from data attribute of the hovered li
$target.stop(true, true).fadeIn(250, function(){ //fadeIn the target and on completion of this
$homeTags.filter(':visible').not($target).fadeOut(250); //fadeOut the others
})
});
您也可以利用當前HTML的索引和索引(而不使用任何ID或數據目標)來進行這種方式,但是之前的更為明確。
$('.homeTags:first').show();
$('#homeModules > li').hover(function(){
var $target = $('.homeTags').eq($(this).index())
$target.fadeIn(250, function(){
$('.homeTags:visible').not($target).fadeOut(250);
});
});
的HTML
<ul id="homeModules">
<li id="mod1"><a href="/portfolio/">VIEW OUR GALLERY</a></li>
<li id="mod2"><a href="/about/">MEET SARAH</a></li>
<li id="mod3"><a href="/become-a-client/">BECOME A CLIENT</a></li>
</ul>
<div class="homeTags" id="homeTag1">
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="homeTags" id="homeTag2">
<p>The Google Fonts API will.</p>
</div>
<div class="homeTags" id="homeTag3">
<p>Check out more advanced techniques.</p>
</div>
的CSS
#homeTag2, #homeTag3 {
display: none;
}
JQUERY
$("#homeModules li").on("mouseenter", function () {
var whichLink = $(this).attr("id").slice(-1);
if ($("#homeTag" + whichLink).css("display") == "block") {
return false;
} else {
$(".homeTags").hide();
$("#homeTag" + whichLink).show();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.