動畫滾動頂部無法在Firefox中工作這個函數工作得很好。它將身體滾動到所需容器的偏移量。function scrolear(destino){
var stop = $(destino).offset().top;
var delay = 1000;
$('body').animate({scrollTop: stop}, delay);
return false;}但在Firefox中并非如此。為什么?為了在接受的答案中處理de Double觸發器,我建議在動畫之前停止元素:$('body,html').stop(true,true).animate({scrollTop: stop}, delay);
3 回答
慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
html
$('body,html').animate( ... );工作實例
html { overflow: hidden; height: 100%; }body { overflow: auto; height: 100%; }更新
scrollTop
function runOnce(fn) {
var count = 0;
return function() {
if(++count == 1)
fn.apply(this, arguments);
};};$('body, html').animate({ scrollTop: stop }, delay, runOnce(function() {
console.log('scroll complete');}));
幕布斯6054654
TA貢獻1876條經驗 獲得超7個贊
$('html, body')
.animate({ scrollTop: 100 })
.promise()
.then(function(){
// callback code here
})});// Note that the DOM needs to be loaded first, // or else document.body will be undefinedfunction getScrollTopElement() {
// if missing doctype (quirks mode) then will always use 'body'
if ( document.compatMode !== 'CSS1Compat' ) return 'body';
// if there's a doctype (and your page should)
// most browsers will support the scrollTop property on EITHER html OR body
// we'll have to do a quick test to detect which one...
var html = document.documentElement;
var body = document.body;
// get our starting position.
// pageYOffset works for all browsers except IE8 and below
var startingY = window.pageYOffset || body.scrollTop || html.scrollTop;
// scroll the window down by 1px (scrollTo works in all browsers)
var newY = startingY + 1;
window.scrollTo(0, newY);
// And check which property changed
// FF and IE use only html. Safari uses only body.
// Chrome has values for both, but says
// body.scrollTop is deprecated when in Strict mode.,
// so let's check for html first.
var element = ( html.scrollTop === newY ) ? 'html' : 'body';
// now reset back to the starting position
window.scrollTo(0, startingY);
return element;}// store the element selector name in a global var -
// we'll use this as the selector for our page scrolling animation.scrollTopElement = getScrollTopElement();$(scrollTopElement).animate({ scrollTop: 100 }, 500, function() {
// normal callback});
牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
$('body,html').animate({scrollTop: 50}, 500);body { height: 100%};auto100%
- 3 回答
- 0 關注
- 410 瀏覽
添加回答
舉報
0/150
提交
取消
