2015-02-21 12 views
5

C'è un altro modo per collegarsi a un elemento sulla stessa pagina senza utilizzare gli URL di ancoraggio?

Alternativa all'ancora URL

<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
<meta charset="utf-8"> 
<title>Alternative to anchor URL</title> 
</head> 

<body> 

<input type="button" value="Go there &raquo;" /> 

<div style="height:1280px;"></div> 

<div id="goHere" style="width:360px;height:240px;background-color:#61b2cc"></div> 

</body> 
</html> 
+0

Bind il clic e utilizzare scrollTo? – Lloyd

risposta

8

C'è! Guardate il codice qui sotto:

<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
<meta charset="utf-8"> 
<title>Alternative to anchor URL</title> 

<script type="text/javascript"> 
function scrollWindow() { 
    { 
    var top = document.getElementById('goHere').offsetTop; 
    window.scrollTo(0, top); 
    } 
} 
scrollWindow(); 
</script> 
</head> 

<body> 

<input type="button" onclick="scrollWindow()" value="Go there &raquo;" /> 

<div style="height:1280px;"></div> 

<div id="goHere" style="width:360px;height:240px;background-color:#61b2cc"></div> 

</body> 
</html> 
+0

Questo è esattamente quello che cercavo, grazie mille! –

2

Con jQuery:

$("button").click(function(){ 
    window.scrollTo($("#goHere").offset().top); 
});