最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alternative to anchor URL - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question asked Feb 21, 2015 at 12:05 user4583684user4583684 1
  • Bind the click and use scrollTo? – Lloyd Commented Feb 21, 2015 at 12:07
Add a ment  | 

2 Answers 2

Reset to default 7

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>

With jQuery:

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

评论列表(0)

  1. 暂无评论