jquery.switch.js 4.6 KB

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