[英]scrollIntoView in not working, why? and how to fixed?
我怎樣才能使這段代碼工作? 我收到錯誤
//scroll to section 這部分是獲取所有的
const linkes = document.querySelectorAll('a')
linkes.forEach(link => {
link.addEventListener('click', toSection())
})
這部分是為 scrollIntoView 構建 function
function toSection() {
sections.forEach(section => {
const sectionsIds = section.id
})
sectionsIds.scrollIntoView();
}
你可以這樣做
<a data-myattr="myScrollSection1">to section 1</a>
<a data-myattr="myScrollSection2">to section 2</a>
<div id="myScrollSection1">Section1</div>
<div id="myScrollSection2">Section2</div>
<script>
const links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
links[i].addEventListener("click", (e) => {
myId = e.target.dataset.myattr;
console.log(myId);
document.getElementById(myId).scrollIntoView();
});
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.