js如何实现惯性滑动效果

JavaScript016

js如何实现惯性滑动效果,第1张

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

下面是简单的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>

可以给你的webView的scrollView属性指定delegate然后实现scrollViewDidEndDecelerating方法来监听惯性滚动停止事件,这个时候获取scrollView的滚动位置,然后通过js传到网页中

行车中发动机突然熄火怎样处置?这个问题其实是驾照考试里面的一道题目,标准答案是:缓慢减速停车。

因为在行驶过程中,发动机的突然熄火并不会立马使车辆停止,惯性的作用会使车辆继续往前行驶一段距离。此时不要慌张,握紧方向盘,打开双闪,打开右转向灯,确定无后车跟随后缓慢靠边停车,并且在车后150米后放置安全警示牌。尝试重新启动汽车,如果无法启动,接着打电话给专业的维修店或者交通警察进行处理。

行车中发动机突然熄火的原因:

1、油路故障。油管破裂,汽油泵罢工,燃油消耗殆尽,进油口堵塞等。

2、电路故障。低压线路短路造成电路的断电,检查点火开关,起动机有无故障,还有保险盒的保险丝断路问题。

3、点火元器件故障。点火线圈、点火模块,火花塞等元件存在故障,导致发动机能启动,过一会儿就会熄火,然后又能启动,之后又熄火。

4、发动机皮带的错位或断裂。皮带经过长时间的使用会老化,同时也会松动,从而导致的断裂和错位会使发动机熄火。

5、驾驶技巧不娴熟。自动挡的汽车一般不会出现熄火的现象,手动挡的车型则需要油离配合的技巧。有时候汽车速度下降了,车主没有换挡意识,还继续停留在高档位而不换回低档位,就会将车子憋熄火。

特别需要注意,车子在行车途中熄火了,不要因为过于紧张而轻易的制动,这样可能把惯性能量浪费掉,而错失靠边停车的机会。在高速路上,将车子停在路中间,这样的行为非常危险。