admin管理员组文章数量:1178538
For example, to scroll to a certain element on the page (ie here: How to go to a specific element on page?)
$("#fromTHIS").click(function() {
$("html, body").animate({ scrollTop: $("#toTHIS").offset().top }, 500);
return true;
});
I've tried both and they both look that they are doing the job. What am I missing?
For example, to scroll to a certain element on the page (ie here: How to go to a specific element on page?)
$("#fromTHIS").click(function() {
$("html, body").animate({ scrollTop: $("#toTHIS").offset().top }, 500);
return true;
});
I've tried both and they both look that they are doing the job. What am I missing?
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Oct 10, 2013 at 18:27 LiorLior 40.6k12 gold badges39 silver badges40 bronze badges 2- 6 Are you testing cross browser? $('html, body') covers all browsers while $('body') doesn't. – Jeffpowrs Commented Oct 10, 2013 at 18:29
- 1 as i recall, you need that that selector if you want your code to work in iframes – mkoryak Commented Oct 10, 2013 at 18:29
3 Answers
Reset to default 29The reason you use a selector for BOTH $('html, body')
is because of web browser inconsistency. After a few tests I have found three things:
- The browsers
Firefox
&IE
utilize the html portion of this selector - Browsers in the "webkit class" eg:
Safari
andChrome
respond to the body. Although one can avoid the issue all together by using$(document)
instead.
There's also a ticket on the jQuery bug tracker specifically stating this issue here
$('html, body')
seems to be the jquery solution for cross-browser scroll animation.
If you want a cross browser solution without animation, you can go ahead and try this:
$(window).scrollTop(0);
// Accepts int of pixels.
Tested it on latest Chrome, Opera and FF. Seems to work cross browser. (Unless someone can confirm that it doesn't work on IE or Safari or others)
Read more about jQuery scrollTop.
Here is an example for cross browser animation:
//('html, body') is the jquery solution for cross-browser scroll animation
$('html, body').animate({
scrollTop: $(".abc-container").offset().top+ "-50px"
}, 300)
本文标签: javascriptdifference between (39htmlbody39)animate and (39body39)animateStack Overflow
版权声明:本文标题:javascript - difference between $('html, body').animate and $('body').animate? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738007638a2048357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论