1
<a href="#scrollDown" id="up">Scroll Down</a>
<p>Long Paragraph.</p>
<a name="scrollDown" id="down">Stop Here</a>

While I click the id 'up' it should scroll down and stop in to id 'down'.

Right now when I click id 'up' the URL is getting changed. for example(../loginAction#scrollDown).

How to scroll down to the id 'down' without changing the page URL. The href 'scrollDown' won't apply to the page URL.

Raja O
  • 634
  • 1
  • 12
  • 33

4 Answers4

3

You can do that by getting the offset of the element and then scrolling to it.

var elementPosition = document.getElementById('#myElement').offsetTop;
window.scrollTo(0, elementPosition);
Erick A. Montañez
  • 447
  • 1
  • 6
  • 11
1

If You use href tag url will change. You can use id tag for up-down links and you can use jQuery $('#up').click

yucel
  • 365
  • 4
  • 11
1

$(function() {
  $('#up').click(function(){
    $('html, body').animate({
        scrollTop: $("#down").offset().top
    }, 2000);
});
  
 })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<a href="javascript:void(0)" id="up">Scroll Down</a>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p>
  <br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p>
  v
  <br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p><br>
<p>Long Paragraph.</p>
















<a name="scrollDown" id="down">Stop Here</a>

</body>
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28
1

The url is changing because that is the value of the href attribute of the link. By default the browser is finding and scrolling to the div with the id of the url hash, so asdf.com/#someid will always display page with #someid div at the top. To avoid the url change and have the same functionality you could use custom attributes for triggers.

<p custom-target="scrollDown" id="up">Scroll Down</p>
<p>Long Paragraph.</p>
<a name="scrollDown" id="down">Stop Here</a>

and then you modify your scrolling script to listen for click on p[custom-target="scrollDown"] I am changing the triggering element to p because there is no need for it to be link anymore.

Bojana Šekeljić
  • 1,056
  • 1
  • 12
  • 30