所有JS代码都在工作,包括其他jquery库。 只是这一个不是,但奇怪的是,控制台中没有报告错误。 这可能是因为抵消财产? PS。在Edge和Chrome中效果很好,所以我想这一定是我的错,但我不知道是什么。 PS2。我很抱歉我的代码有点长,它实际上是来自这里的代码,stackoverflow。
// set to true to see the console.log debug info
var debug = false;
// setup default index of 0
var index = 0;
// setup scrolling flag
var scrolling = false;
// cache a few elements for reference
var win = $(window);
var body = $('body');
var container = $('.container');
// setup a few more variables for future use
var lastTop = 0;
var current, offset;
// bind scroll event
win.on('scroll', function(ev) {
// prevent the default event
ev.preventDefault();
// check to make sure we don't fire the scroll multiple times
if (debug) console.log(scrolling, 'scrolling');
if (scrolling) return;
// set the scrolling flag to true
scrolling = true;
// run the update function
updateActive();
// clear the scrolling flag to allow the user to access the next element
setTimeout(function() {
scrolling = false;
}, 500);
// update a few variables for the next interaction
current = container.find('.inner[data-index=' + index + ']');
offset = current.offset();
lastTop = offset.top;
// handle the animation
body.animate({
scrollTop: offset.top + 'px'
});
});
function updateActive() {
// get the current top offset
var top = win.scrollTop();
// determine which direction we are scrolling
if (top > lastTop) {
// down
if (debug) console.log('scrolling down');
// let make sure we aren't on the last element
if (debug) console.log(index, $('.inner').length);
if (index == $('.inner').length) {
if (debug) console.log('on last element, nothing to do');
return;
}
// update index & top
if (debug) console.log(index, 'index before');
index++;
if (debug) console.log(index, 'index after');
lastTop = top;
} else {
// up
if (debug) console.log('scrolling up');
// let make sure we aren't on the first element
if (!index) {
if (debug) console.log('on first element, nothing to do');
return;
}
// update index & top
if (debug) console.log(index, 'index before');
index--;
if (debug) console.log(index, 'index after');
lastTop = top;
}
}
答案 0 :(得分:0)
每Animate scrollTop not working in firefox,您需要为' html'设置动画。 Firefox中的元素:
$('body,html').animate(...);
有关固定版本,请参阅https://jsfiddle.net/vr2jk1p2/1/。