我偶然发现了一个非常奇怪的错误。当 .extrasWarning 类不在窗口的视线范围内时,我使用 jQuery scrollTop 让窗口滚动到该类的位置。这是以下代码:
$('[data-required] .number select').change(function () {
var number = $(this).closest('.choice_data').data('required'),
windowPos = $(window).height(),
selectedAmount = 0;
alert(windowPos);
$(this).closest('.choice_data').find('.number option:selected').each(function (i) {
selectedAmount = selectedAmount + parseInt($(this).val());
});
if (selectedAmount > number) {
$(this).closest('.choice_data').next('.extrasWarning').show();
var errorPos = $(this).closest('.choice_data').next('.extrasWarning').offset().top;
alert(errorPos);
if (errorPos > windowPos) {
$(window).animate({
scrollTop: errorPos
}, 1000);
}
} else {
$('.extrasWarning').hide();
}
});
当我使用该元素选择另一个选项时,除 $(window).animate 函数外,所有事件均正常触发。 FireFox 显示以下错误:a.ownerDocument 未定义。
问题在于将 animate 函数与scrollTop 函数结合使用。如果我实现以下更改:
if (errorPos > windowPos) {
$(window).scrollTop(errorPos);
}
突然就可以正常工作了!不过,我真的很想使用动画功能。知道我怎样才能做到这一点吗?谢谢!
最佳答案
尝试为 body 和 html 元素添加动画:
if (errorPos > windowPos) {
$("body, html").animate({
scrollTop: errorPos
}, 1000);
}
关于jquery - 使用jQuery的scrollTop和animate函数时出现ownerDocument错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846609/