Slider.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <script>
  2. import { onMount, createEventDispatcher } from 'svelte';
  3. import { fly } from 'svelte/transition';
  4. import { debounce, stepNumberFun } from '../utils';
  5. // 当前值
  6. // Current value
  7. export let value = 40;
  8. // 步长
  9. // Step length
  10. export let step = 1;
  11. // 可选最小值
  12. // Optional minimum value
  13. export let minRange = 0;
  14. // 可选最大值
  15. // Optional maximum value
  16. export let maxRange = 100;
  17. // 是否为区间选择
  18. // is range
  19. export let isRange = false;
  20. // 区间选择开始值
  21. // Range selection start value
  22. export let startValue = 20;
  23. // 区间选择结束值
  24. // Range selection end value
  25. export let endValue = 60;
  26. // 提示显示方式
  27. // Tip display method
  28. export let showTip = 'touch';
  29. // 圆角
  30. // radius
  31. export let radius = 'full';
  32. // 滑块是否为线框
  33. // is line block
  34. export let lineBlock = false;
  35. // 是否使用slot
  36. // is use slot
  37. export let useSlot = false;
  38. // 是否禁用
  39. // is disabled
  40. export let disabled = false;
  41. // 是否只读
  42. // is readonly
  43. export let readonly = false;
  44. let lineDom = null; //滑动条dom slider dom
  45. let blockDom = null; //滑块dom block dom
  46. let blockWidth = 0; //滑块宽度 block width
  47. let lineDomStartX = 0; //滑块条起始位置 slider start position
  48. let lineDomEndX = 0; //滑块条结束位置 slider end position
  49. let lineDomWidth = 0; //滑块条宽度 slider width
  50. let currentX = ((value - minRange) / (maxRange - minRange)) * lineDomWidth; //初始位置 initial position
  51. let currentStartX = ((startValue - minRange) / (maxRange - minRange)) * lineDomWidth; //区间选择时开始位置 start position
  52. let currentEndX = ((endValue - minRange) / (maxRange - minRange)) * lineDomWidth; //区间选择时结束位置 end position
  53. let currentMove = 'none'; //当前移动的滑块 current move block
  54. let isDown = false; //是否按下 is down
  55. const dispatch = createEventDispatcher(); //事件分发器 event dispatcher
  56. //滑动开始
  57. //slide start
  58. const touchLineStart = e => {
  59. if (disabled || readonly) {
  60. return;
  61. }
  62. isDown = true;
  63. const clientX = e.clientX;
  64. if (isRange) {
  65. //判断点击位置距离哪一个更近
  66. // Determine which one is closer to the click position
  67. if (startValue === endValue) {
  68. if (clientX - lineDomStartX <= currentStartX) {
  69. // 点击的是开始滑块
  70. // Click on the start block
  71. currentMove = 'start';
  72. currentStartX = clientX - lineDomStartX;
  73. } else {
  74. // 点击的是结束滑块
  75. // Click on the end block
  76. currentMove = 'end';
  77. currentEndX = clientX - lineDomStartX;
  78. }
  79. } else if (Math.abs(clientX - currentStartX - lineDomStartX) < Math.abs(clientX - currentEndX - lineDomStartX)) {
  80. // 点击的是开始滑块
  81. // Click on the start block
  82. currentMove = 'start';
  83. currentStartX = clientX - lineDomStartX;
  84. } else {
  85. // 点击的是结束滑块
  86. // Click on the end block
  87. currentMove = 'end';
  88. currentEndX = clientX - lineDomStartX;
  89. }
  90. startValue = stepNumberFun(minRange + (currentStartX / lineDomWidth) * (maxRange - minRange), step);
  91. endValue = stepNumberFun(minRange + (currentEndX / lineDomWidth) * (maxRange - minRange), step);
  92. dispatch('change', [startValue, endValue]); //触发事件 trigger event
  93. } else {
  94. currentMove = 'one';
  95. currentX = clientX - lineDomStartX;
  96. value = stepNumberFun((currentX / lineDomWidth) * (maxRange - minRange), step);
  97. dispatch('change', value); //触发事件 trigger event
  98. }
  99. };
  100. const touchLineMove = e => {
  101. lineDom.setPointerCapture(e.pointerId);
  102. if (disabled || readonly) {
  103. return;
  104. }
  105. if (!isDown) {
  106. return;
  107. }
  108. const clientX = e.clientX;
  109. if (isRange) {
  110. if (currentMove === 'start') {
  111. if (clientX <= lineDomStartX) {
  112. currentStartX = 0;
  113. } else if (clientX >= currentEndX + blockWidth / 2) {
  114. currentStartX = currentEndX;
  115. } else {
  116. currentStartX = clientX - lineDomStartX;
  117. }
  118. } else {
  119. if (clientX <= currentStartX + blockWidth / 2) {
  120. currentEndX = currentStartX;
  121. } else if (clientX >= lineDomEndX) {
  122. currentEndX = lineDomEndX - lineDomStartX;
  123. } else {
  124. currentEndX = clientX - lineDomStartX;
  125. //由于开启了防抖,有极短时间内会出现currentEndX大于lineDomEndX的情况,所以这里做了一个判断
  126. //Due to the opening of the anti-shake, there will be a situation where currentEndX is greater than lineDomEndX in a very short time, so a judgment is made here
  127. currentEndX = currentEndX < currentStartX ? currentStartX : currentEndX;
  128. }
  129. }
  130. startValue = stepNumberFun(minRange + (currentStartX / lineDomWidth) * (maxRange - minRange), step);
  131. endValue = stepNumberFun(minRange + (currentEndX / lineDomWidth) * (maxRange - minRange), step);
  132. dispatch('change', [startValue, endValue]); //触发事件 trigger event
  133. } else {
  134. if (clientX <= lineDomStartX) {
  135. currentX = 0;
  136. } else if (clientX >= lineDomEndX) {
  137. currentX = lineDomEndX - lineDomStartX;
  138. } else {
  139. currentX = clientX - lineDomStartX;
  140. }
  141. value = stepNumberFun(minRange + (currentX / lineDomWidth) * (maxRange - minRange), step);
  142. dispatch('change', value); //触发事件 trigger event
  143. }
  144. };
  145. const touchLineEnd = e => {
  146. currentMove = 'none';
  147. isDown = false;
  148. };
  149. const radiusObj = {
  150. none: ' rounded-none',
  151. base: ' rounded',
  152. xl: ' rounded-xl',
  153. full: ' rounded-full',
  154. };
  155. onMount(() => {
  156. lineDomStartX = lineDom.getBoundingClientRect().left;
  157. lineDomEndX = lineDom.getBoundingClientRect().right;
  158. lineDomWidth = lineDom.getBoundingClientRect().width;
  159. currentX = ((value - minRange) / (maxRange - minRange)) * lineDomWidth; //挂载完成之后初始位置 initial position after mounting
  160. currentStartX = ((startValue - minRange) / (maxRange - minRange)) * lineDomWidth; //区间选择开始位置 initial position after mounting
  161. currentEndX = ((endValue - minRange) / (maxRange - minRange)) * lineDomWidth; //区间选择结束位置 initial position after mounting
  162. if (isRange) {
  163. blockWidth = blockDom.getBoundingClientRect().width;
  164. }
  165. });
  166. </script>
  167. <div class={`relative h-7${disabled ? ' opacity-50' : ''}`}>
  168. <!-- svelte-ignore a11y-mouse-events-have-key-events -->
  169. <div
  170. on:pointerdown={touchLineStart}
  171. on:pointermove={debounce(touchLineMove, 5)}
  172. on:pointerup={touchLineEnd}
  173. class="absolute flex flex-col justify-center h-7 w-full touch-none cursor-move"
  174. bind:this={lineDom}
  175. >
  176. {#if useSlot}
  177. <slot />
  178. {:else}
  179. <div class={`w-full h-1 bg-black/5 dark:bg-white/5${radiusObj[radius] || radiusObj['full']}`}>
  180. {#if isRange}
  181. <div
  182. class={`bg-primary dark:bg-dark h-1${radiusObj[radius] || radiusObj['full']}`}
  183. style={`width:${currentEndX - currentStartX}px;transform: translateX(${currentStartX}px);`}
  184. />
  185. {:else}
  186. <div class={`bg-primary dark:bg-dark h-1${radiusObj[radius] || radiusObj['full']}`} style={`width:${currentX}px`} />
  187. {/if}
  188. </div>
  189. {/if}
  190. </div>
  191. {#if isRange}
  192. <div class="absolute flex flex-col justify-center h-7 w-full pointer-events-none">
  193. <div
  194. class={`${
  195. lineBlock
  196. ? 'w-6 h-6 border border-primary dark:border-dark bg-white dark:bg-gray2'
  197. : 'w-5 h-5 ring-4 ring-primary/10 dark:ring-dark/10 bg-primary dark:bg-dark'
  198. }${radiusObj[radius] || radiusObj['full']}`}
  199. style={`transform: translateX(calc(${currentStartX}px - 50%));`}
  200. >
  201. {#if showTip === 'always' || (currentMove === 'start' && showTip !== 'never')}
  202. <div
  203. class={`absolute -top-9 text-white dark:text-black text-xs py-1 bg-black/90 dark:bg-white px-2${
  204. radius === 'none' ? ' rounded-none' : ' rounded'
  205. }`}
  206. style={`left: 50%;transform: translateX(-50%);`}
  207. in:fly={{ y: 8, duration: 500 }}
  208. out:fly={{ y: 8, duration: 300 }}
  209. >
  210. {startValue}
  211. <div
  212. class="absolute w-0 h-0 border-4 border-t-4 border-transparent border-t-black/90 dark:border-t-white"
  213. style={`top:100%;left:50%;transform: translateX(-50%)`}
  214. />
  215. </div>
  216. {/if}
  217. </div>
  218. </div>
  219. <div class="absolute flex flex-col justify-center h-7 w-full pointer-events-none">
  220. <div
  221. class={`${
  222. lineBlock
  223. ? 'w-6 h-6 border border-primary dark:border-dark bg-white dark:bg-gray2'
  224. : 'w-5 h-5 ring-4 ring-primary/10 dark:ring-dark/10 bg-primary dark:bg-dark'
  225. }${radiusObj[radius] || radiusObj['full']}`}
  226. style={`transform: translateX(calc(${currentEndX}px - 50%));`}
  227. bind:this={blockDom}
  228. >
  229. {#if showTip === 'always' || (currentMove === 'end' && showTip !== 'never')}
  230. <div
  231. class={`absolute -top-9 text-white dark:text-black text-xs py-1 bg-black/90 dark:bg-white px-2${
  232. radius === 'none' ? ' rounded-none' : ' rounded'
  233. }`}
  234. style={`left: 50%;transform: translateX(-50%);`}
  235. in:fly={{ y: 8, duration: 500 }}
  236. out:fly={{ y: 8, duration: 300 }}
  237. >
  238. {endValue}
  239. <div
  240. class="absolute w-0 h-0 border-4 border-t-4 border-transparent border-t-black/90 dark:border-t-white"
  241. style={`top:100%;left:50%;transform: translateX(-50%)`}
  242. />
  243. </div>
  244. {/if}
  245. </div>
  246. </div>
  247. {:else}
  248. <div class="absolute flex flex-col justify-center h-7 w-full pointer-events-none">
  249. <div
  250. class={`${
  251. lineBlock
  252. ? 'w-6 h-6 border border-primary dark:border-dark bg-white dark:bg-gray2'
  253. : 'w-5 h-5 ring-4 ring-primary/10 dark:ring-dark/10 bg-primary dark:bg-dark'
  254. }${radiusObj[radius] || radiusObj['full']}`}
  255. style={`transform: translateX(calc(${currentX}px - 50%));`}
  256. >
  257. {#if showTip === 'always' || (currentMove === 'one' && showTip !== 'never')}
  258. <div
  259. class={`absolute -top-9 text-white dark:text-black text-xs py-1 bg-black/90 dark:bg-white px-2${
  260. radius === 'none' ? ' rounded-none' : ' rounded'
  261. }`}
  262. style={`left: 50%;transform: translateX(-50%);`}
  263. in:fly={{ y: 8, duration: 500 }}
  264. out:fly={{ y: 8, duration: 300 }}
  265. >
  266. {value}
  267. <div
  268. class="absolute w-0 h-0 border-4 border-t-4 border-transparent border-t-black/90 dark:border-t-white"
  269. style={`top:100%;left:50%;transform: translateX(-50%)`}
  270. />
  271. </div>
  272. {/if}
  273. </div>
  274. </div>
  275. {/if}
  276. </div>