Javascript 使用scrollIntoView提供偏移量
我正在使用Javascript 使用scrollIntoView提供偏移量,javascript,angular,typescript,ionic3,Javascript,Angular,Typescript,Ionic3,我正在使用scrollIntoView()转到选定的输入。但是我把它设为top,所以我想给它一个偏移量 但这似乎不起作用我也检查了scrollTo方法,并这样做: this.keyboard.onKeyboardShow().subscribe(() => { console.log('document.activeElement!!!!', document.activeElement) // document.activeElement.scrollIntoView(t
scrollIntoView()
转到选定的输入。但是我把它设为top,所以我想给它一个偏移量
但这似乎不起作用我也检查了scrollTo方法,并这样做:
this.keyboard.onKeyboardShow().subscribe(() => {
console.log('document.activeElement!!!!', document.activeElement)
// document.activeElement.scrollIntoView(true);
const top = document.activeElement.getBoundingClientRect().top + window.scrollY;
console.log(top);
window.scroll(0, top)
window.scrollTo({
top: top,
behavior: 'smooth'
});
// document.activeElement.scrollTo(0, 10);
window.scrollBy(-100, 0);
});
但这将不再滚动到该元素。谁能帮我设置一个偏移量到
scrollIntoView()
我不知道你到底想做什么。是否要滚动到元素上方或下方?@ChrisSandvik滚动到document.activeElement,并在上方设置偏移量<代码>document.activeElement.scrollIntoView(true)代码>这是有效的,但是用这种方法我不能给出任何偏移量。当你使用窗口时,它是滚动的。滚动到?因为你正在使用的偏移量应该是负的,根本不滚动,控制台。log(顶部)记录我436.5238037109375
我也尝试过这个。带有viewchild(content)的内容可能尝试设置left:0
在滚动选项中我不知道你到底想做什么。是否要滚动到元素上方或下方?@ChrisSandvik滚动到document.activeElement,并在上方设置偏移量<代码>document.activeElement.scrollIntoView(true)代码>这是有效的,但是用这种方法我不能给出任何偏移量。当你使用窗口时,它是滚动的。滚动到?因为您正在使用的偏移量可能为负,根本不滚动,并且控制台。日志(顶部)记录我436.5238037109375
我也尝试了此操作。带有viewchild(内容)的内容可能尝试在scrollTo选项中设置left:0