"draggable"이미지 세트의 초기 위치를 이미지의 데이터 속성으로 저장하려고합니다. 그런 다음 "putBack"버튼을 누르면 이미지가 원래 위치로 돌아갑니다. 문제는 이미지가 상대 위치로 돌아 오는 것입니다. Top & Left 대신 2XTop 및 2XLeft로 배치됩니다.jquery 애니메이션 위치 - 절대 값과 상대 값
// 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를 ... 상위 li
하지 img
의 위치를 유지하는 것처럼 보이는
ul{
width:100px;
padding:0px;
list-style:none;
font-size:20px;
}
당신은 또한 당신의 HTML을 추가 할 수 있습니까? –