import { Component, ComponentInterface, Element, Event, EventEmitter, Host, Listen, Method, Prop, Watch, h } from '@stencil/core' import classNames from 'classnames' import { debounce, handleStencilNodes } from '../../utils' import type { ScrollViewContext } from '@tarojs/taro' function easeOutScroll (from: number, to: number, duration = 500, callback?) { if (from === to || typeof from !== 'number') { return } const change = to - from const sTime = Date.now() const isLarger = to >= from function linear (t: number, b: number, c: number, d: number): number { return c * t / d + b } function step () { from = linear(Date.now() - sTime, from, change, duration) if ((isLarger && from >= to) || (!isLarger && to >= from)) { callback(to) return } callback(from) requestAnimationFrame(step) } step() } @Component({ tag: 'taro-scroll-view-core', styleUrl: './style/index.scss' }) export class ScrollView implements ComponentInterface { private _scrollLeft = 0 private _scrollTop = 0 @Element() el: HTMLElement @Prop() scrollX = false @Prop() scrollY = false @Prop() upperThreshold: number | string = 50 @Prop() lowerThreshold: number | string = 50 @Prop({ attribute: 'scroll-top', reflect: true }) mpScrollTop: number | string @Prop({ attribute: 'scroll-left', reflect: true }) mpScrollLeft: number | string @Prop({ attribute: 'scroll-into-view', reflect: true }) mpScrollIntoView: string @Prop({ attribute: 'scroll-into-view-alignment' }) mpScrollIntoViewAlignment: 'start' | 'center' | 'end' | 'nearest' @Prop({ attribute: 'scroll-with-animation' }) animated = false @Prop() enhanced = false @Prop() showScrollbar = true @Prop() stopTouchMovePropagation = true @Event({ eventName: 'scroll', bubbles: false }) onScroll: EventEmitter @Event({ eventName: 'scrolltoupper', bubbles: false }) onScrollToUpper: EventEmitter @Event({ eventName: 'scrolltolower', bubbles: false }) onScrollToLower: EventEmitter @Watch('mpScrollLeft') watchScrollLeft (newVal) { const left = Number(newVal) const { animated } = this this.mpScrollToMethod({ left, animated }) } @Watch('mpScrollTop') watchScrollTop (newVal) { const top = Number(newVal) const { animated } = this this.mpScrollToMethod({ top, animated }) } @Watch('mpScrollIntoView') watchScrollIntoView (newVal) { this.mpScrollIntoViewMethod(newVal) } @Listen('scroll') handleScroll (e: Event) { if (e instanceof CustomEvent) return e.stopPropagation() e.stopImmediatePropagation?.() const { scrollLeft, scrollTop, scrollHeight, scrollWidth } = this.el this._scrollLeft = scrollLeft this._scrollTop = scrollTop this.upperAndLower() this.onScroll.emit({ scrollLeft, scrollTop, scrollHeight, scrollWidth }) } @Listen('touchmove') handleTouchMove (e: Event) { if (e instanceof CustomEvent) return if (this.stopTouchMovePropagation) { e.stopPropagation() } } @Method() async mpScrollToMethod(object: ScrollViewContext.scrollTo.Option) { let { top, left, duration, animated = false } = object if (this.scrollY && typeof top === 'number' && !isNaN(top) && top !== this._scrollTop) { if (animated) { easeOutScroll(this._scrollTop, top, duration, pos => (this.el.scrollTop = pos)) } else { this.el.scrollTop = top } this._scrollTop = top } if (this.scrollX && typeof left === 'number' && !isNaN(left) && left !== this._scrollLeft) { if (animated) { easeOutScroll(this._scrollLeft, left, duration, pos => (this.el.scrollLeft = pos)) } else { this.el.scrollLeft = left } this._scrollLeft = left } } @Method() async mpScrollIntoViewMethod(selector: string) { if (typeof selector === 'string' && selector) { // 未开启的滚动轴使用 nearest 避免原生 scrollIntoView 沿该轴滚动整页 document.querySelector(`#${selector}`)?.scrollIntoView({ behavior: this.animated ? 'smooth' : 'auto', block: this.scrollY ? (this.mpScrollIntoViewAlignment || 'center') : 'nearest', inline: this.scrollX ? (this.mpScrollIntoViewAlignment || 'start') : 'nearest' }) } } componentDidLoad () { const top = Number(this.mpScrollTop) const left = Number(this.mpScrollLeft) const { animated } = this this.mpScrollToMethod({ top, left, animated }) } upperAndLower = debounce(() => { const { offsetWidth, offsetHeight, scrollLeft, scrollTop, scrollHeight, scrollWidth } = this.el const lowerThreshold = Number(this.lowerThreshold) const upperThreshold = Number(this.upperThreshold) if ( !isNaN(lowerThreshold) && ((this.scrollY && offsetHeight + scrollTop + lowerThreshold >= scrollHeight) || (this.scrollX && offsetWidth + scrollLeft + lowerThreshold >= scrollWidth)) ) { this.onScrollToLower.emit({ direction: this.scrollX ? 'right' : (this.scrollY ? 'bottom' : '') }) } if ( !isNaN(upperThreshold) && ((this.scrollY && scrollTop <= upperThreshold) || (this.scrollX && scrollLeft <= upperThreshold)) ) { this.onScrollToUpper.emit({ direction: this.scrollX ? 'left' : (this.scrollY ? 'top' : '') }) } }, 200) componentDidRender () { handleStencilNodes(this.el) } render () { const { scrollX, scrollY, enhanced, showScrollbar } = this const cls = classNames({ 'taro-scroll-view__scroll-x': scrollX, 'taro-scroll-view__scroll-y': scrollY, 'taro-scroll--hidebar': enhanced === true && showScrollbar === false, 'taro-scroll--enhanced': enhanced === true }) return ( ) } }