1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Title</ title >
6+ < style >
7+ * {
8+ margin : 0 ;
9+ padding : 0 ;
10+ }
11+ # div {
12+ width : 200px ;
13+ height : 200px ;
14+ background : red;
15+ position : absolute;
16+ }
17+ div img {
18+ width : 100% ;
19+ height : 100% ;
20+ display : block;
21+ }
22+ </ style >
23+ </ head >
24+ < body >
25+ < div id ="div "> < img src ="img/1.png " alt =""> </ div >
26+ < script src ="js/event.js "> </ script >
27+ < script >
28+ var oDiv = document . getElementById ( 'div' ) ;
29+ var flg = 0 ;
30+ on ( oDiv , 'mousedown' , down ) ;
31+ //功能:改变函数中的this指向;
32+ function processThis ( fn , context ) {
33+ return function ( e ) {
34+ fn . call ( context , e )
35+ }
36+ }
37+ function down ( e ) {
38+ this . x = this . offsetLeft ;
39+ this . y = this . offsetTop ;
40+ this . mx = e . clientX ;
41+ this . my = e . clientY ;
42+ this . MOVE = processThis ( move , this ) ;
43+ this . UP = processThis ( up , this ) ;
44+ if ( this . setCapture ) { //IE浏览器
45+ this . setCapture ( ) ; //捕获焦点
46+ on ( this , 'mousemove' , move ) ;
47+ on ( this , 'mouseup' , up ) ;
48+ } else { //标准浏览器
49+ on ( document , 'mousemove' , this . MOVE ) ;
50+ on ( document , 'mouseup' , this . UP ) ;
51+ e . preventDefault ( ) ;
52+ }
53+ clearTimeout ( this . flyTimer ) ;
54+ clearTimeout ( this . dropTimer ) ;
55+ }
56+ function move ( e ) {
57+ //浏览器每隔一段事件,捕获一次mousemove事件,所以,我们在这段事件中,走的距离越远;速度就越快;所以,可以把距离作为速度;
58+ this . style . left = e . clientX - this . mx + this . x + 'px' ;
59+ this . style . top = e . clientY - this . my + this . y + 'px' ;
60+
61+ if ( ! this . prevSpeed ) {
62+ this . prevSpeed = e . clientX ; //当没有上一次落脚点的时候,把e.clientX作为上一次的落脚点
63+ } else {
64+ this . speedX = e . clientX - this . prevSpeed ; //本次鼠标落脚点-上次鼠标落脚点
65+ this . prevSpeed = e . clientX ;
66+ }
67+ }
68+ function up ( ) {
69+ if ( this . releaseCapture ) {
70+ this . releaseCapture ( ) ;
71+ off ( this , 'mousemove' , move ) ;
72+ off ( this , 'mouseup' , up ) ;
73+ } else {
74+ off ( document , 'mousemove' , this . MOVE ) ;
75+ off ( document , 'mouseup' , this . UP ) ;
76+ }
77+ fly . call ( this ) ;
78+ drop . call ( this ) ;
79+ }
80+ function fly ( ) {
81+ clearTimeout ( this . flyTimer ) ;
82+ this . speedX *= .93 ; //加上摩擦效果
83+ var l = this . offsetLeft + this . speedX ;
84+ var maxL = ( document . documentElement . clientWidth || document . body . clientWidth ) - this . offsetWidth ;
85+ //边界值判断
86+ if ( l > maxL ) {
87+ l = maxL ;
88+ this . speedX *= - 1 ; //碰撞后反弹
89+ } else if ( l < 0 ) {
90+ l = 0 ;
91+ this . speedX *= - 1 ;
92+ }
93+ this . style . left = l + 'px' ;
94+ if ( Math . abs ( this . speedX ) >= 0.5 ) { //运动的条件;
95+ this . flyTimer = setTimeout ( processThis ( fly , this ) , 20 ) ;
96+ }
97+ //console.log(this.speedX)// 测试运动用的;
98+
99+ }
100+ function drop ( ) {
101+ clearTimeout ( this . dropTimer )
102+ if ( ! this . speedY ) {
103+ this . speedY = 9.8 ;
104+ } else {
105+ this . speedY += 9.8 ;
106+ }
107+ var t = this . offsetTop + this . speedY ;
108+ this . speedY *= .98 ;
109+ var maxT = ( document . documentElement . clientHeight || document . body . clientHeight ) - this . offsetHeight ;
110+ if ( t > maxT ) { //触底
111+ t = maxT ;
112+ this . speedY *= - 1 ;
113+ flg ++ ;
114+ } else {
115+ flg = 0 ;
116+ }
117+ this . style . top = t + 'px' ;
118+ if ( flg < 2 ) {
119+ this . dropTimer = setTimeout ( processThis ( drop , this ) , 20 ) ;
120+ }
121+ } ;
122+
123+
124+
125+ </ script >
126+ </ body >
127+ </ html >
0 commit comments