我想在javascript中重现onfling事件 我知道我必须计算px /时间,但我不知道我需要什么时间参考。
答案 0 :(得分:0)
虽然此代码不符合预期目的 它确实演示了如何“计算px /时间”
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fling Me</title>
<style>
div{
position:absolute;
top:300px;
left:300px;
width:100px;
height:100px;
background-color:red;
}
</style>
</head>
<body>
republicans must abandon trump or be complicit in his advocacy of assassinating a sitting president
<div></div>
<script>
'use strict';
(function(){
var mm,ox,oy,el,t,l,tt=0,tl=0,dt=0,dl=0,tmr,s;
document.querySelector('div').addEventListener('mousedown',
function(ev){
el = ev.target;
s = el.style ;
ox=ev.offsetX; oy=ev.offsetY;
t=ev.clientY;l=ev.clientX;
tmr = setInterval(function(){
dt = tt - t;
dl = tl - l;
tt = t;
tl = l;
},30);
el.addEventListener('mousemove',
mm = function(ev){
t=ev.clientY;l=ev.clientX;
s.top = t - ox + 'px';
s.left = l - oy + 'px';
}
);
el.addEventListener('mouseup',
function(ev){
clearInterval(tmr);
el.removeEventListener('mousemove',mm);
tmr = setInterval(function(){
s.top = parseInt(s.top) - dt + 'px';
s.left = parseInt(s.left) - dl + 'px';
},30);
setTimeout("location.assign(location.href)",2000);
}
);
}
)})();
</script>
</body>
</html>