您应该首先修复锚点并使用散列片段来允许锚点之间的本地导航。
我创建了一个非常简单的演示让您理解这一点(不使用您的标记来保持简单)。
演示:http://jsfiddle.net/abhitalks/9uxGq/15/
(另一个带有您的标记的演示:http://jsfiddle.net/abhitalks/9uxGq/19/)
您需要两个锚点,一个作为点击链接,另一个将目标位置标记为锚点。
例如:
<div>
<a id="LearnMore1"></a> <!-- Used for marking the anchor with hash fragment -->
<h2>Sub Heading 2</h2>
<p>
Some text content here
</p>
<a href="#LearnMore2">Learn More</a> <!-- Used to click to got to next anchor -->
</div>
注意:当然,您可以使用div(或者在您的情况下为section)和@,而不是使用第二个锚点作为标记987654327@。但是,a 更好,因为它对内容导航更具语义,并且意味着 anchor。
一旦完成,这将成为您的后备方案。现在您可以使用 jQuery 等轻松实现动画了。
就这么简单:
// bind click on anchors (use selectors as per your requirements)
$("a").on("click", function(e) {
e.preventDefault(); // prevent the default behaviour
var nextAnchor = this.hash.replace("#", ""); // get the next marker anchor
var gotoPoint = $("#" + nextAnchor).position().top; // get the position of marker
$('body').animate({ scrollTop: gotoPoint }, 'normal'); // animate the body
});
或者:
// bind click on anchors (use selectors as per your requirements)
$("a").on("click", function(e) {
e.preventDefault(); // prevent the default behaviour
var nextAnchor = $(this).attr('href'); // get the next marker anchor
var gotoPoint = $(nextAnchor).position().top; // get the position of marker
$('body').animate({ scrollTop: gotoPoint }, 'normal'); // animate the body
});
现在将它应用到您的用例中,演示变为:http://jsfiddle.net/abhitalks/9uxGq/19/
希望对您有所帮助,您可以在标记和用例中解决它。
.