Permalink
Browse files

optimize `toggle` methods: code style, less objects

 - manageable line sizes
 - cache result of `$(this)` so less objects are created

References #475
  • Loading branch information...
1 parent 023c6a8 commit b745a193ba8dfe654d93c2978f18469893340552 @mislav mislav committed Sep 8, 2012
Showing with 14 additions and 5 deletions.
  1. +10 −4 src/fx_methods.js
  2. +4 −1 src/zepto.js
View
@@ -36,8 +36,12 @@
}
$.fn.toggle = function(speed, callback) {
- if (speed === undefined || typeof speed == 'boolean') return origToggle.call(this, speed)
- else return this.each(function(){ $(this)[$(this).css('display') == 'none' ? 'show' : 'hide'](speed, callback) })
+ if (speed === undefined || typeof speed == 'boolean')
+ return origToggle.call(this, speed)
+ else return this.each(function(){
+ var el = $(this)
+ el[el.css('display') == 'none' ? 'show' : 'hide'](speed, callback)
+ })
}
$.fn.fadeTo = function(speed, opacity, callback) {
@@ -57,8 +61,10 @@
$.fn.fadeToggle = function(speed, callback) {
return this.each(function(){
- var hidden = $(this).css('opacity') == 0 || $(this).css('display') == 'none'
- $(this)[hidden ? 'fadeIn' : 'fadeOut'](speed, callback)
+ var el = $(this)
+ el[
+ (el.css('opacity') == 0 || el.css('display') == 'none') ? 'fadeIn' : 'fadeOut'
+ ](speed, callback)
})
}
View
@@ -409,7 +409,10 @@ var Zepto = (function() {
return this.css("display", "none")
},
toggle: function(setting){
- return this.each(function(){ (setting === undefined ? $(this).css("display") == "none" : setting) ? $(this).show() : $(this).hide() })
+ return this.each(function(){
+ var el = $(this)
+ ;(setting === undefined ? el.css("display") == "none" : setting) ? el.show() : el.hide()
+ })
},
prev: function(selector){ return $(this.pluck('previousElementSibling')).filter(selector || '*') },
next: function(selector){ return $(this.pluck('nextElementSibling')).filter(selector || '*') },

0 comments on commit b745a19

Please sign in to comment.