jquery.swipe.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. var isSwipeX = null;
  2. $(function() {
  3. $.fn.swipe = function(options) {
  4. var _data = this.data("data");
  5. if (_data != null) {
  6. if (_data.index != options.index) {
  7. this.css({
  8. "-webkit-transform": "translate3d(" + options.index * -1 * _data.width + "rem,0,0)"
  9. })
  10. }
  11. this.data("data", $.extend({}, _data, options));
  12. _data = this.data("data");
  13. return
  14. }
  15. var defaults = {
  16. index: 0,
  17. isRem: false
  18. };
  19. var opts = $.extend({}, defaults, options);
  20. this.data("data", opts);
  21. this.each(function() {
  22. var that = $(this);
  23. var _this = $(this)[0];
  24. var _width = opts.width || parseInt($(this).children().css("width"));
  25. var sx = 0,
  26. sy = 0,
  27. mx = 0,
  28. my = 0,
  29. fontNum = 1,
  30. cRem = "px";
  31. if (opts.isRem) {
  32. cRem = "rem";
  33. fontNum = parseInt($("html").css("font-size"))
  34. }
  35. that.css({
  36. "-webkit-transition-duration": "300ms",
  37. "-webkit-transition-style": "preserve-3d",
  38. "-webkit-transition-timing-function": "linear",
  39. "-webkit-transform": "translate3d(" + opts.index * -1 * opts.width + cRem + ",0,0)"
  40. });
  41. _this.addEventListener("touchstart", function(e) {
  42. sx = e.touches[0].pageX, sy = e.touches[0].pageY, mx = 0, my = 0;
  43. that.css({
  44. "-webkit-transition-duration": "0ms"
  45. })
  46. }, false);
  47. _this.addEventListener("touchmove", function(e) {
  48. mx = (e.touches[0].pageX - sx) / fontNum, my = (e.touches[0].pageY - sy) / fontNum;
  49. if (isSwipeX == null) {
  50. isSwipeX = Math.abs(mx) > Math.abs(my)
  51. }
  52. if (isSwipeX) {
  53. e.preventDefault();
  54. that.css({
  55. "-webkit-transform": "translate3d(" + (opts.index * -1 * opts.width + mx) + cRem + ",0,0)"
  56. })
  57. }
  58. }, false);
  59. _this.addEventListener("touchend", function(e) {
  60. if (isSwipeX) {
  61. var num = 45 / fontNum;
  62. if (Math.abs(mx) > num) {
  63. var _n = that.children(opts.childrenClass).length;
  64. if (mx < 0) {
  65. if (opts.index < _n - 1) opts.index++
  66. } else {
  67. if (opts.index > 0) opts.index--
  68. }
  69. }
  70. that.attr("data-indexNum", opts.index);
  71. that.css({
  72. "-webkit-transition-duration": "200ms",
  73. "-webkit-transform": "translate3d(" + opts.index * -1 * opts.width + cRem + ",0,0)"
  74. });
  75. if (opts.afterSwipe) {
  76. opts.afterSwipe(opts.index)
  77. }
  78. }
  79. isSwipeX = null
  80. }, false)
  81. });
  82. return this
  83. };
  84. $.fn.swipeUp = function(options) {
  85. var _data = this.data("data");
  86. if (_data != null) {
  87. if (_data.index != options.index) {
  88. var _c = this.children(_data.childrenClass);
  89. _c.css({
  90. zIndex: "18",
  91. display: "none"
  92. });
  93. $(_c.get(options.index)).css({
  94. zIndex: "20",
  95. display: "block"
  96. })
  97. }
  98. this.data("data", $.extend({}, _data, options));
  99. _data = this.data("data");
  100. return
  101. }
  102. var defaults = {
  103. index: 0,
  104. speed: 300,
  105. childrenClass: ".house_section"
  106. };
  107. var opts = $.extend({}, defaults, options);
  108. this.data("data", opts);
  109. return this.each(function() {
  110. var that = $(this);
  111. var w = parseInt(that.css("width"));
  112. var h = parseInt(that.css("height"));
  113. var o = 0,
  114. c = "white";
  115. var childrenObj = that.children(opts.childrenClass);
  116. if (opts.index >= childrenObj.length) {
  117. alert("index value is too big.")
  118. }
  119. var currObj, prevObj, nextObj;
  120. var freshObj = function() {
  121. childrenObj.css({
  122. "-webkit-transform": "translate3d(0,0,0)",
  123. "-webkit-transition-duration": "0ms",
  124. opacity: 1,
  125. display: "none"
  126. });
  127. currObj = prevObj = nextObj = null;
  128. if (opts.index < 0) {
  129. currObj = $(childrenObj[childrenObj.length - 1]).show().css({
  130. zIndex: "20"
  131. });
  132. opts.index = childrenObj.length - 1
  133. } else {
  134. if (opts.index == childrenObj.length) {
  135. currObj = $(childrenObj[0]).show().css({
  136. zIndex: "20"
  137. });
  138. opts.index = 0
  139. } else {
  140. currObj = $(childrenObj[opts.index]).show().css({
  141. zIndex: "20"
  142. })
  143. }
  144. }
  145. if (opts.index > 0) {
  146. prevObj = $(childrenObj[opts.index - 1])
  147. } else {
  148. prevObj = $(childrenObj[childrenObj.length - 1])
  149. }
  150. if (opts.index < childrenObj.length - 1) {
  151. nextObj = $(childrenObj[opts.index + 1])
  152. } else {
  153. if (!currObj.hasClass("isshare_wrap")) {
  154. nextObj = $(childrenObj[0])
  155. }
  156. }
  157. };
  158. freshObj();
  159. var startY = 0,
  160. moveY = 0,
  161. endY = 0;
  162. swipeUpFlag = true;
  163. this.addEventListener("touchstart", function(e) {
  164. if (!swipeUpFlag) return;
  165. opts = that.data("data");
  166. freshObj();
  167. if (opts.beforeSwipe) {
  168. opts.beforeSwipe(opts.index)
  169. }
  170. startY = e.touches[0].pageY, endY = 0;
  171. c = currObj.css("backgroundColor");
  172. childrenObj.css({
  173. "-webkit-transition-duration": "0ms"
  174. })
  175. });
  176. this.addEventListener("touchmove", function(e) {
  177. if (!swipeUpFlag || isSwipeX != null && isSwipeX != false) return;
  178. e.preventDefault();
  179. moveY = e.touches[0].pageY;
  180. endY = moveY - startY;
  181. o = Math.abs(endY) / h;
  182. var e = false;
  183. if (prevObj) {
  184. if (endY > 0) {
  185. prevObj.css({
  186. zIndex: 18,
  187. display: "block",
  188. opacity: .5 + o
  189. })
  190. } else {
  191. prevObj.hide()
  192. }
  193. } else if (endY > 0) {
  194. e = true
  195. }
  196. if (nextObj) {
  197. if (endY < 0) {
  198. nextObj.css({
  199. zIndex: 18,
  200. display: "block",
  201. opacity: .5 + o
  202. })
  203. } else {
  204. nextObj.hide()
  205. }
  206. } else if (endY < 0) {
  207. e = true
  208. }
  209. if (e) {
  210. o = 0;
  211. endY = endY / 2;
  212. $("body").css("backgroundColor", c)
  213. } else {
  214. $("body").css("backgroundColor", "white")
  215. }
  216. currObj.css({
  217. "-webkit-transform": "translate3d(0," + endY + "px,0)",
  218. opacity: 1 - o / 2
  219. })
  220. });
  221. this.addEventListener("touchend", function(e) {
  222. if (!swipeUpFlag || isSwipeX != null && isSwipeX != false) return;
  223. if (Math.abs(endY) > 0) {
  224. childrenObj.css({
  225. "-webkit-transition-duration": opts.speed + "ms"
  226. });
  227. if (Math.abs(endY) < h / 8) {
  228. currObj.css({
  229. "-webkit-transform": "translate3d(0,0,0)",
  230. opacity: 1
  231. });
  232. if (nextObj) {
  233. nextObj.css({
  234. display: "none",
  235. opacity: 1
  236. })
  237. }
  238. } else {
  239. e.preventDefault();
  240. var _my = 0;
  241. if (endY < 0 && nextObj) {
  242. nextObj.css({
  243. opacity: 1
  244. });
  245. opts.index++;
  246. _my = -1 * h
  247. } else if (endY > 0 && prevObj) {
  248. prevObj.css({
  249. opacity: 1
  250. });
  251. opts.index--;
  252. _my = h
  253. } else {
  254. _my = 0
  255. }
  256. endY = 0;
  257. swipeUpFlag = false;
  258. currObj.css({
  259. "-webkit-transform": "translate3d(0," + _my + "px,0)",
  260. opacity: _my == 0 ? 1 : 0,
  261. zIndex: 20
  262. });
  263. setTimeout(function() {
  264. freshObj();
  265. swipeUpFlag = true;
  266. if (opts.afterSwipe) {
  267. opts.afterSwipe(opts.index)
  268. }
  269. }, opts.speed)
  270. }
  271. }
  272. that.attr("data-indexNum", opts.index)
  273. });
  274. if (opts.init) {
  275. opts.init()
  276. }
  277. });
  278. return this
  279. }
  280. })