[英]Animate div based on position
I've done the following to animate divs to get a sort of zooming effect. 我已经完成了以下操作以使div动画化,从而获得一种缩放效果。
HTML HTML
<div class='tile-container'>
<div class='tile'>
<p>testing</p>
</div>
<div class='tile'></div>
<div class='tile'></div>
<div class='tile'></div>
<div class='tile'></div>
<div class='tile'></div>
<div class='clearfix'></div>
</div>
JS JS
$('.tile').click(function(){
if($(this).hasClass('clicked')){
return false;
}
var itemPosition = $(this).position(),
containerWidth = ( $('.tile-container').width() / 100 ) * 91,
containerHeight = ( $('.tile-container').height() / 100 ) * 86,
clone = $(this).clone();
$(clone).css({
'position':'absolute',
'top': -itemPosition.top,
'left': itemPosition.left
}).appendTo('.tile-container').animate({
width : containerWidth,
height: containerHeight
}).children().fadeOut("slow").promise().done(function(){
$(clone).append("<p class='back'>back</p>").addClass('clicked')
});
})
$(document).on('click','.back',function(){
var tile = $(this).parent()
$(tile).animate({
width : '50px',
height: '50px'
},function(){
$(tile).remove();
})
})
CSS CSS
body{
margin:0px;
padding:0px;
}
.tile-container{
background-color:#ccc;
width:210px;
height:140px;
}
.tile{
background-color:blue;
width:50px;
height:50px;
margin:10px;
float:left;
cursor:pointer;
}
.clearfix{
clear:both;
}
The animation works fine for first left element. 动画对于第一个左元素很好用。 How can I manage the rest of the elements with various positions.
如何管理处于不同职位的其余元素。
Just add top:0 and left:0 style to the animate 只需将top:0和left:0样式添加到动画中
.appendTo('.tile-container').animate({
width : containerWidth,
height: containerHeight,
'top': 0,
'left': 0
})
http://jsfiddle.net/z1xwxan5/13/ http://jsfiddle.net/z1xwxan5/13/
If you add top:0 and left:0 to the animate it'll come from the element you want. 如果将top:0和left:0添加到动画中,它将来自您想要的元素。
animate({
top : 0,
left : 0,
width : containerWidth,
height: containerHeight
})
To make it go back is a little trickier, but I guess just make itemPosition a global scope variable that gets assigned when you click an element, and then use that to animate top:itemPosition.top and left:itemPosition.left. 要使它返回还有些棘手,但是我想只是将itemPosition设为一个全局作用域变量,当您单击某个元素时,该变量将被赋值,然后使用该变量为top:itemPosition.top和left:itemPosition.left设置动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.