具有setInterval的窗口位置

时间:2016-11-19 00:28:47

标签: javascript

我正在寻找下面这些代码的组合

document.getElementsByTagName("a").addEventListener("click", myFunction);
function myFunction(item) {
    window.location = (""+window.location).replace(/#[A-Za-z0-9_]*$/,'')+"#"+item;
}

与此结合

var duration = 10;
var Ypos = 400;
var timerID = setInterval(function() {
    window.scrollBy(0, duration);
    if( window.pageYOffset >= Ypos )
        clearInterval(timerID);
}, 13);

我想要的是窗口位置为var Ypos,这可能吗?

HTML

<div class="menu">
<a href="#item_one">Item 1</a>
<a href="#item_two">Item 2</a>
<a href="#item_three">Item 3</a>
<a href="#item_four">Item 4</a>
</div>

<div id="item_one">item_one and more text</div>

<div id="item_two">item_two and more text</div>

<div id="item_three">item_three and more text</div>

<div id="item_four">item_four and more text</div>

0 个答案:

没有答案