标签:length 改进 host 代码实现 article asc 兼容性 html nbsp
从 JQuery 引入今天的正题。用 JQuery 实现锚点链接之间的平滑滚动。曾经介绍过一个用 JS 实现的页面锚点跳转缓冲特效,效果相当不错。能够在同一页面的锚点链接之间实现平滑的滚动,可是 JS 代码相对来说比較冗长。如今好了。仅仅要已经载入了 JQuery。我们就能够用较为简短的代码实现同样的效果。
用法例如以下:
1、加载 JQuery 库。
2、关键代码:
$(document).ready(function() { $(‘a[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) { var targetOffset = $target.offset().top; $(‘html,body‘).animate({ scrollTop: targetOffset }, 1000); return false; } } }); });
标签:length 改进 host 代码实现 article asc 兼容性 html nbsp
原文地址:http://www.cnblogs.com/wgwyanfs/p/6821813.html