【发布时间】:2012-04-11 07:21:48
【问题描述】:
我想将一组“可拖动”图像的初始位置存储为图像中的数据属性。然后,当按下“putBack”按钮时,图像将返回其原点。问题是图像返回到相对位置。它们被定位到 2XTop 和 2XLeft,而不是 Top & Left。
// assign data attributes x,y data attributes
$('li img').each(function(index) {
$(this).data("Left", $(this).parent().position().left)
.data("Top", $(this).parent().position().top);
console.log($(this).data("Top"));
});
// button to put images back where they started
$("#putBack").bind('click', function() {
$('li img').each(function(index) {
console.log($(this).data("Top"));
$(this).parent().animate(
{ "left": $(this).data("Left"),
"top": $(this).data("Top")
}, "slow");
});
});
html...
<ul>
<li id='apple'><img src="images/apple.png"/></li>
<li id='bread'><img src="images/bread.png"/></li>
<li id='banana'><img src="images/banana.png"/></li>
<li id='hot_dog'><img src="images/hot_dog.png"/></li>
<ul>
css...
ul{
width:100px;
padding:0px;
list-style:none;
font-size:20px;
}
【问题讨论】:
标签: jquery layout position jquery-animate