为什么老师说 delay 是 options 的 没有听懂 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | ( function ($) { 'use strict' function Dropdown(elem, options) { this .$elem = $(elem); this .options = options; this .$layer = this .$elem.find( '.dropdown-layer' ); this .activeClass = options.active + '-active' ; this .$layer.showHide(options); var self = this ; if (options.event === 'click' ) { this .$elem.on( 'click' , function (e) { self.show(); e.stopPropagation(); }); $(document).on( 'click' , $.proxy( this .hide, this )); } else { this .$elem.hover($.proxy( this .show, this ), $.proxy( this .hide, this )); } }; Dropdown.DEFAULTS = { event: "hover" , css3: true , js: true , animation: 'fade' , delay: 1000, active: 'dropdown' }; //作业部分代码修改 // this.$elem.addClass(this.activeClass); // this.$layer.showHide('show'); Dropdown.prototype.show = function () { var self = this ; if ( this .options.delay) { this .timer = setTimeout( function () { _show(); }, this .options.delay); } else { _show(); } function _show() { self.$elem.addClass(self.activeClass); self.$layer.showHide( 'show' ); } } Dropdown.prototype.hide = function () { if ( this .options.delay) { clearTimeout( this .timer); } this .$elem.removeClass( this .activeClass); this .$layer.showHide( 'hide' ); }; // var dropdown = new Dropdown(); // var dropdown2 = new Dropdown(); // var dropdown3 = new Dropdown(); // dropdown.show(); // dropdown.hide(); // function dropdown(elem, options) { // var $elem = $(elem), // $layer = $elem.find('.dropdown-layer'), // activeClass = $elem.data('active') + '-active'; // // $layer.showHide({ // // css3: true, // // js: true, // // animation: 'slideUpDown' // // }); //在这里传参,不方便,易于出错,应该在调用时传参。 // $layer.showHide(options); // $elem.hover(function() { // $elem.addClass(activeClass); // $layer.showHide('show'); // }, function() { // $elem.removeClass(activeClass); // $layer.showHide('hide'); // }); // } // var defaults = { // css3: true, // js: true, // animation: 'slideUpDown' // }; $.fn.extend({ dropdown: function (option) { return this .each( function () { var options = $.extend({}, Dropdown.DEFAULTS, $( this ).data(), option); new Dropdown( this , options); }); } }); })(jQuery); |
不懂 this.options = options; 这行代码
为什么这么写了 下面这些代码就能生效 delay 就定义了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Dropdown.prototype.show = function () { var self = this ; if ( this .options.delay) { this .timer = setTimeout( function () { _show(); }, this .options.delay); } else { _show(); } self.$elem.addClass(self.activeClass); self.$layer.showHide( 'show' ); function _show() { self.$elem.addClass(self.activeClass); self.$layer.showHide( 'show' ); } }; Dropdown.prototype.hide = function () { if ( this .options.delay) { clearTimeout( this .timer); }; this .$elem.removeClass( this .activeClass); this .$layer.showHide( 'hide' ); }; |
18
收起
正在回答
1回答
同学你好,this.options = options;写这句可以理解为:把options绑定到实例化对象上。
可以打印看下:
控制台:
如果不写的话,那么控制台打印结果如下:
没有options是读取不到delay,不能实现效果。
如果我的回答帮到了你,欢迎采纳,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧