Javascript scrollIntoView在惰性图像加载下无法正常工作
当页面上有惰性图像加载时,我很难让scrollIntoView API正常工作 本质上,当页面加载时,我通过Javascript scrollIntoView在惰性图像加载下无法正常工作,javascript,lazy-loading,scrollto,js-scrollintoview,Javascript,Lazy Loading,Scrollto,Js Scrollintoview,当页面上有惰性图像加载时,我很难让scrollIntoView API正常工作 本质上,当页面加载时,我通过querySelector抓取元素,当它滚动时,焦点由于图像加载而关闭(它将页面向下移动) 我已经尝试了没有延迟图像加载的页面,它工作正常 我不完全清楚我该如何处理这件事。我也有同样的问题,我有两个解决办法。两者都不是超理想的,但它们确实有效 您可以尝试一些概念,其中之一是轮询滚动事件是否完成,但是如果使用持续时间,您应该能够猜测整个事件。使用默认的摆动测量将使动画减慢一半,使动画加快一半
querySelector
抓取元素,当它滚动时,焦点由于图像加载而关闭(它将页面向下移动)
我已经尝试了没有延迟图像加载的页面,它工作正常
我不完全清楚我该如何处理这件事。我也有同样的问题,我有两个解决办法。两者都不是超理想的,但它们确实有效 您可以尝试一些概念,其中之一是轮询滚动事件是否完成,但是如果使用持续时间,您应该能够猜测整个事件。使用默认的摆动测量将使动画减慢一半,使动画加快一半,因此您有点拘泥于使用线性 这样做的想法是启动滚动动画,在它结束之前,启动另一个滚动动画。第一个动画应该能让你大致到达你想去的地方,在接近结尾的时候,文档应该已经加载了它需要加载的任何东西,第二个动画应该缩小它的范围并滚动到正确的位置 主动画和面片动画需要重叠。我已经对它们进行了设置,这样面片动画将与主动画同时完成。这可能是不好的形式。可以尝试在主动画结束时启动面片,或使面片延伸到主动画结束之后。调整数字以获得所需的结果 这当然是哈克里 使用jQuery,您可以得到一个用于完成动画的回调,这可能是一种比同时运行两者更好的方法 首先,jQuery使用setTimeout猜测开始修补的时间:
$(function() {
$('html,body').animate({ // main animation block
scrollTop: target.offset().top
},{
easing: 'linear',
duration: 800 // how long to animate in total
});
setTimeout(function() {
$('html,body').animate({
scrollTop: target.offset().top
}, {
easing: 'linear',
duration: 200 // how long to patch the animation
});
},600); // how long to wait before starting the patch
});
其次,使用complete的jQuery更简洁:
$(function() {
$('html,body').animate({
scrollTop: target.offset().top
},{
duration: 600, // main animation time
easing: 'linear',
complete: function() {
$('html,body').animate({
scrollTop: target.offset().top,
},{
easing: 'linear',
duration: 200 // patch time, this starts after main
});
}
});
});
Safari不支持开箱即用的平滑滚动,因此需要jQuery或其他javascript库来实现。但是,对于Chrome、Edge、Firefox等浏览器,以下内容应该可以使用:
var source = document.getElementById('yoursourceelementid');
source.onclick = function() {
var target = document.getElementById('idyouwanttoscrollto');
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
duration: 800 // how long to animate
});
setTimeout(function() { // patch animation
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
duration: 200 // how long to patch
});
}, 600); // how long to wait before patching
}
我也有同样的问题,我有两个解决办法。两者都不是超理想的,但它们确实有效 您可以尝试一些概念,其中之一是轮询滚动事件是否完成,但是如果使用持续时间,您应该能够猜测整个事件。使用默认的摆动测量将使动画减慢一半,使动画加快一半,因此您有点拘泥于使用线性 这样做的想法是启动滚动动画,在它结束之前,启动另一个滚动动画。第一个动画应该能让你大致到达你想去的地方,在接近结尾的时候,文档应该已经加载了它需要加载的任何东西,第二个动画应该缩小它的范围并滚动到正确的位置 主动画和面片动画需要重叠。我已经对它们进行了设置,这样面片动画将与主动画同时完成。这可能是不好的形式。可以尝试在主动画结束时启动面片,或使面片延伸到主动画结束之后。调整数字以获得所需的结果 这当然是哈克里 使用jQuery,您可以得到一个用于完成动画的回调,这可能是一种比同时运行两者更好的方法 首先,jQuery使用setTimeout猜测开始修补的时间:
$(function() {
$('html,body').animate({ // main animation block
scrollTop: target.offset().top
},{
easing: 'linear',
duration: 800 // how long to animate in total
});
setTimeout(function() {
$('html,body').animate({
scrollTop: target.offset().top
}, {
easing: 'linear',
duration: 200 // how long to patch the animation
});
},600); // how long to wait before starting the patch
});
其次,使用complete的jQuery更简洁:
$(function() {
$('html,body').animate({
scrollTop: target.offset().top
},{
duration: 600, // main animation time
easing: 'linear',
complete: function() {
$('html,body').animate({
scrollTop: target.offset().top,
},{
easing: 'linear',
duration: 200 // patch time, this starts after main
});
}
});
});
Safari不支持开箱即用的平滑滚动,因此需要jQuery或其他javascript库来实现。但是,对于Chrome、Edge、Firefox等浏览器,以下内容应该可以使用:
var source = document.getElementById('yoursourceelementid');
source.onclick = function() {
var target = document.getElementById('idyouwanttoscrollto');
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
duration: 800 // how long to animate
});
setTimeout(function() { // patch animation
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
duration: 200 // how long to patch
});
}, 600); // how long to wait before patching
}