5
jQuery에서 시각적으로나 DOM에서 두 컨테이너의 위치를 바꿀 수 있습니까?2 개의 HTML 요소를 시각적으로 DOM에 교환하고 모두 애니메이션으로 만드시겠습니까?
나는이 :
<div id="container">
<div id="one"><a class="moveUp">Up</a></div>
<div id="two"><a class="moveUp">Up</a></div>
<div id="three"><a class="moveUp">Up</a></div>
</div>
가 링크를 클릭하면 위의 하나 그 DIV의 위치를 교환합니다. 대부분의 예제에서는 절대 위치 지정을 사용하고이를 위해 top 속성을 상쇄하지만 ... 사용자가 다른 곳에서 함수를 호출 할 때 화면에 표시되는 순서대로 데이터를 읽어야합니다.
그래서 나는이 함께했다 :$('#container div').on('click', '.moveUp', function() {
divInQuestion = $(this).closest('div').attr('id'); //id of this div
if (divInQuestion > '1') {
switchWithDiv = divInQuestion - 1; //id of other div
firstSelector = $('#chapterset-'+divInQuestion).html(); //save contents of each div. I actually don't
secondSelector = $('#chapterset-'+switchWithDiv).html(); //do it this way, I regenerate the content
$('#chapterset-'+divInQuestion).html()firstSelector.replaceWith(secondSelector); //replace div with other div
$('#chapterset-'+switchWithDiv).html()secondSelector.replaceWith(firstSelector);
}
});
지금, 내 코드는 실제로 더 복잡 이것보다이지만, 내가 뭘하는지의 쉘을 제공합니다.
jQuery는 잘 작동하지만 애니메이션을 어떻게 포함합니까?
추신 : jsFiddle을 사용하려고했으나 서버가 다운되었을 수 있습니다. ?? !!?
꽤 오래된 스레드 - 이제는 내가 우연히 여기에서 비틀 거리며 솔루션을 테스트합니다 ... 다른 높이 div가있을 때 솔루션에 약간의 조정이 필요합니다. clickedDiv 높이는 두 애니메이션의 거리가 될 수 없습니다 (너 이 경우의 원래 코드 동작은 https://jsfiddle.net/wasb01m9/1/에서 확인할 수 있습니다. 이 문제를 해결하려면 https://jsfiddle.net/9g8Lvbs1/1/ 애니메이션의 다른 최상위 값 (다른 div outerHeight)을 사용하십시오. – ejuhjav