ボタンを押すと1へスクロールさせたいです。
topに戻るjsにbodyやhtmlのセレクタが見当たらないので、どの部分を変更すればいいのか分かりません。
ご教授お願いします。
html
1<body> 2 <div id="scroll-one">1</div> 3 <button id="scroll-btn">1へスクロール</button> 4 <div id="top">top</div> 5</body>
js
1scrollTop("top",500); 2function scrollTop(elem,duration){ 3var target=document.getElementById(elem); 4target.addEventListener("click",function(){ 5var currentY=window.pageYOffset; 6var step=duration/currentY>1?10:100; 7var timeStep=duration/currentY*step; 8var intervalID=setInterval(scrollUp,timeStep); 9function scrollUp(){ 10currentY=window.pageYOffset; 11if(currentY===0){ 12clearInterval(intervalID); 13}else{ 14scrollBy(0,-step); 15} 16} 17}); 18}
topとは、ページの一番上ということでよろしいですか?
説明不足ですみません。ページ内リンクにスクロールさせたいのですが、教本にはトップに戻るコードしか載っていませんでした。なのでそのコードを編集しようとしたのですが、どこを編集すればいいのか分からなかったので、質問させて頂きました。
実用するなら、http://kyasper.com/jquery-tips/ などを参考に作り直すことをすすめます。
回答3件
あなたの回答
tips
プレビュー