switchery.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*!
  2. * Switchery jQuery Plugin - Copyright (c) 2016 Foxteam
  3. * Dual-licensed under the BSD or MIT licenses
  4. */
  5. ;(function()
  6. {
  7. var Switchery = function(elem, params){
  8. var obj = $(elem);
  9. obj.hide();
  10. var small = obj.hasClass('small');
  11. var checked = elem.checked;
  12. var switchery = $('<div class="switchery ' + (small?"switchery-small":"") +'"><small></small></div>');
  13. obj.after(switchery);
  14. if(checked){
  15. switchery.addClass('checked');
  16. }
  17. switchery.click(function(e){
  18. switchery.toggleClass('checked');
  19. obj.click && obj.trigger('click',e);
  20. });
  21. return switchery;
  22. }
  23. $.fn.switchery = function(params)
  24. {
  25. var lists = this,
  26. retval = this;
  27. lists.each(function()
  28. {
  29. var plugin = $(this).data("switchery");
  30. if (!plugin) {
  31. $(this).data("switchery", new Switchery(this, params));
  32. $(this).data("switchery-id", new Date().getTime());
  33. } else {
  34. if (typeof params === 'string' && typeof plugin[params] === 'function') {
  35. retval = plugin[params]();
  36. }
  37. }
  38. });
  39. return retval || lists;
  40. };
  41. })();