ScrollList.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <template>
  2. <div class="list-scroll-wrap" :style="{
  3. height: visibleData.length ? `${listHeight}px` : 'auto',
  4. maxHeight: visibleData.length ? `${maxListHeight}px` : `${listHeight}px`,
  5. transition: listWrapTransition
  6. }">
  7. <!-- 拖拽器 -->
  8. <div v-if="props.showDragger" class="list-dragger" ref="draggerRef" @touchstart="onDraggerTouchStart"
  9. @touchmove="onDraggerTouchMove" @touchend="onDraggerTouchEnd" @mousedown="onDraggerMouseDown" :style="{ top: 0 }">
  10. <div class="list-dragger-bar"></div>
  11. </div>
  12. <div class="list-scroller" ref="listRef" :style="{ height: '100%' }">
  13. <div class="list-wrap" :style="{
  14. paddingTop: `${paddingTop + topPaddingSize}px`,
  15. paddingBottom: `${bottomPaddingSize}px`,
  16. }" ref="listWrap">
  17. <template v-for="(item, index) in visibleData" :key="getStableItemKey(item, index)">
  18. <slot
  19. name="item"
  20. :data="item"
  21. :index="index"
  22. :isLast="index === visibleData.length - 1"
  23. v-bind="item"
  24. ></slot>
  25. </template>
  26. </div>
  27. </div>
  28. <!-- <div style="position: absolute; z-index: 100;top: 30%; left: 0;color: white;background: black;">
  29. s {{ scrollTopView }} p {{ paddingTop }} si {{ startIndex }} b {{ paddingBottom }} ei {{ endIndex }}
  30. </div> -->
  31. </div>
  32. </template>
  33. <script setup>
  34. import { computed, ref, onMounted, onBeforeUnmount, nextTick, watch } from "vue";
  35. import { useResizeObserver } from "@vueuse/core";
  36. import { sleep } from "../utils";
  37. const props = defineProps(["data", "height", "maxHeight", "showDragger", "bottomPadding", "topPadding", "nativeScrollMode", "smoothFollow"]);
  38. const emit = defineEmits(["scrollPositionChange"]);
  39. const listRef = ref(null);
  40. const listWrap = ref(null);
  41. const draggerRef = ref(null);
  42. const listSize = 20;
  43. const minListHeight = 100;
  44. const maxListHeight = computed(() => (props.maxHeight || 400));
  45. const bottomPaddingSize = computed(() => Math.max(0, Number(props.bottomPadding || 0)));
  46. const topPaddingSize = computed(() => Math.max(0, Number(props.topPadding || 0)));
  47. const listHeight = ref(props.height || 400);
  48. const startIndex = ref(0);
  49. const endIndex = ref(100);
  50. const paddingTop = ref(0);
  51. const paddingBottom = ref(0);
  52. let nextStableItemKey = 0;
  53. const stableItemKeys = new WeakMap();
  54. const scrollAnimationDurationMs = 300;
  55. let scrollAnimationFrameId = 0;
  56. let scrollAnimationRevision = 0;
  57. let isScrollAnimationActive = false;
  58. let scrollAnimationResolve = null;
  59. let lastScrollLogTop = null;
  60. let programmaticScrollKind = null;
  61. let programmaticScrollClearFrameId = 0;
  62. let isAutoScrollTopPinned = false;
  63. let isAutoScrollTopPinSuppressedByUser = false;
  64. const beginProgrammaticScroll = (kind) => {
  65. if (programmaticScrollClearFrameId) {
  66. cancelAnimationFrame(programmaticScrollClearFrameId);
  67. programmaticScrollClearFrameId = 0;
  68. }
  69. programmaticScrollKind = kind;
  70. if (kind === "single") {
  71. scheduleClearProgrammaticScroll();
  72. }
  73. };
  74. const clearProgrammaticScroll = () => {
  75. if (programmaticScrollClearFrameId) {
  76. cancelAnimationFrame(programmaticScrollClearFrameId);
  77. programmaticScrollClearFrameId = 0;
  78. }
  79. programmaticScrollKind = null;
  80. };
  81. const clearAutoScrollTopPin = () => {
  82. isAutoScrollTopPinned = false;
  83. };
  84. const setAutoScrollTopPinSuppressedByUser = (active) => {
  85. isAutoScrollTopPinSuppressedByUser = Boolean(active);
  86. };
  87. const setAutoScrollTopPinned = (active) => {
  88. isAutoScrollTopPinned = Boolean(active);
  89. };
  90. const isAutoScrollTopPinnedState = () => isAutoScrollTopPinned;
  91. const isAutoScrollTopPinSuppressedByUserState = () => isAutoScrollTopPinSuppressedByUser;
  92. const scheduleClearProgrammaticScroll = () => {
  93. if (programmaticScrollClearFrameId) {
  94. cancelAnimationFrame(programmaticScrollClearFrameId);
  95. }
  96. programmaticScrollClearFrameId = requestAnimationFrame(() => {
  97. programmaticScrollClearFrameId = 0;
  98. if (programmaticScrollKind === "single") {
  99. programmaticScrollKind = null;
  100. }
  101. });
  102. };
  103. function animateScrollTo(targetTop, duration = scrollAnimationDurationMs) {
  104. if (!listRef.value || !Number.isFinite(targetTop)) {
  105. return Promise.resolve();
  106. }
  107. cancelScrollAnimation();
  108. beginProgrammaticScroll("animated");
  109. const maxScrollTop = Math.max(0, listRef.value.scrollHeight - listRef.value.clientHeight);
  110. const startTop = listRef.value.scrollTop;
  111. const resolvedTargetTop = Math.max(0, Math.min(maxScrollTop, targetTop));
  112. if (resolvedTargetTop === startTop) {
  113. clearProgrammaticScroll();
  114. return Promise.resolve();
  115. }
  116. return new Promise((resolve) => {
  117. const startTime = performance.now();
  118. const revision = ++scrollAnimationRevision;
  119. isScrollAnimationActive = true;
  120. scrollAnimationResolve = resolve;
  121. const finish = () => {
  122. if (scrollAnimationResolve) {
  123. scrollAnimationResolve();
  124. scrollAnimationResolve = null;
  125. }
  126. scrollAnimationFrameId = 0;
  127. isScrollAnimationActive = false;
  128. clearProgrammaticScroll();
  129. };
  130. const animate = (now) => {
  131. if (revision !== scrollAnimationRevision || !listRef.value) {
  132. finish();
  133. return;
  134. }
  135. const progress = Math.min(1, (now - startTime) / duration);
  136. const eased = 1 - Math.pow(1 - progress, 3);
  137. listRef.value.scrollTop = startTop + (resolvedTargetTop - startTop) * eased;
  138. if (progress < 1) {
  139. scrollAnimationFrameId = requestAnimationFrame(animate);
  140. } else {
  141. finish();
  142. }
  143. };
  144. scrollAnimationFrameId = requestAnimationFrame(animate);
  145. });
  146. }
  147. const cancelScrollAnimation = () => {
  148. if (scrollAnimationFrameId) {
  149. cancelAnimationFrame(scrollAnimationFrameId);
  150. scrollAnimationFrameId = 0;
  151. }
  152. if (scrollAnimationResolve) {
  153. scrollAnimationResolve();
  154. scrollAnimationResolve = null;
  155. }
  156. scrollAnimationRevision += 1;
  157. isScrollAnimationActive = false;
  158. clearProgrammaticScroll();
  159. };
  160. const getStableItemKey = (item, index) => {
  161. if (!item || typeof item !== "object") {
  162. return `primitive-${index}`;
  163. }
  164. if (!stableItemKeys.has(item)) {
  165. stableItemKeys.set(item, `record-${nextStableItemKey++}`);
  166. }
  167. return stableItemKeys.get(item);
  168. };
  169. const visibleData = computed(() => {
  170. return props.data; //.slice(startIndex.value, endIndex.value)
  171. });
  172. const scrollTopView = ref(0);
  173. // 控制 transition
  174. const isDragging = ref(false);
  175. const listWrapTransition = computed(() =>
  176. isDragging.value ? "none" : "all 0.3s ease-in-out"
  177. );
  178. const updateVisibleData = () => {
  179. const { scrollTop } = listRef.value;
  180. let _startIndex = 0;
  181. let _endIndex = props.data.length - 1;
  182. let _paddingTop = 0;
  183. let _paddingBottom = 0;
  184. let onView = 0;
  185. let isTopEdgeTouched = false;
  186. let isBottomEdgeTouched = false;
  187. let _bufferZone = 500;
  188. let gap = 6;
  189. props.data.forEach((item, index) => {
  190. if (
  191. scrollTop > _paddingTop + (item.rect?.height || 0) + gap + _bufferZone &&
  192. !isTopEdgeTouched
  193. ) {
  194. _paddingTop += (item.rect?.height || 0) + gap;
  195. _startIndex = index + 1;
  196. } else {
  197. isTopEdgeTouched = true;
  198. }
  199. if (scrollTop + listHeight.value + _bufferZone > onView + (item.rect?.height || 0) + gap) {
  200. onView += (item.rect?.height || 0) + gap;
  201. } else {
  202. _paddingBottom += (item.rect?.height || 0) + gap;
  203. if (!isBottomEdgeTouched) {
  204. _endIndex = index + 1;
  205. isBottomEdgeTouched = true;
  206. }
  207. }
  208. });
  209. scrollTopView.value = scrollTop;
  210. if (startIndex.value != _startIndex) {
  211. startIndex.value = _startIndex;
  212. }
  213. if (endIndex.value != _endIndex) {
  214. endIndex.value = _endIndex;
  215. }
  216. if (paddingTop.value !== _paddingTop) {
  217. paddingTop.value = _paddingTop;
  218. }
  219. if (paddingBottom.value !== _paddingBottom) {
  220. paddingBottom.value = _paddingBottom;
  221. }
  222. };
  223. onMounted(() => {
  224. listRef.value.addEventListener("scroll", handleScrollEvent);
  225. if (!props.nativeScrollMode) {
  226. listRef.value.addEventListener("mousemove", handleMousemove);
  227. listRef.value.addEventListener("mousewheel", handleScroll);
  228. listRef.value.addEventListener("touchmove", handleTouchMove);
  229. listRef.value.addEventListener("touchstart", handleTouch);
  230. }
  231. window.addEventListener("mousemove", onDraggerMouseMove);
  232. window.addEventListener("mouseup", onDraggerMouseUp);
  233. });
  234. onBeforeUnmount(() => {
  235. listRef.value.removeEventListener("scroll", handleScrollEvent);
  236. if (!props.nativeScrollMode) {
  237. listRef.value.removeEventListener("touchstart", handleTouch);
  238. listRef.value.removeEventListener("mousemove", handleMousemove);
  239. listRef.value.removeEventListener("mousewheel", handleScroll);
  240. listRef.value.removeEventListener("touchmove", handleTouchMove);
  241. }
  242. window.removeEventListener("mousemove", onDraggerMouseMove);
  243. window.removeEventListener("mouseup", onDraggerMouseUp);
  244. stopScrollFollow();
  245. cancelScrollAnimation();
  246. });
  247. let lastFinger;
  248. let currentPoint = { x: 0, y: 0 };
  249. useResizeObserver(listWrap, (entry) => {
  250. if (isScrollAnimationActive) {
  251. return;
  252. }
  253. // 流式增长时这里每帧都会触发;走跟随方法,避免瞬时跳底抵消平滑。
  254. // smoothFollow 为假时该方法内部退化回原来的瞬时贴底。
  255. scrollToBottomFollow();
  256. });
  257. let requestId = null;
  258. function handleTouchMove(e) {
  259. if (e.target instanceof Element && e.target.classList.contains("half-chat-box-dragger")) {
  260. return;
  261. }
  262. if (requestId) {
  263. cancelAnimationFrame(requestId);
  264. }
  265. setAutoScrollTopPinSuppressedByUser(true);
  266. clearAutoScrollTopPin();
  267. if (e.touches.length) {
  268. const rect = listRef.value.getBoundingClientRect();
  269. const finger = e.touches[0];
  270. if (
  271. currentPoint.x > rect.left &&
  272. currentPoint.x < rect.left + rect.width &&
  273. currentPoint.y > rect.top &&
  274. currentPoint.y < rect.top + rect.height
  275. ) {
  276. if (currentPoint) {
  277. const deltaY = finger.pageY - lastFinger.y;
  278. const dampingFactor = 1; // Damping factor to simulate iOS-like scrolling
  279. const newScrollTop = listRef.value.scrollTop - deltaY * dampingFactor;
  280. // Apply easing effect
  281. listRef.value.style.transition = "scroll-top 0.2s ease-out";
  282. listRef.value.scrollTo({ top: newScrollTop });
  283. // Add inertia effect using requestAnimationFrame
  284. let velocity = deltaY;
  285. function applyInertia() {
  286. velocity *= 0.95; // Friction factor
  287. if (Math.abs(velocity) >= 0.1) {
  288. listRef.value.scrollTop -= velocity;
  289. requestId = requestAnimationFrame(applyInertia);
  290. }
  291. }
  292. requestId = requestAnimationFrame(applyInertia);
  293. }
  294. lastFinger = { x: finger.pageX, y: finger.pageY };
  295. }
  296. }
  297. }
  298. function handleMousemove(e) {
  299. currentPoint = { x: e.pageX, y: e.pageY };
  300. lastFinger = { ...currentPoint };
  301. }
  302. function handleTouch(e) {
  303. if (e.touches.length && e.touches.length == 1) {
  304. const finger = e.touches[0];
  305. currentPoint = { x: finger.pageX, y: finger.pageY };
  306. lastFinger = { ...currentPoint };
  307. }
  308. setAutoScrollTopPinSuppressedByUser(true);
  309. clearAutoScrollTopPin();
  310. cancelAutoScroll();
  311. }
  312. function handleScroll(e) {
  313. const rect = listRef.value.getBoundingClientRect();
  314. if (
  315. currentPoint.x > rect.left &&
  316. currentPoint.x < rect.left + rect.width &&
  317. currentPoint.y > rect.top &&
  318. currentPoint.y < rect.top + rect.height
  319. ) {
  320. setAutoScrollTopPinSuppressedByUser(true);
  321. listRef.value.scrollTo({ top: listRef.value.scrollTop + e.deltaY });
  322. e.preventDefault();
  323. }
  324. clearAutoScrollTopPin();
  325. cancelAutoScroll()
  326. }
  327. const isAutoScroll = ref(true);
  328. const handleScrollEvent = (event) => {
  329. const currentTop = listRef.value?.scrollTop ?? 0;
  330. if (lastScrollLogTop === currentTop) {
  331. return;
  332. }
  333. lastScrollLogTop = currentTop;
  334. const shouldSuppressUserScrollIntent =
  335. Boolean(event?.isTrusted) &&
  336. (isScrollAnimationActive || programmaticScrollKind !== null);
  337. if (shouldSuppressUserScrollIntent) {
  338. emit("scrollPositionChange", {
  339. scrollTop: currentTop,
  340. isTrusted: Boolean(event?.isTrusted),
  341. isProgrammaticScroll: true,
  342. });
  343. if (!isScrollAnimationActive && programmaticScrollKind === "single") {
  344. scheduleClearProgrammaticScroll();
  345. }
  346. return;
  347. }
  348. if (event?.isTrusted) {
  349. setAutoScrollTopPinSuppressedByUser(true);
  350. clearAutoScrollTopPin();
  351. cancelAutoScroll();
  352. }
  353. emit("scrollPositionChange", {
  354. scrollTop: currentTop,
  355. isTrusted: Boolean(event?.isTrusted),
  356. isProgrammaticScroll: false,
  357. });
  358. };
  359. const disableAutoScroll = () => {
  360. isAutoScroll.value = false;
  361. stopScrollFollow();
  362. };
  363. const scrollToBottom = () => {
  364. if (isAutoScroll.value) {
  365. beginProgrammaticScroll("single");
  366. listRef.value.scrollTo({
  367. top: listRef.value.scrollHeight,
  368. });
  369. }
  370. };
  371. // ── 流式贴底跟随(豆包式平滑滚动)──
  372. // smoothFollow 为真时用 rAF 缓动逼近底部;否则退化为原来的瞬时贴底。
  373. // ⚠️ 程序化设定 scrollTop 产生的是 trusted 滚动事件(Chromium),每帧都必须
  374. // beginProgrammaticScroll("single") 打标,否则自己的滚动会被当成用户滚动
  375. // → cancelAutoScroll → 循环自杀,且 pin 抑制标志会被污染。
  376. const FOLLOW_SCROLL_RATIO = 0.35;
  377. const FOLLOW_SNAP_DISTANCE = 1.5;
  378. let followFrameId = 0;
  379. const stopScrollFollow = () => {
  380. if (followFrameId) {
  381. cancelAnimationFrame(followFrameId);
  382. followFrameId = 0;
  383. }
  384. };
  385. const scrollToBottomFollow = () => {
  386. if (!listRef.value) {
  387. return;
  388. }
  389. if (!props.smoothFollow) {
  390. scrollToBottom();
  391. return;
  392. }
  393. // 幂等:已有一个循环在跑就交给它继续逼近;用户滚走后不再追。
  394. if (followFrameId || !isAutoScroll.value) {
  395. return;
  396. }
  397. const step = () => {
  398. followFrameId = 0;
  399. if (!listRef.value || !isAutoScroll.value || !props.smoothFollow) {
  400. clearProgrammaticScroll();
  401. return;
  402. }
  403. const maxScrollTop = Math.max(
  404. 0,
  405. listRef.value.scrollHeight - listRef.value.clientHeight
  406. );
  407. const distance = maxScrollTop - listRef.value.scrollTop;
  408. beginProgrammaticScroll("single");
  409. if (distance <= FOLLOW_SNAP_DISTANCE) {
  410. listRef.value.scrollTop = maxScrollTop;
  411. return;
  412. }
  413. listRef.value.scrollTop += distance * FOLLOW_SCROLL_RATIO;
  414. followFrameId = requestAnimationFrame(step);
  415. };
  416. followFrameId = requestAnimationFrame(step);
  417. };
  418. const scrollToBottomOnce = () => {
  419. const targetTop = Math.max(0, listRef.value.scrollHeight - listRef.value.clientHeight);
  420. return animateScrollTo(targetTop);
  421. };
  422. const scrollToBottomInstant = () => {
  423. beginProgrammaticScroll("single");
  424. listRef.value.scrollTo({
  425. top: listRef.value.scrollHeight,
  426. behavior: "auto"
  427. });
  428. };
  429. const scrollToPositionInstant = (top) => {
  430. if (!listRef.value || !Number.isFinite(top)) {
  431. return;
  432. }
  433. beginProgrammaticScroll("single");
  434. const maxScrollTop = Math.max(0, listRef.value.scrollHeight - listRef.value.clientHeight);
  435. const nextTop = Math.max(0, Math.min(maxScrollTop, top));
  436. listRef.value.scrollTo({
  437. top: nextTop,
  438. behavior: "auto",
  439. });
  440. };
  441. const scrollBy = (delta) => {
  442. if (!listRef.value || !Number.isFinite(delta) || delta === 0) {
  443. return;
  444. }
  445. beginProgrammaticScroll("single");
  446. const maxScrollTop = Math.max(0, listRef.value.scrollHeight - listRef.value.clientHeight);
  447. const nextTop = Math.max(0, Math.min(maxScrollTop, listRef.value.scrollTop + delta));
  448. listRef.value.scrollTo({
  449. top: nextTop,
  450. behavior: "auto",
  451. });
  452. };
  453. const scrollByAnimated = (delta, duration = scrollAnimationDurationMs) => {
  454. if (!listRef.value || !Number.isFinite(delta) || delta === 0) {
  455. return Promise.resolve();
  456. }
  457. const maxScrollTop = Math.max(0, listRef.value.scrollHeight - listRef.value.clientHeight);
  458. const startTop = listRef.value.scrollTop;
  459. const targetTop = Math.max(0, Math.min(maxScrollTop, startTop + delta));
  460. if (targetTop === startTop) {
  461. return Promise.resolve();
  462. }
  463. return animateScrollTo(targetTop, duration);
  464. };
  465. const getDistanceToBottom = () => {
  466. return Math.max(
  467. 0,
  468. listRef.value.scrollHeight - listRef.value.scrollTop - listRef.value.clientHeight
  469. );
  470. };
  471. const getScrollElement = () => listRef.value;
  472. const getScrollMetrics = () => {
  473. if (!listRef.value) {
  474. return null;
  475. }
  476. const scrollTop = listRef.value.scrollTop;
  477. const clientHeight = listRef.value.clientHeight;
  478. const scrollHeight = listRef.value.scrollHeight;
  479. return {
  480. scrollTop,
  481. clientHeight,
  482. scrollHeight,
  483. distanceToBottom: Math.max(0, scrollHeight - scrollTop - clientHeight),
  484. distanceToTop: Math.max(0, scrollTop),
  485. isAtBottom: scrollTop + clientHeight >= scrollHeight - 1,
  486. isAtTop: scrollTop <= 1,
  487. };
  488. };
  489. const isNearBottom = () => {
  490. if (!listRef.value) {
  491. return false;
  492. }
  493. return getDistanceToBottom() <= 1;
  494. };
  495. const cancelAutoScroll = () => {
  496. stopScrollFollow();
  497. cancelScrollAnimation();
  498. const isAtBottom = listRef.value.scrollTop + listRef.value.clientHeight >= listRef.value.scrollHeight;
  499. isAutoScroll.value = isAtBottom;
  500. };
  501. const enableAutoScroll = () => {
  502. isAutoScroll.value = true;
  503. if (isAutoScrollTopPinSuppressedByUser) {
  504. setAutoScrollTopPinSuppressedByUser(false);
  505. }
  506. };
  507. const isAutoScrollEnabled = () => isAutoScroll.value;
  508. const isProgrammaticScrollSuppressed = () => programmaticScrollKind !== null || isScrollAnimationActive;
  509. // 拖拽器相关
  510. let dragStartY = 0;
  511. let dragStartHeight = 0;
  512. function onDraggerTouchStart(e) {
  513. if (e.touches.length !== 1) return;
  514. isDragging.value = true;
  515. dragStartY = e.touches[0].clientY;
  516. dragStartHeight = listHeight.value;
  517. document.body.style.userSelect = "none";
  518. }
  519. function onDraggerTouchMove(e) {
  520. if (!isDragging.value) return;
  521. const deltaY = e.touches[0].clientY - dragStartY;
  522. let newHeight = dragStartHeight - deltaY;
  523. newHeight = Math.max(minListHeight, Math.min(maxListHeight.value, newHeight));
  524. listHeight.value = newHeight;
  525. }
  526. function onDraggerTouchEnd() {
  527. isDragging.value = false;
  528. document.body.style.userSelect = "";
  529. }
  530. function onDraggerMouseDown(e) {
  531. isDragging.value = true;
  532. dragStartY = e.clientY;
  533. dragStartHeight = listHeight.value;
  534. document.body.style.userSelect = "none";
  535. }
  536. function onDraggerMouseMove(e) {
  537. if (!isDragging.value) return;
  538. const deltaY = e.clientY - dragStartY;
  539. let newHeight = dragStartHeight + deltaY;
  540. newHeight = Math.max(minListHeight, Math.min(maxListHeight.value, newHeight));
  541. listHeight.value = newHeight;
  542. }
  543. function onDraggerMouseUp() {
  544. if (isDragging.value) {
  545. isDragging.value = false;
  546. document.body.style.userSelect = "";
  547. }
  548. }
  549. // 监听外部 height 变化
  550. watch(
  551. () => props.height,
  552. (val) => {
  553. if (typeof val === "number") {
  554. listHeight.value = val;
  555. }
  556. }
  557. );
  558. watch(
  559. () => bottomPaddingSize.value,
  560. async (nextBottomPadding, previousBottomPadding) => {
  561. if (nextBottomPadding <= previousBottomPadding) {
  562. return;
  563. }
  564. const shouldFollowBottom = isNearBottom();
  565. if (!shouldFollowBottom) {
  566. return;
  567. }
  568. await sleep(200);
  569. if (!listRef.value) {
  570. return;
  571. }
  572. scrollToBottomOnce();
  573. }
  574. );
  575. defineExpose({
  576. scrollToBottom,
  577. scrollToBottomFollow,
  578. scrollToBottomOnce,
  579. scrollToBottomInstant,
  580. scrollToPositionInstant,
  581. scrollBy,
  582. scrollByAnimated,
  583. getDistanceToBottom,
  584. getScrollElement,
  585. getScrollMetrics,
  586. enableAutoScroll,
  587. disableAutoScroll,
  588. isAutoScrollEnabled,
  589. isProgrammaticScrollSuppressed,
  590. setAutoScrollTopPinned,
  591. isAutoScrollTopPinned: isAutoScrollTopPinnedState,
  592. isAutoScrollTopPinSuppressedByUser: isAutoScrollTopPinSuppressedByUserState,
  593. cancelAutoScroll,
  594. cancelScrollAnimation,
  595. });
  596. </script>
  597. <style lang="scss">
  598. .list-scroll-container {
  599. position: relative;
  600. }
  601. .list-scroller {
  602. overflow: scroll;
  603. scrollbar-width: thin;
  604. scrollbar-color: transparent transparent;
  605. }
  606. .list-scroll-wrap:hover .list-scroller {
  607. scrollbar-color: rgba(0, 0, 0, 0.2) transparent;
  608. }
  609. .list-scroll-wrap {
  610. box-sizing: border-box;
  611. pointer-events: initial;
  612. /* transition 由绑定的 style 控制 */
  613. position: relative;
  614. }
  615. .list-wrap {
  616. box-sizing: border-box;
  617. display: flex;
  618. flex-direction: column;
  619. align-items: flex-start;
  620. transition: all 0.3s ease-in-out;
  621. }
  622. .list-dragger {
  623. position: absolute;
  624. left: 0;
  625. right: 0;
  626. bottom: 0;
  627. height: 20px;
  628. display: flex;
  629. align-items: center;
  630. justify-content: center;
  631. z-index: 10;
  632. touch-action: none;
  633. cursor: ns-resize;
  634. transform: translateY(-100%);
  635. user-select: none;
  636. }
  637. .list-dragger-bar {
  638. margin: 0 auto;
  639. &::before {
  640. content: "";
  641. position: absolute;
  642. top: 50%;
  643. left: 50%;
  644. transform: translate(-50%, -50%);
  645. width: 64px;
  646. height: 2px;
  647. box-sizing: content-box;
  648. border-width: 2px;
  649. border-style: solid;
  650. border-color: hsla(0, 0%, 100%, 1);
  651. border-left: none;
  652. border-right: none;
  653. }
  654. }
  655. </style>