jQuery – 捲軸滾動到指定的錨點

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
 $("a").click(function() {
 console.log($(this).attr("href"))

 var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');

 $body.animate({
 scrollTop: $($(this).attr("href")).offset().top + "px"
 }, {
 duration: 500,
 easing: "swing"
 });

 return false;
 });

});
</script>
<div>
 <a href="#footer">HELLOW</a>

 <div id="footer" style="margin-top:10000px;"> FOOTER </div>
</div>

發表迴響