【发布时间】:2015-03-11 01:31:03
【问题描述】:
我有一个简单的导航。
<nav>
<a href="#section1">page 1</a>
<a href="#section2">page 2</a>
<a href="#section3">page 3</a>
</nav>
当用户单击其中一个链接时,我希望页面动画到页面上的相应部分。
我似乎无法从变量中获取 .offset().top 值。任何帮助将不胜感激。
$("nav a").click(function(e) {
e.preventDefault();
// figure out which button was clicked
var targetName = $(this).attr("href");
// get the current position of the target element
var targetOffset = targetName.offset().top;
// send the page there
$("html, body").animate({
top: targetOffset
});
});
【问题讨论】:
标签: jquery jquery-animate offset var