页面向上向下滚动,分享到的模块随着滑动。
要点:
代码如下:
var scrtop =document.documentElement.scrollTop||document.body.scrollTop
var height = document.documentElement.clientHeight||document.body.clientHeight
var top = scrtop + (height - jb51.offsetHeight)/2
top = parseInt(top)
获得页面垂直居中的位置
上代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>无标题文档</title>
<style>
body{margin:0padding:0font:12px/1.5 arialheight:2000px}
#jb51{width:100pxheight:200pxline-height:200px
text-align:centerborder:1p solid #ccc
background:#f5f5f5position:absoluteleft:-100pxtop:0}
#jb51_tit{position:absoluteright:-20pxtop:60px
width:20pxheight:60pxpadding:10px 0
background:#06ctext-align:center
line-height:18pxcolor:#fff}
</style>
<script>
window.onload = function(){
var jb51 = document.getElementById("jb51")
jb51.onmouseover = function(){
startrun(jb51,0,"left")
}
jb51.onmouseout = function(){
startrun(jb51,-100,"left")
}
window.onscroll = window.onresize = function(){
var scrtop=document.documentElement.scrollTop||document.body.scrollTop
var height=document.documentElement.clientHeight||document.body.clientHeight
var top = scrtop + (height - jb51.offsetHeight)/2
top = parseInt(top)
startrun(jb51,top,"top")
}
}
var timer = null
function startrun(obj,target,direction){
clearInterval(timer)
timer = setInterval(function(){
var speed = 0
if(direction == "left"){
speed = (target-obj.offsetLeft)/8
speed = speed>0?Math.ceil(speed):Math.floor(speed)
if(obj.offsetLeft == target){
clearInterval(timer)
}else{
obj.style.left = obj.offsetLeft + speed + "px"
}
}
if(direction == "top"){
speed = (target-obj.offsetTop)/8
speed = speed>0?Math.ceil(speed):Math.floor(speed)
if(obj.offsetTop == target){
clearInterval(timer)
}else{
obj.style.top = obj.offsetTop + speed + "px"
}
document.title = obj.offsetTop + ',' + target + ',' +speed
}
},30)
}
</script>
</head>
<body>
<div id="jb51">
分享到内容
<span id="jb51_tit">分享到</span>
</div>
</body>
</html>
主要思路是:鼠标当前点到下一点直接间隔计算出速度。这样就实现了惯性滑动效果。下面是简单的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>