如何在不影响回调的情况下停止滚动动画(jQuery.animate())
How to stop scroll animation without affecting the callback (jQuery.animate())
在我的网站应用程序中,我通过调用此函数来使用滚动动画:
function Scroll_To_Top(element, offset, duration, easing, callback = null, args = null) {
$('html, body').on('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove', function() { $('html, body').stop(); });
$('html, body').animate({scrollTop: element.offset().top - offset}, duration, easing, function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
if (callback != null && args != null) {
callback.apply(this, args);
}
else {
if (callback != null && args == null) {
callback();
}
}
});
}
用户滚动页面后动画立即停止,这是预期的,但这当然也会停止传递给动画函数的回调。
是否可以在不影响传递给动画函数的回调的情况下实现类似的功能?
非常感谢您的帮助!
解决方案:
function Scroll_To_Top(element, offset, duration, easing, callback = null, args = null) {
$('html, body').on('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove', function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
$('html, body').stop();
if ((callback != null) && (args != null)) {
callback.apply(this, args);
}
else {
if ((callback != null) && (args == null)) {
callback();
}
}
});
$('html, body').animate({scrollTop: element.offset().top - offset}, duration, easing, function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
if ((callback != null) && (args != null)) {
callback.apply(this, args);
}
else {
if ((callback != null) && (args == null)) {
callback();
}
}
});
}
在我的网站应用程序中,我通过调用此函数来使用滚动动画:
function Scroll_To_Top(element, offset, duration, easing, callback = null, args = null) {
$('html, body').on('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove', function() { $('html, body').stop(); });
$('html, body').animate({scrollTop: element.offset().top - offset}, duration, easing, function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
if (callback != null && args != null) {
callback.apply(this, args);
}
else {
if (callback != null && args == null) {
callback();
}
}
});
}
用户滚动页面后动画立即停止,这是预期的,但这当然也会停止传递给动画函数的回调。
是否可以在不影响传递给动画函数的回调的情况下实现类似的功能? 非常感谢您的帮助!
解决方案:
function Scroll_To_Top(element, offset, duration, easing, callback = null, args = null) {
$('html, body').on('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove', function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
$('html, body').stop();
if ((callback != null) && (args != null)) {
callback.apply(this, args);
}
else {
if ((callback != null) && (args == null)) {
callback();
}
}
});
$('html, body').animate({scrollTop: element.offset().top - offset}, duration, easing, function() {
$('html, body').off('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove');
if ((callback != null) && (args != null)) {
callback.apply(this, args);
}
else {
if ((callback != null) && (args == null)) {
callback();
}
}
});
}