JQuery 实现在同一页面锚点链接之间的平滑滚动

JavaScript06

JQuery 实现在同一页面锚点链接之间的平滑滚动,第1张

web开发前端一直用JQuery

,真正接触了才体会到,JQuery

原来比我想象的要强大的多,也可能比我体会到的还要强大的多,特别是兼容性那个好,于是把一些好玩的,酷炫的,可以代替

JS

的,统统给用上了。

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

}

}

})

})

还是再要强调一下加载的顺序,先引用JQuery

类库。顺便说一下,经测试,该滚动效果在各浏览器下都兼容适用,唯有在

Opera

下表现有点怪异,还有待改进。

主要思路是:鼠标当前点到下一点直接间隔计算出速度。这样就实现了惯性滑动效果。

下面是简单的js代码实现:仅供参考:

<style>    

#div1{ width:100px height:100px background:red position:absolute left:0px top:0}    

</style>    

<script>    

window.onload=function(){    

var oDiv=document.getElementById('div1')    

var iSpeedX=0    

var iSpeedY=0     

var lastX=0    

var lastY=0    

var timer=null     

oDiv.onmousedown=function(ev){    //div的鼠标按下事件,主要计算鼠标当前位置,和移动位置。这样可以计算出鼠标移动速度。

var oEvent=ev || event    

var disX=oEvent.clientX-oDiv.offsetLeft    

var disY=oEvent.clientY-oDiv.offsetTop      

clearInterval(timer)      

document.onmousemove=function(ev){   //鼠标拖动事件。 

var oEvent=ev || event     

oDiv.style.left=oEvent.clientX-disX+'px'    

oDiv.style.top=oEvent.clientY-disY+'px'    

iSpeedX=oEvent.clientX-lastX    

iSpeedY=oEvent.clientY-lastY     

lastX=oEvent.clientX    

lastY=oEvent.clientY

}    

document.onmouseup=function(){    //当鼠标抬起后,清掉移动事件。

document.onmousemove=null    

document.onmouseup=null

oDiv.releaseCapture && oDiv.releaseCapture()      

startMove()    

}    

oDiv.setCapture && oDiv.setCapture()    

return false

}         

function startMove(){    //移动函数,主要操作是计算鼠标移动速度和移动方向。

clearInterval(timer)    

timer=setInterval(function(){    

iSpeedY+=3    

var t=oDiv.offsetTop+iSpeedY    

var l=oDiv.offsetLeft+iSpeedX    

if(t>document.documentElement.clientHeight-oDiv.offsetHeight){    

t=document.documentElement.clientHeight-oDiv.offsetHeight    

iSpeedY*=-0.8    

iSpeedX*=0.8

}     

if(t<0){    

t=0    

iSpeedY*=-0.8    

iSpeedX*=0.8

}    

if(l>document.documentElement.clientWidth-oDiv.offsetWidth){    

l=document.documentElement.clientWidth-oDiv.offsetWidth

    

iSpeedX*=-0.8    

iSpeedY*=0.8    

}    

if(l<0){    

l=0    

iSpeedX*=-0.8    

iSpeedY*=0.8

    

}    

    

oDiv.style.left=l+'px'    

oDiv.style.top=t+'px'    

    

if(Math.abs(iSpeedX)<1)iSpeedX=0    

if(Math.abs(iSpeedY)<1)iSpeedY=0    

if(iSpeedX==0 && iSpeedY==0 && t==document.documentElement.clientHeight-oDiv.offsetHeight){    

clearInterval(timer)    

}    

document.title=i++    

},30)

}    

}    

</script>    

</head>    

<body>    

<div id="div1"></div>    

</body>