我试图通过添加 $opened = false;
并进一步跟进来更改此脚本。目的是一次只允许打开一个.textwrap
div,因此当您单击另一个 div 时,第一个会淡出,第二个会淡入。然而,我所做的只允许.textwrap
单击打开一个,然后我必须单击它关闭再次点击后才能打开下一个.textwrap
。我尝试交换if ( $opened == false )
到else ( $opened == false )
,这完全改变了脚本。在此不胜感激。谢谢
jQuery:
<script>
$opened = false;
$('.smallwrap').each(function(){
var text = $(this).find('.textwrap'),
pic = $(this).find('.picwrap'),
clicked = false;
$(this).hover(function(){
$(text).stop().fadeIn(200);
}, function(){
if ( clicked == false ) {
$(text).stop().fadeOut(150);
} else {
// do nothing
}
});
$(this).on('click', function(){
if ( clicked == false ) {
if ( $opened == false ) {
$(text).show();
clicked = true;
$opened = true;
}
} else {
$(text).stop().fadeOut(150, function(){
clicked = false;
$opened = false;
});
}
});
});
</script>
HTML:
<div id="infowrap">
<div class="mainwrap">
<div class="smallwrap">
<div class="picwrap"><a href="#"><img class="pic" alt="pic1" src="img/pic1.jpg"></a></div>
<div class="textwrap"><p>Mustache art party pug whatever mixtape, pork belly sriracha gentrify swag. Try-hard selvage butcher high life, hashtag DIY ennui.</p></div>
</div>
<div class="smallwrap">
<div class="picwrap"><a href="#"><img class="pic" alt="pic1" src="img/pic1.jpg"></a></div>
<div class="textwrap"><p>Mustache art party pug whatever mixtape, pork belly sriracha gentrify swag. Try-hard selvage butcher high life, hashtag DIY ennui.</p></div>
</div>
<div class="smallwrap">
<div class="picwrap"><a href="#"><img class="pic" alt="pic1" src="img/pic1.jpg"></a></div>
<div class="textwrap"><p>Mustache art party pug whatever mixtape, pork belly sriracha gentrify swag. Try-hard selvage butcher high life, hashtag DIY ennui.</p></div>
</div>
<div class="smallwrap">
<div class="picwrap"><a href="#"><img class="pic" alt="pic1" src="img/pic1.jpg"></a></div>
<div class="textwrap"><p>Mustache art party pug whatever mixtape, pork belly sriracha gentrify swag. Try-hard selvage butcher high life, hashtag DIY ennui.</p></div>
</div>
</div>
</div>
CSS:
#infowrap {
background: rgba(255,255,255,0.96);
z-index: 900;
display: none;
position: fixed;
top:10px;left:10px;right:10px;bottom:10px;
vertical-align: center;
}
.mainwrap {
width: 540px;
height: 540px;
margin: 50px auto 0 auto;
}
.smallwrap {
width: 250px;
height: 250px;
margin: 10px;
float: left;
position: relative;
}
.picwrap {
width: 250px;
height: 250px;
}
.pic {
width: 250px;
height: 250px;
}
.textwrap {
width: 200px;
height: 250px;
position: absolute;
display: none;
}
.smallwrap:nth-child(1) .textwrap {
left: -225px;
top: 0px;
}
.smallwrap:nth-child(2) .textwrap {
right: -225px;
top: 0px;
}
.smallwrap:nth-child(3) .textwrap {
left: -225px;
bottom: 0px;
}
.smallwrap:nth-child(4) .textwrap {
right: -225px;
bottom: 0px;
}