jquery.switch.js 6.4 KB

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