Javascript 如何使用scrollIntoView()同时平滑滚动两个元素?
我正在尝试使用scrollIntoView()同时平滑滚动两个div。我尝试过这两种方法,但只有最后一个div称为scrolls: 尝试1:具有两个参数的函数:仅滚动第二个参数Javascript 如何使用scrollIntoView()同时平滑滚动两个元素?,javascript,jquery,html,scroll,smooth-scrolling,Javascript,Jquery,Html,Scroll,Smooth Scrolling,我正在尝试使用scrollIntoView()同时平滑滚动两个div。我尝试过这两种方法,但只有最后一个div称为scrolls: 尝试1:具有两个参数的函数:仅滚动第二个参数 function precedent_scroll(link, section) { document.getElementById(link).scrollIntoView({behavior: "smooth"}); document.getElementById(section).scrollIntoVie
function precedent_scroll(link, section) {
document.getElementById(link).scrollIntoView({behavior: "smooth"});
document.getElementById(section).scrollIntoView({behavior: "smooth"});
}
尝试2:背对背调用函数:仅滚动“section2\u IDname”
仅使用scrollIntoView()就可以做到这一点吗?将其用于jQuery:
function double_scroll(id1, id2) {
var id1_parent_st = $([id1 parent]).scrollTop();
var id2_parent_st = $([id2 parent]).scrollTop();
$([id1 parent]).animate({
scrollTop: $(id1).position().top + id1_parent_st
}, 500, function(){
});
$([id2 parent]).animate({
scrollTop: $(id2).position().top + id2_parent_st
}, 500, function(){
});
}
$([div]).click(function() {double_scroll("#p1_link", "#p1_section")});
如果目标是滚动到一个,则需要设置超时,让用户查看具体时间,然后滚动到other@charlietfl这就是我目前正在使用的替代解决方案,但是有没有一种方法可以同时使用scrollIntoView({behavior:“smooth”})(或者类似动画的东西)来实现这两种功能?不是内置的。。。不可以。可以使用jQuery animate with delay(),但它需要您自己的position calcsDid。您知道如何在没有jQuery的情况下实现吗?@NahushFarkande我相信,如果您只查看如何将部分代码翻译为纯JS,它会起作用。例如
function double_scroll(id1, id2) {
var id1_parent_st = $([id1 parent]).scrollTop();
var id2_parent_st = $([id2 parent]).scrollTop();
$([id1 parent]).animate({
scrollTop: $(id1).position().top + id1_parent_st
}, 500, function(){
});
$([id2 parent]).animate({
scrollTop: $(id2).position().top + id2_parent_st
}, 500, function(){
});
}
$([div]).click(function() {double_scroll("#p1_link", "#p1_section")});