做页面制作也有两年了,其中也做过许多页面效果,有简单的,也有复杂的,今天就来分享一个导航滚动到内容的特效。
平时我们做导航滚动到内容都是通过锚点来做,刷的一下就直接跳到内容了,没有一丝的滚动效果,而且 url 链接最后会有“小尾巴”,今天我就介绍一款 jquery 做的滚动的特效,既可以设置滚动速度,又可以在 url 链接上没有“小尾巴”。
html:
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> #a{height:800px;background:red;} #b{height:800px;background:green;} #c{height:800px;background:black;} #d{height:800px;background:yellow;} </style> </head> <body> <div class="link"> <a href="javascript:;" onclick="scroll('a')">1111111111</a> <a href="javascript:;" onclick="scroll('b')">2222222222</a> <a href="javascript:;" onclick="scroll('c')">3333333333</a> <a href="javascript:;" onclick="scroll('d')">4444444444</a> </div> <div id="a"></div> <div id="b"></div> <div id="c"></div> <div id="d"></div> </body> </html> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="jquery.scrollto.js"></script> <script type="text/javascript"> function scroll(id){ $("#"+id).ScrollTo(1000); } </script>
jquery.scrollto.js:
jQuery.getPos = function (e) { var l = 0; var t = 0; var w = jQuery.intval(jQuery.css(e,'width')); var h = jQuery.intval(jQuery.css(e,'height')); var wb = e.offsetWidth; var hb = e.offsetHeight; while (e.offsetParent){ l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0); t += e.offsetTop + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0); e = e.offsetParent; } l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0); t += e.offsetTop + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0); return {x:l, y:t, w:w, h:h, wb:wb, hb:hb}; }; jQuery.getClient = function(e) { if (e) { w = e.clientWidth; h = e.clientHeight; } else { w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth; h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight; } return {w:w,h:h}; }; jQuery.getScroll = function (e) { if (e) { t = e.scrollTop; l = e.scrollLeft; w = e.scrollWidth; h = e.scrollHeight; } else { if (document.documentElement && document.documentElement.scrollTop) { t = document.documentElement.scrollTop; l = document.documentElement.scrollLeft; w = document.documentElement.scrollWidth; h = document.documentElement.scrollHeight; } else if (document.body) { t = document.body.scrollTop; l = document.body.scrollLeft; w = document.body.scrollWidth; h = document.body.scrollHeight; } } return { t: t, l: l, w: w, h: h }; }; jQuery.intval = function (v) { v = parseInt(v); return isNaN(v) ? 0 : v; }; jQuery.fn.ScrollTo = function(s) { o = jQuery.speed(s); return this.each(function(){ new jQuery.fx.ScrollTo(this, o); }); }; jQuery.fx.ScrollTo = function (e, o) { var z = this; z.o = o; z.e = e; z.p = jQuery.getPos(e); z.s = jQuery.getScroll(); z.clear = function(){clearInterval(z.timer);z.timer=null}; z.t=(new Date).getTime(); z.step = function(){ var t = (new Date).getTime(); var p = (t - z.t) / z.o.duration; if (t >= z.o.duration+z.t) { z.clear(); setTimeout(function(){z.scroll(z.p.y, z.p.x)},13); } else { st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t; sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l; z.scroll(st, sl); } }; z.scroll = function (t, l){window.scrollTo(l, t)}; z.timer=setInterval(function(){z.step();},13); };
调用方法:
$(id).ScrollTo(speed);//id是跳转到内容的id;speed是滚动速度,值越大,滚动越慢