jquery.switch.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. !function ($) {
  2. "use strict";
  3. $.fn.switch = function (method) {
  4. var methods = {
  5. init:function () {
  6. this.each(function () {
  7. var $element = $(this)
  8. , $div
  9. , $switchLeft
  10. , $switchRight
  11. , $label
  12. , myClasses = ""
  13. , classes = $element.attr('class')
  14. , color;
  15. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  16. if (classes.indexOf(el) >= 0)
  17. myClasses = el;
  18. });
  19. if ($element.data('on') !== undefined)
  20. color = "switch-" + $element.data('on');
  21. $switchLeft = $('<span>')
  22. .addClass("switch-left")
  23. .addClass(myClasses)
  24. .addClass(color)
  25. .text("ON");
  26. if ($element.data('off') !== undefined)
  27. color = "switch-" + $element.data('off');
  28. $switchRight = $('<span>')
  29. .addClass("switch-right")
  30. .addClass(myClasses)
  31. .addClass(color)
  32. .text("OFF");
  33. $label = $('<label>')
  34. .html("&nbsp;")
  35. .addClass(myClasses)
  36. .attr('for', $element.find('input').attr('id'));
  37. $div = $element.find('input').wrap($('<div>')).parent().data('animated', false);
  38. if ($element.data('animated') !== false)
  39. $div.addClass('switch-animate').data('animated', true);
  40. $div.append($switchLeft);
  41. $div.append($label);
  42. $div.append($switchRight);
  43. $element.find('>div').addClass(
  44. $element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
  45. );
  46. if ($element.find('input').is(':disabled'))
  47. $(this).addClass('deactivate');
  48. var changeStatus = function ($this) {
  49. $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
  50. };
  51. $switchLeft.on('click', function (e) {
  52. changeStatus($(this));
  53. });
  54. $switchRight.on('click', function (e) {
  55. changeStatus($(this));
  56. });
  57. $element.find('input').on('change', function (e) {
  58. var $element = $(this).parent();
  59. e.preventDefault();
  60. e.stopImmediatePropagation();
  61. $element.css('left', '');
  62. if ($(this).is(':checked'))
  63. $element.removeClass('switch-off').addClass('switch-on');
  64. else $element.removeClass('switch-on').addClass('switch-off');
  65. if ($element.data('animated') !== false)
  66. $element.addClass("switch-animate");
  67. $element.parent().trigger('switch-change', {'el':$(this), 'value':$(this).is(':checked')})
  68. });
  69. $element.find('label').on('mousedown touchstart', function (e) {
  70. var $this = $(this)
  71. , moving = false;
  72. e.preventDefault();
  73. e.stopImmediatePropagation();
  74. $this.closest('div').removeClass('switch-animate');
  75. if ($this.closest('.switch').is('.deactivate'))
  76. $this.unbind('click');
  77. else {
  78. $this.on('mousemove touchmove', function (e) {
  79. var $element = $(this).closest('.switch')
  80. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  81. , percent = (relativeX / $element.width()) * 100
  82. , left = 25
  83. , right = 75;
  84. moving = true;
  85. if (percent < left)
  86. percent = left;
  87. else if (percent > right)
  88. percent = right;
  89. $element.find('>div').css('left', (percent - right) + "%")
  90. });
  91. $this.on('click touchend', function (e) {
  92. var $this = $(this)
  93. , $target = $(e.target)
  94. , $myCheckBox = $target.siblings('input');
  95. e.stopImmediatePropagation();
  96. e.preventDefault();
  97. $this.unbind('mouseleave');
  98. if (moving)
  99. $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
  100. else $myCheckBox.prop("checked", !$myCheckBox.is(":checked"));
  101. moving = false;
  102. $myCheckBox.trigger('change');
  103. });
  104. $this.on('mouseleave', function (e) {
  105. var $this = $(this)
  106. , $myCheckBox = $this.siblings('input');
  107. e.preventDefault();
  108. e.stopImmediatePropagation();
  109. $this.unbind('mouseleave');
  110. $this.trigger('mouseup');
  111. $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  112. });
  113. $this.on('mouseup', function (e) {
  114. e.stopImmediatePropagation();
  115. e.preventDefault();
  116. $(this).unbind('mousemove');
  117. });
  118. }
  119. });
  120. }
  121. );
  122. },
  123. toggleActivation:function () {
  124. $(this).toggleClass('deactivate');
  125. },
  126. toggleState:function (skipOnChange) {
  127. var $input = $(this).find('input:checkbox');
  128. $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  129. },
  130. setState:function (value, skipOnChange) {
  131. $(this).find('input:checkbox').prop('checked', value).trigger('change', skipOnChange);
  132. },
  133. status:function () {
  134. return $(this).find('input:checkbox').is(':checked');
  135. },
  136. destroy:function () {
  137. var $div = $(this).find('div')
  138. , $checkbox;
  139. $div.find(':not(input:checkbox)').remove();
  140. $checkbox = $div.children();
  141. $checkbox.unwrap().unwrap();
  142. $checkbox.unbind('change');
  143. return $checkbox;
  144. }
  145. };
  146. if (methods[method])
  147. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  148. else if (typeof method === 'object' || !method)
  149. return methods.init.apply(this, arguments);
  150. else
  151. $.error('Method ' + method + ' does not exist!');
  152. };
  153. }(jQuery);
  154. $(function () {
  155. $('.switch').switch();
  156. });