【发布时间】:2014-01-17 21:17:36
【问题描述】:
我将有一些图像,正面是图片,背面是信息。我将它设置为在悬停时翻转,但翻转一个似乎也会翻转另一个(在这个例子中,我只使用了两个正面黑色和背面蓝色的图像)
如何更改代码以使其仅翻转鼠标悬停的一张图像?
脚本
$(document).ready(function(){
var margin=$('.front').width()/2;
var width=$('.front').width();
var height=$('.front').height();
$('.back').stop().css({width:'0px',height:''+height+'px',
marginLeft:''+margin+'px',opacity:'0.5'});
$('.front').hover(function(){
$(this).stop().animate({width:'0px',height:''+height+'px',
marginLeft:''+margin+'px',opacity:'0.5'},
{duration:500});
window.setTimeout(function() {
$('.back').stop().animate({width:''+width+'px',height:''+height+'px',
marginLeft:'0px',opacity:'1'},
{duration:500});
},500);
});
$('.back').hover(function(){
$(this).stop().animate({width:'0px',height:''+height+'px',
marginLeft:''+margin+'px',opacity:'0.5'},
{duration:500});
window.setTimeout(function() {
$('.front').stop().animate({width:''+width+'px',height:''+height+'px',
marginLeft:'0px',opacity:'1'},
{duration:500});
},500);
});
});
标记:
<div id="container">
<img class="front" id="image1" src="images/front_image.png" alt="" />
<img class="back" id="image2" src="images/back_image.png" alt="" />
</div>
<div id="container">
<img class="front" id="image3" src="images/front_image.png" alt="" />
<img class="back" id="image4" src="images/back_image.png" alt="" />
</div>
CSS:
#container {position: relative; width: 140px; height: 200px; display: inline-block;}
.front {position: absolute; cursor: pointer;}
.back {display: none; position: absolute; cursor: pointer;}
【问题讨论】:
-
id应该是唯一的 -
我创建了一个JSFiddle,其中包含您提供给我们的信息。它似乎没有破裂。
-
谢谢,Loyalar。但是,当您将鼠标悬停在小提琴中的一张图像上时,图像会完全消失。
-
如果你想要 css 解决方案你可以看看这个davidwalsh.name/css-flip