I set 버튼을 눌렀을 때 페이지 맨 위로 스크롤하지. 하지만 첫 번째 if 문은 사용 여부를 확인하기 위해 한 페이지 상단에 0 으로 설정되지 않았습니다. # 39 의 0 이 아닌 내가 애니메이션할 신앙이니라 it& 스크롤하지 페이지 맨.
var body = $("body");
var top = body.scrollTop() // Get position of the body
if(top!=0)
{
body.animate({scrollTop:0}, '500');
}
그 뒤 한 페이지가 가장 까다로운 부품 이제는 애니메이션 스크롤됩니다. 그래서 제 다음 페이지 포지셔닝하십시오 thought) 는, 확인할 수 있다. 그래서 사용되는 콘솔 로그를 확인할 수 있습니다.
console.log(top); // the result was 365
이 결과, 내게 준 것은 내가 포지셔닝하십시오 I& # 39 m 추측, 365 는 현재 상위 번호 스크롤하지 직전.
내 질문은 어떻게 설정합니까 스크램블된 포지셔닝하십시오 0, 0 에서 실행되는 다른 애니메이션 내가 할 수 있도록 추가 일단 페이지는?
감사합니다!
이를 위한 명령을 실행할 수 있는 기능을 설정할 수 있습니다 애니메이션할 콜백하는 스크롤해야 이후 애니메이션 완료되었습니다.
예를 들면 다음과 같습니다.
var body = $("html, body");
body.stop().animate({scrollTop:0}, 500, 'swing', function() {
alert("Finished animating");
});
여기서 더 javascript 에 추가할 수 있는 코드가 더 애니메이션 경고마다 실행할 수 있습니다.
또한, & # 39 swing& # 39;; 부드럽게를 설정할 수 있습니다. 자세한 정보는 http://api.jquery.com/animate/ 체크아웃합니다.
이 시도하시겠습니까 대신:
var body = $("body, html");
var top = body.scrollTop() // Get position of the body
if(top!=0)
{
body.animate({scrollTop :0}, 500,function(){
//DO SOMETHING AFTER SCROLL ANIMATION COMPLETED
alert('Hello');
});
}
이를 위해 콜백하는 방법을 사용할 수 있습니다.
body.animate({
scrollTop:0
}, 500,
function(){} // callback method use this space how you like
);
id 로 스크롤하지 어떠한 요소 또는 이름:
SmoothScrollTo("#elementId", 1000);
코드:
function SmoothScrollTo(id_or_Name, timelength){
var timelength = timelength || 1000;
$('html, body').animate({
scrollTop: $(id_or_Name).offset().top-70
}, timelength, function(){
window.location.hash = id_or_Name;
});
}
$(function () {
$('a[href*="#"]:not([href="#"])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
또는 그들을 시험하노라
$(function () {$('a').click(function () {
$('body,html').animate({
scrollTop: 0
}, 600);
return false;});});