jquery.switch.js 6.7 KB

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