1

I am looking for combine these codes below

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

Combine with this

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

What I want is the window location as var Ypos, is this possible?

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>
  • 2
    Possible duplicate of [Simple jQuery scroll to anchor up or down the page...?](http://stackoverflow.com/questions/8579643/simple-jquery-scroll-to-anchor-up-or-down-the-page) –  Nov 19 '16 at 00:37
  • he wants to use setInterval to navigate to a new fragment (#) of the page every few seconds. no need to be rude to someone who's first language is not english – thedarklord47 Nov 19 '16 at 00:37
  • The dupe @ChrisG linked appears to solve his question without abusing `window.location` – thedarklord47 Nov 19 '16 at 00:39
  • Your first code should be `location.hash = item`. After that I don't understand what you want. – Oriol Nov 19 '16 at 21:52
  • I am looking for something like the Jquery code, but in plain JavaScript. If the upper code can transmit the position to the second code "var Ypos" I think it's working isn't it? – Loek Lemmens Nov 19 '16 at 21:53

0 Answers0