File tree Expand file tree Collapse file tree 5 files changed +270
-179
lines changed
Expand file tree Collapse file tree 5 files changed +270
-179
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ <h1>Hello, this is an image viewer.</h1>
3636 container : 'body' ,
3737 enableScale : true ,
3838 startIndex : 0 ,
39+ loop : false ,
3940 headerRender : function ( ) {
4041 setTimeout ( function ( ) {
4142 document . getElementById ( 'close' ) . addEventListener ( 'click' , function ( ) {
@@ -56,15 +57,15 @@ <h1>Hello, this is an image viewer.</h1>
5657 swipeLastLeft : function ( imageViewer , distance ) {
5758 console . log ( 'swipeLastLeft' , distance ) ;
5859 if ( distance > 50 ) {
59- imageViewer . setImageOption ( [ 'images/4.jpg' ] ) ;
60- return true ;
60+ // imageViewer.setImageOption(['images/4.jpg']);
61+ // return true;
6162 }
6263 } ,
6364 swipeFirstRight : function ( imageViewer , distance ) {
6465 console . log ( 'swipeFirstRight' , distance ) ;
6566 if ( distance > 30 ) {
66- imageViewer . setImageOption ( [ 'images/5.jpg' , 'images/6.jpg' , 'images/7.jpg' ] ) ;
67- return true ;
67+ // imageViewer.setImageOption(['images/5.jpg', 'images/6.jpg', 'images/7.jpg']);
68+ // return true;
6869 }
6970 }
7071 } ) ;
Original file line number Diff line number Diff line change 1+ class Event {
2+ /**
3+ * 构造器
4+ * @param _enableMultiple 标记该事件实例是否允许同一事件挂载多个处理函数
5+ */
6+ constructor ( _enableMultiple = true ) {
7+ this . _enableMultiple = _enableMultiple ;
8+ this . _handlers = { } ;
9+ }
10+
11+ /**
12+ * 绑定事件处理函数
13+ * @param name 事件名字
14+ * @param handler 对应的事件处理函数
15+ * @param enableMultiple 标记该事件是否允许挂载多个处理函数
16+ */
17+ on ( name , handler , enableMultiple ) {
18+ enableMultiple = enableMultiple === undefined ? this . _enableMultiple : enableMultiple ;
19+ if ( enableMultiple ) {
20+ if ( ! this . _handlers [ name ] ) {
21+ this . _handlers [ name ] = [ ] ;
22+ }
23+ this . _handlers [ name ] . push ( handler ) ;
24+ } else {
25+ this . _handlers [ name ] = [ handler ] ;
26+ }
27+ }
28+
29+ /**
30+ * 销毁对应的处理函数
31+ * @param name 事件名
32+ */
33+ off ( name ) {
34+ delete this . _handlers [ name ] ;
35+ }
36+
37+ /**
38+ * 触发事件
39+ * @param name 事件名
40+ * @param args 参数数组,传递给各个事件处理函数
41+ */
42+ emit ( name , ...args ) {
43+ let handlers = this . _handlers [ name ] || [ ] , event = { stop : false } , length = handlers . length ;
44+ //传递给事件处理函数的第一个参数为事件对象
45+ //该对象拥有一些可能会有用的属性和函数(比如可以终止处理函数链的执行exit)
46+ args = args . concat ( [ {
47+ name : name , //事件名
48+ length : length , //事件处理函数的数量
49+ /**
50+ * 阻止继续执行函数处理链并退出
51+ */
52+ exit ( ) {
53+ event . stop = true ;
54+ }
55+ } ] ) ;
56+ if ( length ) {
57+ //仅当存在处理函数时才执行
58+ handlers . forEach ( ( handler ) => {
59+ ! event . stop && handler . apply ( this , args ) ;
60+ } ) ;
61+ }
62+ }
63+ }
64+
65+ export default Event ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ class Lock {
33 this . _locks = { } ;
44 }
55
6- addLock ( name ) {
6+ createLock ( name ) {
77 this . _locks [ name ] = false ;
88 }
99
You can’t perform that action at this time.
0 commit comments