Swipe.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. SWIPE_KEY: () => SWIPE_KEY,
  21. default: () => stdin_default
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_use = require("@vant/use");
  28. var import_use_touch = require("../composables/use-touch");
  29. var import_use_expose = require("../composables/use-expose");
  30. var import_on_popup_reopen = require("../composables/on-popup-reopen");
  31. const [name, bem] = (0, import_utils.createNamespace)("swipe");
  32. const swipeProps = {
  33. loop: import_utils.truthProp,
  34. width: import_utils.numericProp,
  35. height: import_utils.numericProp,
  36. vertical: Boolean,
  37. autoplay: (0, import_utils.makeNumericProp)(0),
  38. duration: (0, import_utils.makeNumericProp)(500),
  39. touchable: import_utils.truthProp,
  40. lazyRender: Boolean,
  41. initialSwipe: (0, import_utils.makeNumericProp)(0),
  42. indicatorColor: String,
  43. showIndicators: import_utils.truthProp,
  44. stopPropagation: import_utils.truthProp
  45. };
  46. const SWIPE_KEY = Symbol(name);
  47. var stdin_default = (0, import_vue2.defineComponent)({
  48. name,
  49. props: swipeProps,
  50. emits: ["change"],
  51. setup(props, {
  52. emit,
  53. slots
  54. }) {
  55. const root = (0, import_vue2.ref)();
  56. const track = (0, import_vue2.ref)();
  57. const state = (0, import_vue2.reactive)({
  58. rect: null,
  59. width: 0,
  60. height: 0,
  61. offset: 0,
  62. active: 0,
  63. swiping: false
  64. });
  65. const touch = (0, import_use_touch.useTouch)();
  66. const {
  67. children,
  68. linkChildren
  69. } = (0, import_use.useChildren)(SWIPE_KEY);
  70. const count = (0, import_vue2.computed)(() => children.length);
  71. const size = (0, import_vue2.computed)(() => state[props.vertical ? "height" : "width"]);
  72. const delta = (0, import_vue2.computed)(() => props.vertical ? touch.deltaY.value : touch.deltaX.value);
  73. const minOffset = (0, import_vue2.computed)(() => {
  74. if (state.rect) {
  75. const base = props.vertical ? state.rect.height : state.rect.width;
  76. return base - size.value * count.value;
  77. }
  78. return 0;
  79. });
  80. const maxCount = (0, import_vue2.computed)(() => Math.ceil(Math.abs(minOffset.value) / size.value));
  81. const trackSize = (0, import_vue2.computed)(() => count.value * size.value);
  82. const activeIndicator = (0, import_vue2.computed)(() => (state.active + count.value) % count.value);
  83. const isCorrectDirection = (0, import_vue2.computed)(() => {
  84. const expect = props.vertical ? "vertical" : "horizontal";
  85. return touch.direction.value === expect;
  86. });
  87. const trackStyle = (0, import_vue2.computed)(() => {
  88. const style = {
  89. transitionDuration: `${state.swiping ? 0 : props.duration}ms`,
  90. transform: `translate${props.vertical ? "Y" : "X"}(${state.offset}px)`
  91. };
  92. if (size.value) {
  93. const mainAxis = props.vertical ? "height" : "width";
  94. const crossAxis = props.vertical ? "width" : "height";
  95. style[mainAxis] = `${trackSize.value}px`;
  96. style[crossAxis] = props[crossAxis] ? `${props[crossAxis]}px` : "";
  97. }
  98. return style;
  99. });
  100. const getTargetActive = (pace) => {
  101. const {
  102. active
  103. } = state;
  104. if (pace) {
  105. if (props.loop) {
  106. return (0, import_utils.clamp)(active + pace, -1, count.value);
  107. }
  108. return (0, import_utils.clamp)(active + pace, 0, maxCount.value);
  109. }
  110. return active;
  111. };
  112. const getTargetOffset = (targetActive, offset = 0) => {
  113. let currentPosition = targetActive * size.value;
  114. if (!props.loop) {
  115. currentPosition = Math.min(currentPosition, -minOffset.value);
  116. }
  117. let targetOffset = offset - currentPosition;
  118. if (!props.loop) {
  119. targetOffset = (0, import_utils.clamp)(targetOffset, minOffset.value, 0);
  120. }
  121. return targetOffset;
  122. };
  123. const move = ({
  124. pace = 0,
  125. offset = 0,
  126. emitChange
  127. }) => {
  128. if (count.value <= 1) {
  129. return;
  130. }
  131. const {
  132. active
  133. } = state;
  134. const targetActive = getTargetActive(pace);
  135. const targetOffset = getTargetOffset(targetActive, offset);
  136. if (props.loop) {
  137. if (children[0] && targetOffset !== minOffset.value) {
  138. const outRightBound = targetOffset < minOffset.value;
  139. children[0].setOffset(outRightBound ? trackSize.value : 0);
  140. }
  141. if (children[count.value - 1] && targetOffset !== 0) {
  142. const outLeftBound = targetOffset > 0;
  143. children[count.value - 1].setOffset(outLeftBound ? -trackSize.value : 0);
  144. }
  145. }
  146. state.active = targetActive;
  147. state.offset = targetOffset;
  148. if (emitChange && targetActive !== active) {
  149. emit("change", activeIndicator.value);
  150. }
  151. };
  152. const correctPosition = () => {
  153. state.swiping = true;
  154. if (state.active <= -1) {
  155. move({
  156. pace: count.value
  157. });
  158. } else if (state.active >= count.value) {
  159. move({
  160. pace: -count.value
  161. });
  162. }
  163. };
  164. const prev = () => {
  165. correctPosition();
  166. touch.reset();
  167. (0, import_use.doubleRaf)(() => {
  168. state.swiping = false;
  169. move({
  170. pace: -1,
  171. emitChange: true
  172. });
  173. });
  174. };
  175. const next = () => {
  176. correctPosition();
  177. touch.reset();
  178. (0, import_use.doubleRaf)(() => {
  179. state.swiping = false;
  180. move({
  181. pace: 1,
  182. emitChange: true
  183. });
  184. });
  185. };
  186. let autoplayTimer;
  187. const stopAutoplay = () => clearTimeout(autoplayTimer);
  188. const autoplay = () => {
  189. stopAutoplay();
  190. if (props.autoplay > 0 && count.value > 1) {
  191. autoplayTimer = setTimeout(() => {
  192. next();
  193. autoplay();
  194. }, +props.autoplay);
  195. }
  196. };
  197. const initialize = (active = +props.initialSwipe) => {
  198. if (!root.value) {
  199. return;
  200. }
  201. const cb = () => {
  202. var _a, _b;
  203. if (!(0, import_utils.isHidden)(root)) {
  204. const rect = {
  205. width: root.value.offsetWidth,
  206. height: root.value.offsetHeight
  207. };
  208. state.rect = rect;
  209. state.width = +((_a = props.width) != null ? _a : rect.width);
  210. state.height = +((_b = props.height) != null ? _b : rect.height);
  211. }
  212. if (count.value) {
  213. active = Math.min(count.value - 1, active);
  214. }
  215. state.active = active;
  216. state.swiping = true;
  217. state.offset = getTargetOffset(active);
  218. children.forEach((swipe) => {
  219. swipe.setOffset(0);
  220. });
  221. autoplay();
  222. };
  223. if ((0, import_utils.isHidden)(root)) {
  224. (0, import_vue2.nextTick)().then(cb);
  225. } else {
  226. cb();
  227. }
  228. };
  229. const resize = () => initialize(state.active);
  230. let touchStartTime;
  231. const onTouchStart = (event) => {
  232. if (!props.touchable)
  233. return;
  234. touch.start(event);
  235. touchStartTime = Date.now();
  236. stopAutoplay();
  237. correctPosition();
  238. };
  239. const onTouchMove = (event) => {
  240. if (props.touchable && state.swiping) {
  241. touch.move(event);
  242. if (isCorrectDirection.value) {
  243. const isEdgeTouch = !props.loop && (state.active === 0 && delta.value > 0 || state.active === count.value - 1 && delta.value < 0);
  244. if (!isEdgeTouch) {
  245. (0, import_utils.preventDefault)(event, props.stopPropagation);
  246. move({
  247. offset: delta.value
  248. });
  249. }
  250. }
  251. }
  252. };
  253. const onTouchEnd = () => {
  254. if (!props.touchable || !state.swiping) {
  255. return;
  256. }
  257. const duration = Date.now() - touchStartTime;
  258. const speed = delta.value / duration;
  259. const shouldSwipe = Math.abs(speed) > 0.25 || Math.abs(delta.value) > size.value / 2;
  260. if (shouldSwipe && isCorrectDirection.value) {
  261. const offset = props.vertical ? touch.offsetY.value : touch.offsetX.value;
  262. let pace = 0;
  263. if (props.loop) {
  264. pace = offset > 0 ? delta.value > 0 ? -1 : 1 : 0;
  265. } else {
  266. pace = -Math[delta.value > 0 ? "ceil" : "floor"](delta.value / size.value);
  267. }
  268. move({
  269. pace,
  270. emitChange: true
  271. });
  272. } else if (delta.value) {
  273. move({
  274. pace: 0
  275. });
  276. }
  277. state.swiping = false;
  278. autoplay();
  279. };
  280. const swipeTo = (index, options = {}) => {
  281. correctPosition();
  282. touch.reset();
  283. (0, import_use.doubleRaf)(() => {
  284. let targetIndex;
  285. if (props.loop && index === count.value) {
  286. targetIndex = state.active === 0 ? 0 : index;
  287. } else {
  288. targetIndex = index % count.value;
  289. }
  290. if (options.immediate) {
  291. (0, import_use.doubleRaf)(() => {
  292. state.swiping = false;
  293. });
  294. } else {
  295. state.swiping = false;
  296. }
  297. move({
  298. pace: targetIndex - state.active,
  299. emitChange: true
  300. });
  301. });
  302. };
  303. const renderDot = (_, index) => {
  304. const active = index === activeIndicator.value;
  305. const style = active ? {
  306. backgroundColor: props.indicatorColor
  307. } : void 0;
  308. return (0, import_vue.createVNode)("i", {
  309. "style": style,
  310. "class": bem("indicator", {
  311. active
  312. })
  313. }, null);
  314. };
  315. const renderIndicator = () => {
  316. if (slots.indicator) {
  317. return slots.indicator({
  318. active: activeIndicator.value,
  319. total: count.value
  320. });
  321. }
  322. if (props.showIndicators && count.value > 1) {
  323. return (0, import_vue.createVNode)("div", {
  324. "class": bem("indicators", {
  325. vertical: props.vertical
  326. })
  327. }, [Array(count.value).fill("").map(renderDot)]);
  328. }
  329. };
  330. (0, import_use_expose.useExpose)({
  331. prev,
  332. next,
  333. state,
  334. resize,
  335. swipeTo
  336. });
  337. linkChildren({
  338. size,
  339. props,
  340. count,
  341. activeIndicator
  342. });
  343. (0, import_vue2.watch)(() => props.initialSwipe, (value) => initialize(+value));
  344. (0, import_vue2.watch)(count, () => initialize(state.active));
  345. (0, import_vue2.watch)(() => props.autoplay, autoplay);
  346. (0, import_vue2.watch)([import_utils.windowWidth, import_utils.windowHeight], resize);
  347. (0, import_vue2.watch)((0, import_use.usePageVisibility)(), (visible) => {
  348. if (visible === "visible") {
  349. autoplay();
  350. } else {
  351. stopAutoplay();
  352. }
  353. });
  354. (0, import_vue2.onMounted)(initialize);
  355. (0, import_vue2.onActivated)(() => initialize(state.active));
  356. (0, import_on_popup_reopen.onPopupReopen)(() => initialize(state.active));
  357. (0, import_vue2.onDeactivated)(stopAutoplay);
  358. (0, import_vue2.onBeforeUnmount)(stopAutoplay);
  359. (0, import_use.useEventListener)("touchmove", onTouchMove, {
  360. target: track
  361. });
  362. return () => {
  363. var _a;
  364. return (0, import_vue.createVNode)("div", {
  365. "ref": root,
  366. "class": bem()
  367. }, [(0, import_vue.createVNode)("div", {
  368. "ref": track,
  369. "style": trackStyle.value,
  370. "class": bem("track", {
  371. vertical: props.vertical
  372. }),
  373. "onTouchstartPassive": onTouchStart,
  374. "onTouchend": onTouchEnd,
  375. "onTouchcancel": onTouchEnd
  376. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]), renderIndicator()]);
  377. };
  378. }
  379. });