JavaScriptのソース
window.scrollToが効きません
このソースのどこがおかしいのでしょうか?
発生している問題・エラーメッセージ
//トップ移動に関する関数 (function(e){ const links = document.querySelectorAll('a[href^=\'#\']'); for(var i = 0; i < links.length; i++){ let href = (links[i].attributes.href === undefined) ? null : links[i].attributes.href.nodeValue.toString(); if (href !== null && href.length > 1 && href.substr(0, 1) == '#') { links[i].onclick = function () { let href = this.attributes.href.nodeValue.toString(); //let target = href.split('#'); let element = document.querySelector(href); let target_anchor = element.getBoundingClientRect(); let y = window.pageYOffset; let target_element = (y !== undefined) ? y : document.documentElement.scrollTop console.log(y); window.scroll( 0, 1000 ); let d = { top: target_anchor.top + target_element, behavior: "smooth" } window.scrollTo(d); /* window.scrollTo({ top: target_anchor.top + target_element, behavior: "smooth" }); */ } } } //e.preventDefault; })();
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/05 14:01