用jquery实现平滑的页面滚动效果
通过几句jquery代码实现页面平滑滚动到某一锚点的效果。实现代码来源于 https://css-tricks.com/snippets/jquery/smooth-scrolling 实现的jquery代码如下: $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); 这里我们就先了解一下location对象及它的一些属性: location对象属于window对象,包含有关当前url的信息。 location对象的属性: location.href:返回当前页的完整的url;