jquery.switch.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. !function ($) {
  2. "use strict";
  3. $.fn.switch = function () {
  4. this.each(function () {
  5. var $element = $(this)
  6. , $div
  7. , $switchLeft
  8. , $switchRight
  9. , $label
  10. , myClasses = ""
  11. , classes = $element.attr('class')
  12. , color;
  13. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  14. if (classes.indexOf(el) >= 0)
  15. myClasses = el;
  16. });
  17. if ($element.data('on') !== undefined)
  18. color = "switch-" + $element.data('on');
  19. $switchLeft = $('<span>')
  20. .addClass("switch-left")
  21. .addClass(myClasses)
  22. .addClass(color)
  23. .text("ON");
  24. if ($element.data('off') !== undefined)
  25. color = "switch-" + $element.data('off');
  26. $switchRight = $('<span>')
  27. .addClass("switch-right")
  28. .addClass(myClasses)
  29. .addClass(color)
  30. .text("OFF");
  31. $label = $('<label>')
  32. .html("&nbsp;")
  33. .addClass(myClasses)
  34. .attr('for', $element.find('input').attr('id'));
  35. $div = $element.find('input').wrap($('<div>')).parent().data('animated', false);
  36. if($element.data('animated') !== false)
  37. $div.addClass('switch-animate').data('animated', true);
  38. $div.append($switchLeft);
  39. $div.append($label);
  40. $div.append($switchRight);
  41. $element.find('>div').addClass(
  42. $element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
  43. );
  44. if ($element.find('input').is(':disabled'))
  45. $(this).addClass('deactivate');
  46. var changeStatus = function ($this) {
  47. $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
  48. };
  49. $switchLeft.on('click', function (e) {
  50. changeStatus($(this));
  51. });
  52. $switchRight.on('click', function (e) {
  53. changeStatus($(this));
  54. });
  55. $element.find('input').on('change', function (e) {
  56. var $element = $(this).parent();
  57. e.preventDefault();
  58. e.stopImmediatePropagation();
  59. $element.css('left', '');
  60. if ($(this).is(':checked'))
  61. $element.removeClass('switch-off').addClass('switch-on');
  62. else $element.removeClass('switch-on').addClass('switch-off');
  63. if($element.data('animated') !== false)
  64. $element.addClass("switch-animate");
  65. $element.parent().trigger('switch-change', {'el': $(this), 'value': $(this).is(':checked')})
  66. });
  67. $element.find('label').on('mousedown touchstart', function (e) {
  68. var $this = $(this)
  69. , moving = false;
  70. e.preventDefault();
  71. e.stopImmediatePropagation();
  72. $this.closest('div').removeClass('switch-animate');
  73. if ($this.closest('.switch').is('.deactivate'))
  74. $this.unbind('click');
  75. else {
  76. $this.on('mousemove touchmove', function (e) {
  77. var $element = $(this).closest('.switch')
  78. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  79. , percent = (relativeX / $element.width()) * 100
  80. , left = 25
  81. , right = 75;
  82. moving = true;
  83. if (percent < left)
  84. percent = left;
  85. else if (percent > right)
  86. percent = right;
  87. $element.find('>div').css('left', (percent - right) + "%")
  88. });
  89. $this.on('click touchend', function (e) {
  90. var $this = $(this)
  91. , $target = $(e.target)
  92. , $myCheckBox = $target.siblings('input');
  93. e.stopImmediatePropagation();
  94. e.preventDefault();
  95. $this.unbind('mouseleave');
  96. if (moving)
  97. $myCheckBox.attr('checked', !(parseInt($this.parent().css('left')) < -25));
  98. else $myCheckBox.attr("checked", !$myCheckBox.is(":checked"));
  99. moving = false;
  100. $myCheckBox.trigger('change');
  101. });
  102. $this.on('mouseleave', function (e) {
  103. var $this = $(this)
  104. , $myCheckBox = $this.siblings('input');
  105. e.preventDefault();
  106. e.stopImmediatePropagation();
  107. $this.unbind('mouseleave');
  108. $this.trigger('mouseup');
  109. $myCheckBox.attr('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  110. });
  111. $this.on('mouseup', function (e) {
  112. e.stopImmediatePropagation();
  113. e.preventDefault();
  114. $(this).unbind('mousemove');
  115. });
  116. }
  117. });
  118. }
  119. );
  120. };
  121. }(jQuery);
  122. $(function () {
  123. $('.switch').switch();
  124. });