正常的本页面锚连接跳转的时候跟PPT似的,特别生硬,用户体验很是差。jquery
这时候咱们就能够借助smooth-scroll.js这个插件,来实现本页面的平滑的跳转。函数
<script src="js/jquery-1.10.2.js"></script> <script src="js/jquery.smooth-scroll.min.js"></script> <script src="js/jquery.ba-bbq.js"></script>
$('a').smoothScroll({});
$('#container a').smoothScroll();
$('#container a').smoothScroll({ excludeWithin: ['.container2'] });
$('a').smoothScroll({ exclude: ['.rough','#chunky'] });
$('.backtotop').smoothScroll({ offset: -100 });
$('a').smoothScroll({ beforeScroll: function() { alert('ready to go!'); } });
$('a').smoothScroll({ afterScroll: function() { alert('we made it!'); } });
$.smoothScroll({ //滑动到的位置的偏移量 offset: 0, //滑动的方向,可取 'top' 或 'left' direction: 'top', // 只有当你想重写默认行为的时候才会用到 scrollTarget: null, // 滑动开始前的回调函数。`this` 表明正在被滚动的元素 beforeScroll: function() {}, //滑动完成后的回调函数。 `this` 表明触发滑动的元素 afterScroll: function() {}, //缓动效果 easing: 'swing', //滑动的速度 speed: 400, // "自动" 加速的系数 autoCoefficent: 2 });
什么?没看懂??不要紧!!下边还有一波无脑操做!this
只须要把文件导入后,将下边的代码copy进去就能够了,即可轻松实现页面中的平滑滚动spa
<script> $(document) .on('click', 'a[href*="#"]', function() { if ( this.hash ) { $.bbq.pushState( '#/' + this.hash.slice(1) ); return false; } }) .ready(function() { $(window).bind('hashchange', function(event) { var tgt = location.hash.replace(/^#\/?/,''); if ( document.getElementById(tgt) ) { $.smoothScroll({scrollTarget: '#' + tgt}); } }); $(window).trigger('hashchange'); }); </script>
下面就是点击technolog滑动到 id为a1的div区,简单吧!插件
<a href="#a1">Technology</a> <div id="a1"> </div>
smooth-scroll.js的下载地址?网上好多好多,找不到正确的?留言邮箱啊,博主服务一条龙。。。code