0

I am using both jQuery and Zepto. I want to conditionally invoke Zepto methods on a jQuery collection. Anyone have an eloquent solution? My code currently looks something like this:

// Obtain jQuery Object
var $selector = jQuery('.selector');

// Desktop
$selector.click( ... );

// Mobile
if (window.Zepto){
  $selector.slideLeft( ... );
}
Globalz
  • 4,474
  • 6
  • 34
  • 43
  • Why not integrate such Zepto methods into jQuery plugins? –  Mar 07 '13 at 01:22
  • Tried that... Can't do it without editing Zepto source... i.e. e.touches needs to become e.originalEvent.touches... etc etc – Globalz Mar 07 '13 at 21:59

1 Answers1

0

It is very brittle, but you can use Function.call to apply Zepto functions to jQuery objects. I did a proof-of-concept fiddle (http://jsfiddle.net/STnUQ/1/) and obviously it works for the simple css function.

var div = jQuery('.test');
Zepto().css.call(div, 'color', 'red');

So long as the underlying data structure of the jQuery object matches Zepto, it will work just fine. But this is not guaranteed (or even probable) and you should do extensive testing with your specific use.

chowey
  • 9,138
  • 6
  • 54
  • 84