2015-02-21 11 views
5

Gibt es eine andere Möglichkeit, ein Element auf derselben Seite zu verknüpfen, ohne Anker-URLs zu verwenden?

Alternative zum Verankern der 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 dem Klick und nutzen scrollTo? – Lloyd

Antwort

8

Es ist! Schauen Sie sich den folgenden Code:

<!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

Das ist genau das, was ich gesucht habe, vielen Dank! –

2

Mit jQuery:

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