2017-12-31 98 views
0

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"> to a javascript

<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"> 
+4

Why do you want to use javascript instead of ' '? –

+0

試してみてください:https://stackoverflow.com/a/2368393/8237835 –

+0

[JavaScript - アンカーにジャンプ]可能な複製(https://stackoverflow.com/questions/5694686/javascript-jump-to-anchor) –

答えて

2

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>

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); 

    } 
+2

これは本当に答えではありません。簡単な英語と例を使用して、ソリューションを展開し、さらに何ができるかを説明してください。これらの質問は、元のポスターだけでなく、Google検索でここに到着する可能性のある人を支援するためのものです。 – Airhead