원하는 동작은 다음과 같습니다. 상단 버튼을 클릭하고 하단 div로 스크롤 한 다음 위의 div를 분리합니다. 하단 div의 버튼을 클릭하면 분리 된 요소를 다시 삽입하고 상단 div로 스크롤 한 다음 아래 div를 분리합니다.분리 div를 동적으로 추가하고 스크롤하십시오.
결국 div 사이에는 애니메이션 스크롤이 있고 다른 div는 분리/다시 삽입됩니다.
어떤 이유인지 이것은 나를 위해 작동하지 않습니다. 이 jsfiddle에서 위쪽 링크를 클릭하면 예상대로 동작합니다. 그러나 스크롤 업은 작동하지 않습니다. 분리 된 요소는 다시 삽입되지 않습니다! 내가 도대체 뭘 잘못하고있는 겁니까? 미리 감사드립니다!
HTML
<div id="w">
<div id="top">
<a href='#bottom' id="scrollDown">Scroll down!</a>
</div>
<div id="space"></div>
<div id="bottom">
<a href="#top" id="scrollUp">Scroll up!</a>
</div>
</div>
jQuery를
var t;
var s;
var b;
jQuery('#scrollDown').on('click',function(){
var w = jQuery('#w');
if(b){
w.prepend(b);
b = null;
}
if(s){
w.prepend(s);
s = null;
}
var bottomPosition = jQuery('#bottom').position().top;
$("html, body").animate({scrollTop:bottomPosition},1000,'swing',function(){
t = jQuery('#top').detach();
s = jQuery('#space').detach();
});
return false;
});
jQuery('#scrollUp').on('click',function(){
var w = jQuery('#w');
if(s){
w.prepend(s);
s = null;
}
if(t){
w.prepend(t);
t = null;
}
$("html, body").animate({scrollTop:0},1000,'swing',function(){
b = jQuery('#bottom').detach();
s = jQuery('#space').detach();
});
return false;
});
그래서이와 코드와 주변 연주와 함께 좋아
div { display:block; }
#top {
height: 500px;
width:100%;
background-color: #AAA;
overflow:hidden;
}
#space {
height: 1000px;
background-color: #336688;
}
#bottom {
width:100%;
height: 500px;
background-color: #D0D0D0;
}
왜 당신이'.detach()'를 사용하여 복잡하게 만들었습니까? 'height : 0px'가 당신을 위해 작동하지 않을까요? – Anubhav
더 나은 성능을 얻으려면 dom 트리에서 요소를 제거하는 것이 더 좋을 것이라고 생각했습니다. 상단 및 하단 div에 물건이 많이 있습니다! 어쨌든 일반적인 질문으로 예상대로 작동하지 않는 이유가 궁금합니다! – seyfe