2017-12-31 91 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