slider.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. (function ($) {
  2. function Slider(cnf) {
  3. this.opts = {
  4. direction: cnf.direction || "left",
  5. width: cnf.width || null,
  6. height: cnf.height || null,
  7. dom: $(cnf.dom),
  8. time: cnf.time || null,
  9. shade: cnf.shade,
  10. closeShade: (typeof cnf.closeShade == 'undefined') ? true : cnf.closeShade,
  11. callback: cnf.callback || null,
  12. background: cnf.background || null,
  13. top: cnf.top || null,
  14. right: cnf.right || null,
  15. bottom: cnf.bottom || null,
  16. left: cnf.left || null,
  17. zIndex: cnf.zIndex || 97,
  18. hasTopNavbar: (typeof cnf.hasTopNavbar == 'undefined') ? true : cnf.hasTopNavbar,
  19. };
  20. this.id = this.randomString();
  21. this.dom = this.opts.dom[0];
  22. this.container = null;
  23. this.inner = null;
  24. this.shade = null;
  25. this.opened = false;
  26. this.init()
  27. }
  28. Slider.prototype = {
  29. isMobile: function () {
  30. return navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i) ? true : false
  31. },
  32. isSmallScreen: function () {
  33. return screen.width <= 767
  34. },
  35. addEvent: function (f, e, d) {
  36. if (f.attachEvent) {
  37. f.attachEvent("on" + e, d)
  38. } else {
  39. f.addEventListener(e, d, false)
  40. }
  41. },
  42. randomString: function () {
  43. return Math.random().toString(36).substr(2, 6)
  44. },
  45. init: function () {
  46. var self = this;
  47. if (! self.dom) {
  48. throw new Error('Invalid dom');
  49. }
  50. var mainDiv = document.createElement("div");
  51. var innderDiv = document.createElement("div");
  52. var shadeDiv = document.createElement("div");
  53. mainDiv.setAttribute("class", "da-slider-main da-slider-" + self.id);
  54. innderDiv.setAttribute("class", "da-slider-inner");
  55. shadeDiv.setAttribute("class", "da-slider-shade");
  56. innderDiv.appendChild(self.dom);
  57. mainDiv.appendChild(innderDiv);
  58. mainDiv.appendChild(shadeDiv);
  59. $("body")[0].appendChild(mainDiv);
  60. self.container = mainDiv;
  61. self.inner = innderDiv;
  62. self.shade = shadeDiv;
  63. switch (self.opts.direction) {
  64. case "t":
  65. case "top":
  66. self.top = self.opts.top || 0;
  67. self.left = self.opts.left || 0;
  68. self.width = self.opts.width || "100%";
  69. self.height = self.opts.height || "30%";
  70. self.translate = "0,-100%,0";
  71. break;
  72. case "b":
  73. case "bottom":
  74. self.bottom = self.opts.bottom || 0;
  75. self.left = self.opts.left || 0;
  76. self.width = self.opts.width || "100%";
  77. self.height = self.opts.height || "30%";
  78. self.translate = "0,100%,0";
  79. break;
  80. case "r":
  81. case "right":
  82. self.bottom = self.opts.bottom || 0;
  83. self.right = self.opts.right || 0;
  84. self.width = self.opts.width || "30%";
  85. self.height = self.opts.height || self.autoHeight() + "px";
  86. self.translate = "100%,0,0";
  87. break;
  88. default:
  89. self.bottom = self.opts.bottom || 0;
  90. self.left = self.opts.left || 0;
  91. self.width = self.opts.width || "30%";
  92. self.height = self.opts.height || self.autoHeight() + "px";
  93. self.translate = "-100%,0,0"
  94. }
  95. mainDiv.style.display = "none";
  96. mainDiv.style.position = "fixed";
  97. mainDiv.style.top = "0";
  98. mainDiv.style.left = "0";
  99. mainDiv.style.width = "100%";
  100. mainDiv.style.height = "100%";
  101. mainDiv.style.zIndex = self.opts.zIndex + 1;
  102. innderDiv.style.position = "absolute";
  103. innderDiv.style.top = self.top;
  104. innderDiv.style.bottom = self.bottom;
  105. innderDiv.style.left = self.left;
  106. innderDiv.style.right = self.right;
  107. innderDiv.style.backgroundColor = self.opts.background;
  108. innderDiv.style.transform = "translate3d(" + self.translate + ")";
  109. innderDiv.style.webkitTransition = "all .2s ease-out";
  110. innderDiv.style.transition = "all .2s ease-out";
  111. innderDiv.style.zIndex = self.opts.zIndex + 2;
  112. innderDiv.style.boxShadow = '1px 1px 5px #ccc';
  113. innderDiv.style.overflowY = 'auto';
  114. innderDiv.style.padding = '10px';
  115. shadeDiv.style.width = "100%";
  116. shadeDiv.style.height = "100%";
  117. shadeDiv.style.opacity = "0";
  118. if (self.opts.shade !== false) {
  119. shadeDiv.style.backgroundColor = self.opts.shade || "rgb(0, 0, 0, 0.3)";
  120. }
  121. shadeDiv.style.zIndex = self.opts.zIndex;
  122. shadeDiv.style.webkitTransition = "all .2s ease-out";
  123. shadeDiv.style.transition = "all .2s ease-out";
  124. shadeDiv.style.webkitBackfaceVisibility = "hidden";
  125. self.resize();
  126. self.addListeners();
  127. },
  128. resize: function () {
  129. var self = this,
  130. d = this.opts.direction,
  131. map = {'t': 1, 'top': 1, 'b': 1, 'bottom': 1},
  132. dom = this.inner;
  133. if (! this.opts.height && ! (d in map)) {
  134. self.height = this.autoHeight() + "px";
  135. dom.style.height = '100%'
  136. }
  137. if (this.isSmallScreen() && ! (d in map)) {
  138. self.width = '100%';
  139. dom.style.width = '100%'
  140. }
  141. $(dom).slimScroll({
  142. width: '99.7%',
  143. height: self.height,
  144. });
  145. $(this.container).find('.slimScrollDiv').css({
  146. bottom: self.bottom,
  147. top: self.top,
  148. right: self.right,
  149. left: self.left,
  150. position: 'absolute',
  151. width: self.width,
  152. height: self.height,
  153. // 'box-shadow': '1px 1px 5px #ccc',
  154. });
  155. },
  156. autoHeight: function () {
  157. return document.documentElement.clientHeight
  158. - (this.opts.hasTopNavbar ? (this.isSmallScreen() ? 120 : 60) : 0)
  159. },
  160. toggle: function () {
  161. this.opened ? this.close() : this.open();
  162. },
  163. open: function () {
  164. var self = this;
  165. self.container.style.display = "block";
  166. self.opened = true;
  167. setTimeout(function () {
  168. self.inner.style.transform = "translate3d(0,0,0)";
  169. self.inner.style.webkitTransform = "translate3d(0,0,0)";
  170. self.shade.style.opacity = 0.5
  171. }, 30);
  172. if (self.opts.time) {
  173. self.timer = setTimeout(function () {
  174. self.close()
  175. }, self.opts.time)
  176. }
  177. },
  178. close: function () {
  179. var self = this;
  180. self.timer && clearTimeout(self.timer);
  181. self.inner.style.webkitTransform = "translate3d(" + self.translate + ")";
  182. self.inner.style.transform = "translate3d(" + self.translate + ")";
  183. self.shade.style.opacity = 0;
  184. self.opened = false;
  185. setTimeout(function () {
  186. self.container.style.display = "none";
  187. self.timer = null;
  188. self.opts.callback && self.opts.callback()
  189. }, 300)
  190. },
  191. destroy: function () {
  192. this.container.remove();
  193. },
  194. onClick: function (dom, callback) {
  195. this.addEvent(dom, (this.isMobile() ? "touchend" : "click"), callback)
  196. },
  197. addListeners: function () {
  198. var self = this;
  199. self.addEvent(self.shade, "touchmove", function (f) {
  200. f.preventDefault()
  201. });
  202. self.onClick(self.shade, function (f) {
  203. if (self.opts.closeShade) {
  204. self.close()
  205. }
  206. });
  207. $(window).resize(function () {
  208. self.resize()
  209. })
  210. }
  211. };
  212. LA.Slider = Slider
  213. })(jQuery);