huakuai.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ! function() {
  2. function t(t) {
  3. this.init(t)
  4. }
  5. var e = {
  6. width: "86%",
  7. height: 40,
  8. textMsg: "请按住滑块,拖动到最右边",
  9. successMsg: "验证成功!",
  10. callback: function() {}
  11. };
  12. t.prototype = {
  13. init: function(t) {
  14. this.opts = this.extend(e, t), this.render()
  15. },
  16. render: function() {
  17. this.ele = document.getElementById(this.opts.id);
  18. var t = '<div class="ft-slider-content lock">' + this.opts.textMsg + '</div><div class="ft-slider-bar" style="transform:translate3d(0,0,0);"></div><div class="ft-slider-bg"></div>',
  19. e = document.createElement("div");
  20. e.className = "ft-slider", e.innerHTML = t, this.ele.appendChild(e), this.setOptions(), this.drag()
  21. },
  22. setOptions: function() {
  23. var t = this.ele.getElementsByClassName("ft-slider")[0],
  24. e = t.getElementsByClassName("ft-slider-bar")[0],
  25. s = this.opts,
  26. i = "number" == typeof s.width ? s.width + "px" : s.width,
  27. n = "number" == typeof s.height ? s.height + "px" : s.height;
  28. t.style.width = i, t.style.height = n, t.style.lineHeight = t.offsetHeight + "px", e.style.width = t.offsetHeight + "px", e.style.height = t.offsetHeight + "px"
  29. },
  30. restore: function() {
  31. if(this.result) {
  32. var t = this.opts,
  33. e = this.ele.getElementsByClassName("ft-slider")[0],
  34. s = e.getElementsByClassName("ft-slider-bar")[0],
  35. i = e.getElementsByClassName("ft-slider-content")[0],
  36. n = e.getElementsByClassName("ft-slider-bg")[0];
  37. this.result = !1, i.className = "ft-slider-content lock", s.style.transform = "translate3d(0,0,0)", n.style.width = 0, s.className = "ft-slider-bar transition", i.textContent = t.textMsg
  38. }
  39. },
  40. drag: function() {
  41. var t = this.ele.getElementsByClassName("ft-slider-bar")[0];
  42. this.isDrag = !1, this.result = !1, this.start(t), this.move(t), this.end(t)
  43. },
  44. start: function(t) {
  45. var e = this,
  46. s = this.ele.getElementsByClassName("ft-slider-bg")[0];
  47. t.addEventListener("touchstart", function(t) {
  48. e.result || (this.className = "ft-slider-bar", s.className = "ft-slider-bg", e.isDrag = !0, e.strartX = t.touches[0].clientX, t.preventDefault())
  49. }, !1)
  50. },
  51. move: function(t) {
  52. var e = this,
  53. s = this.ele.getElementsByClassName("ft-slider")[0],
  54. i = s.getElementsByClassName("ft-slider-bg")[0];
  55. t.addEventListener("touchmove", function(t) {
  56. if(e.isDrag && !e.result) {
  57. var n = t.touches[0].clientX - e.strartX,
  58. l = s.offsetWidth - s.offsetHeight;
  59. n = n > 0 ? n > l ? l : n : 0, this.style.transform = "translate3d(" + n + "px,0,0)", i.style.width = n + "px", t.preventDefault()
  60. }
  61. }, !1)
  62. },
  63. end: function(t) {
  64. var e = this,
  65. s = this.ele.getElementsByClassName("ft-slider")[0],
  66. i = s.getElementsByClassName("ft-slider-bg")[0],
  67. n = s.getElementsByClassName("ft-slider-content")[0],
  68. i = s.getElementsByClassName("ft-slider-bg")[0];
  69. t.addEventListener("touchend", function(t) {
  70. if(e.isDrag) {
  71. var l = e.opts,
  72. a = l.callback,
  73. r = t.changedTouches[0].clientX - e.strartX,
  74. d = s.offsetWidth - s.offsetHeight,
  75. h = 0;
  76. r >= d ? (h = d, n.textContent = l.successMsg, this.className = this.className += " success", n.className = "ft-slider-content success", e.result = !0) : e.result = !1, this.className = e.result ? "ft-slider-bar success transition" : "ft-slider-bar transition", i.className = "ft-slider-bg transition", i.style.width = h + "px", this.style.transform = "translate3d(" + h + "px,0,0)", e.isDrag = !1, a && a(e.result), t.preventDefault()
  77. }
  78. }, !1)
  79. },
  80. extend: function(t, e) {
  81. var s = {};
  82. if(!e.id) throw "id is required";
  83. for(var i = 0; i < arguments.length; i++)
  84. for(option in arguments[i]) s[option] = arguments[i][option];
  85. return s
  86. },
  87. getTranslateX: function(t) {
  88. return parseInt(t.match(/-?\d+(?=px)/g)[0])
  89. }
  90. }, window.FtSlider = t
  91. }();