admin管理员组文章数量:1131222
I'm trying to scroll down 100px every time the user gets near the top of the document.
I have the function executing when the user gets close to the top of the document, but the .scrollTo function isn't working.
I put an alert after and before to check to see if it actually was the line or not that was stopping it and only the first alert goes off, here's the code:
alert("starting");
$.scrollTo({ top: '+=100px', left: '+=0px' }, 800);
alert("finished");
I know I have the jquery page linked properly because I'm using many other jquery functions throughout and they all work fine. I've also tried removing the 'px' from above and it doesn't seem to make a difference.
I'm trying to scroll down 100px every time the user gets near the top of the document.
I have the function executing when the user gets close to the top of the document, but the .scrollTo function isn't working.
I put an alert after and before to check to see if it actually was the line or not that was stopping it and only the first alert goes off, here's the code:
alert("starting");
$.scrollTo({ top: '+=100px', left: '+=0px' }, 800);
alert("finished");
I know I have the jquery page linked properly because I'm using many other jquery functions throughout and they all work fine. I've also tried removing the 'px' from above and it doesn't seem to make a difference.
Share Improve this question asked May 7, 2009 at 4:13 Matt DoakMatt Doak 1- 3 Jquery itself my be working fine, but are you sure you have the scrollTo plugin linked properly? Change one of those alerts to alert($.scrollTo); – Andrew Commented May 7, 2009 at 5:03
6 Answers
Reset to default 327$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);
If it's not working why don't you try using jQuery's scrollTop method?
$("#id").scrollTop($("#id").scrollTop() + 100);
If you're looking to scroll smoothly you could use basic javascript setTimeout/setInterval function to make it scroll in increments of 1px over a set length of time.
jQuery now supports scrollTop as an animation variable.
$("#id").animate({"scrollTop": $("#id").scrollTop() + 100});
You no longer need to setTimeout/setInterval to scroll smoothly.
To get around the html
vs body
issue, I fixed this by not animating the css directly but rather calling window.scrollTo();
on each step:
$({myScrollTop:window.pageYOffset}).animate({myScrollTop:300}, {
duration: 600,
easing: 'swing',
step: function(val) {
window.scrollTo(0, val);
}
});
This works nicely without any refresh gotchas as it's using cross-browser JavaScript.
Have a look at http://james.padolsey.com/javascript/fun-with-jquerys-animate/ for more information on what you can do with jQuery's animate function.
Looks like you've got the syntax slightly wrong... I'm assuming based on your code that you're trying to scroll down 100px in 800ms, if so then this works (using scrollTo 1.4.1):
$.scrollTo('+=100px', 800, { axis:'y' });
Actually something like
function scrollTo(prop){
$('html,body').animate({scrollTop: $("#"+prop).offset().top +
parseInt($("#"+prop).css('padding-top'),10) },'slow');
}
will work nicely and support padding. You can also support margins easily - for completion see below
function scrollTo(prop){
$('html,body').animate({scrollTop: $("#"+prop).offset().top
+ parseInt($("#"+prop).css('padding-top'),10)
+ parseInt($("#"+prop).css('margin-top'),10) +},'slow');
}
本文标签: javascriptHow to scroll the window using JQuery scrollTo() functionStack Overflow
版权声明:本文标题:javascript - How to scroll the window using JQuery $.scrollTo() function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736768974a1951978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论