function Dropdown(elem,options),options有问题

来源:3-3 用构造函数的形式重写dropdown模块--触发方式和延迟显示

慕粉1905369247

2020-04-07 12:04:05


(function($){


function Dropdown(elem,options){

this.$elem=$(elem)

this.options=options

this.$layer=this.$elem.find('.dropdown-layer')

this.activeClass=this.$elem.data('active')+'-active';

this.$layer.showHide(options)

var self=this;

if(Dropdown.DEFAULTS.event==='click'){

this.$elem.on('click',function(){

self.show();

// 阻止冒泡

e.stopPropagation();

});

$(document).on('click',$.proxy(this.hide,this))

}else{

this.$elem.hover($.proxy(this.show,this),$.proxy(this.hide,this))

}

console.log(Dropdown.DEFAULTS.event)

}

Dropdown.DEFAULTS={

event:'hover',

css3:false,

js:false,

animation:'fade',

delay:0,

active:'dropdown'

}

Dropdown.prototype.show=function(){

this.timer=setTimeout(function(){

this.$elem.addClass(this.activeClass);

this.$layer.showHide('show')

},0)

}

Dropdown.prototype.hide=function(){

this.$elem.removeClass(this.activeClass);

this.$layer.showHide('hide')

}

var dropdown=new Dropdown();

dropdown.show();

dropdown.hide();

'use strict';

$.fn.extend({

dropdown:function(option){

//返回时遍历所有元素

return this.each(function(){

var options=$.extend({},Dropdown.DEFAULTS,option)

new Dropdown(this,option)

})

}

})


})(jQuery)


写回答

1回答

好帮手慕星星

2020-04-07

同学你好,不是options的问题,是show方法中的问题。定时器中的this指向的window,并不是dropdown对象,所以不能和获取this.$elem,this.$layer等元素。需要提前保存this指向,参考修改:

http://img.mukewang.com/climg/5e8c385c091727bd09430590.jpg

自己再测试下,祝学习愉快!

0

0 学习 · 14456 问题

查看课程