MooTools toggle(fn,fn)的简单实现方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Element.implement({ toggle: function(event, fn, fn2){ var flag = true; return this.addEvent(event, function(){ (flag ? fn : fn2).apply(this, arguments); flag = !flag; }); } }); window.addEvent('domready',function(){ $$('button')[0].toggle('click', function(event){ console.log(event, 'one') }, function(event){ console.log(event, 'two') } ).toggle('mouseenter', function(){ console.log('mousenter one') }, function(){ console.log('mousenter two') } ); |
类似于jQuery toggle(fn,fn)函数的作用。
每次点击后依次调用函数。
如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数,如果有更多函数,则再次触发,直到最后一个。随后的每次点击都重复对这几个函数的轮番调用。
顶!