HTML:jQuery的動畫(),展開一個div
<div id="box0"><img src="http://s14.postimg.org/9x1nrefy5/A_Sunday_on_La_Grande_Jatte.png" id="pic0"></div>
<div id="box1"><img src="http://s14.postimg.org/hbqzjs1tp/starry_night.png" id="pic1"></div>
<div id="box2"><img src=http://s14.postimg.org/x7ftn2se5/mona_lisa.png" id="pic2"></div>
<div id="box3"><img src="http://s14.postimg.org/k4k73t265/the_scream.png" id="pic3"></div>
CSS:
* {
padding: 0px;
margin: 0px;
z-index: 0;
}
img {
position: relative;
}
div {
position: absolute;
overflow: hidden;
border: 8px solid #61260D;
}
#box0 { /* A Sunday on La Grande Jatte */
height: 150px;
width: 120px;
top: 300px;
left: 100px;
}
#pic0 {
left: -15px;
top: -15px;
height: 337px;
width: 500px;
}
#box1 { /* Starry Night */
height: 100px;
width: 400px;
top: 150px;
left: 100px;
}
#pic1 {
left: -35px;
top: -20px;
height: 300px;
width: 480px;
}
#box2 { /* Mona Lisa */
height: 150px;
width: 100px;
top: 190px;
left: 50px;
}
#pic2 {
left: -20px;
top: -20px;
height: 300px;
width: 198px;
}
#box3 { /* The Scream */
height: 200px;
width: 160px;
top: 60px;
left: 200px;
}
#pic3 {
left: -30px;
top: -20px;
height: 400px;
width: 314px;
}
的Javascript:
var h = [];
var w = [];
var left = [];
var top = [];
var speed = 300;
for (var i = 0; i < 4; i ++) {
h[i] = $('#box'+i).css('height');
w[i] = $('#box'+i).css('width');
left[i] = $('#pic'+i).css('left');
top[i] = $('#pic'+i).css('top');
}
for (var i = 0; i < 4; i ++) {
$('div').hover(
function() {
$(this).stop().css({'zIndex':'5'}).animate({
height : $(this).children().css('height'),
width : $(this).children().css('width')
}, speed);
$(this).children().animate({
left : '0',
top : '0'
}, speed);
},
function() {
$(this).stop().css({'zIndex':'0'}).animate({
height : h[i],
width : w[i]
}, speed);
$(this).children().animate({
left : left[i],
top : top[i]
}, speed);
}
);
}
當光標進入一個div我想要做的就是,div將展開爲圖像大小,嵌入的圖像將使用animate()更改其位置。
它確實展開,但是,光標離開時,div不會恢復其原始大小。
Here is the demo
另一個問題:我不能用$( '#PIC' + i)和所以我用$(本)。兒童()。所以爲什麼我不能使用它?這是一個錯誤? – Thang