2

I wish to make a menu. Usually to help the visitor to get to a specific area I'll use this method:

<a href="#id">

would scroll to an element on the current page such as <div id="id">.

I wish to know can this be possible to write in javascript? such as my guess

<a href="javascript:#id"></a>  <div id="id">
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ray
  • 21
  • 1
  • 3

2 Answers2

3

Simply <a> with href with the element id will do the job:

<a href="#{id}"></a>

But if for some wild reason you want to use javascript for this (why?), here is a way:

#foo {
    margin-top: 500px
}
<a href="#" onclick="window.location.href='#foo'; return false;" id="anchor">Click to go to foo</a>
 <div id="foo">Some foo content</div>
 <br>
 <a href="#anchor">Back to anchor</a>
gdoron
  • 147,333
  • 58
  • 291
  • 367
0

auto scroll function scroll any div or body by java script function Scroll function it work with any element have overflow style whatever in body or div

USE THE function BY <a onclick="MWScroll(body Id,target Id , margin top )" ></a>

variables : tb = body Id which element have overflow style

tid = div Id or any elmeant have block style Your Target

tde = the margin after scroll between target elment and top of its body `

function MWScroll(tb,tid,tde) {
     var TY = 0;
         TY = document.getElementById(tid).offsetTop;
        document.getElementById(tb).scroll(0, TY -tde);

    }
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • 2
    This isn't really an answer. Please expand on your solution and explain what it does more, using plain English and examples. These questions are meant to help just the original poster, but also people who may arrive here via Google searches. – Bassinator Dec 31 '17 at 16:15