user4583684
user4583684

Reputation:

Alternative to anchor URL

Is there an other way to link to an element on the same page without using anchor URLs?

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

Upvotes: 5

Views: 2846

Answers (2)

winhowes
winhowes

Reputation: 8065

With jQuery:

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

Upvotes: 2

user4563455
user4563455

Reputation:

There is! Look at the code below:

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

Upvotes: 7

Related Questions