嘗試以下操作:
HTML
<a href="#" class="moveMe"><img src="..." /><span>Move Me</span></a>
CSS
.moveMe {
position: relative;
display: block;
width: 200px;
height: 200px;
overflow: hidden;
}
.moveMe img {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 250px;
}
.moveMe span {
position: absolute;
display: block;
bottom: 0;
left: 0;
padding: 10px 0;
width: 100%;
color: #fff;
text-align: center;
background: #000;
}
JS
$(document).ready(function(){
var toggled = false;
$('.moveMe').bind('click', function(e){
e.preventDefault();
if (toggled){
$('img', this).stop().animate({top: '0'}, 'slow');
toggled = false;
} else {
$('img', this).stop().animate({top: '-50px'}, 'slow');
toggled = true;
}
});
});
JSFiddle Here
根據需要調整CSS和width
height
。
我希望這有助於!
不幸的是,它沒有做任何事情。 –
@JamesMclaren,這個evnet必須寫在document.ready中。我已經用現場演示更新了我的答案。 –
是的,我已經加入了onload。但是,它只改變了高度。我改變了高度,所以它移動了div而不是改變高度。但是,類似於其他答案,它會將div移到頁面的頂部,這不是我想要的。 –