How to use @better-scroll/shared-utils - 10 common examples

To help you get started, we’ve selected a few @better-scroll/shared-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ustbhuangyi / better-scroll / packages / scroll-bar / src / __tests__ / indicator.spec.ts View on Github external
it('should update position and size correctly when direction is horizontal', () => {
      // given

      indicatorOptions.direction = 'horizontal' as Direction
      indicator = new Indicator(bscroll, indicatorOptions)
      // when
      bscroll.trigger('refresh')
      // then
      expect(indicator.el.style.width).toBe('50px')
      expect(indicator.el.style[style.transform as any]).toBe('translateX(5px)')
    })
github ustbhuangyi / better-scroll / packages / core / src / __mocks__ / index.ts View on Github external
'refresh',
    'enable',
    'disable',
    'destroy',
    // scroller
    'beforeScrollStart',
    'scrollStart',
    'scroll',
    'scrollEnd',
    'touchEnd',
    'flick'
  ])
  const res = {
    wrapper: wrapper,
    options: options,
    hooks: new EventEmitter([
      'init',
      'refresh',
      'enable',
      'disable',
      'destroy'
    ]),
    scroller: new Scroller(wrapper, options),
    // own methods
    proxy: jest.fn(),
    refresh: jest.fn(),
    // proxy methods
    scrollTo: jest.fn(),
    resetPosition: jest.fn(),
    registerType: jest.fn().mockImplementation((names: string[]) => {
      names.forEach(name => {
        const eventTypes = eventEmitter.eventTypes
github ustbhuangyi / better-scroll / packages / wheel / src / index.ts View on Github external
refresh() {
    const scroller = this.scroll.scroller
    const scrollBehaviorY = scroller.scrollBehaviorY

    // adjust contentSize
    const contentRect = getRect(scroller.content)
    scrollBehaviorY.contentSize = contentRect.height

    this.items = scroller.content.children
    this.checkWheelAllDisabled()

    this.itemHeight = this.items.length
      ? scrollBehaviorY.contentSize / this.items.length
      : 0

    if (this.selectedIndex === undefined) {
      this.selectedIndex = this.options.selectedIndex || 0
    }

    this.scroll.maxScrollX = 0
    this.scroll.maxScrollY = -this.itemHeight * (this.items.length - 1)
    this.scroll.minScrollX = 0
github ustbhuangyi / better-scroll / packages / core / src / scroller / Scroller.ts View on Github external
private checkClick(e: TouchEvent) {
    // when in the process of pulling down, it should not prevent click
    const cancelable = {
      preventClick: this.animater.forceStopped
    }

    // we scrolled less than momentumLimitDistance pixels
    if (this.hooks.trigger(this.hooks.eventTypes.checkClick)) return true
    if (!cancelable.preventClick) {
      const _dblclick = this.options.dblclick
      let dblclickTrigged = false
      if (_dblclick && this.lastClickTime) {
        const { delay = 300 } = _dblclick as any
        if (getNow() - this.lastClickTime < delay) {
          dblclickTrigged = true
          dblclick(e)
        }
      }
      if (this.options.tap) {
        tap(e, this.options.tap)
      }
      if (
        this.options.click &&
        !preventDefaultExceptionFn(
          e.target,
          this.options.preventDefaultException
        )
      ) {
        click(e)
      }
github ustbhuangyi / better-scroll / packages / core / src / scroller / Actions.ts View on Github external
private handleStart(e: TouchEvent) {
    const timestamp = getNow()
    this.moved = false

    this.startTime = timestamp

    this.directionLockAction.reset()

    this.scrollBehaviorX.start()
    this.scrollBehaviorY.start()

    // force stopping last transition or animation
    this.animater.stop()

    this.scrollBehaviorX.resetStartPos()
    this.scrollBehaviorY.resetStartPos()

    this.hooks.trigger(this.hooks.eventTypes.start, e)
github ustbhuangyi / better-scroll / packages / core / src / scroller / Actions.ts View on Github external
private handleMove(deltaX: number, deltaY: number, e: TouchEvent) {
    if (this.hooks.trigger(this.hooks.eventTypes.beforeMove, e)) {
      return
    }

    const absDistX = this.scrollBehaviorX.getAbsDist(deltaX)
    const absDistY = this.scrollBehaviorY.getAbsDist(deltaY)
    const timestamp = getNow()

    // We need to move at least momentumLimitDistance pixels
    // for the scrolling to initiate
    if (this.checkMomentum(absDistX, absDistY, timestamp)) {
      return true
    }
    if (this.directionLockAction.checkMovingDirection(absDistX, absDistY, e)) {
      this.actionsHandler.setInitiated()
      return true
    }

    const delta = this.directionLockAction.adjustDelta(deltaX, deltaY)

    const newX = this.scrollBehaviorX.move(delta.deltaX)
    const newY = this.scrollBehaviorY.move(delta.deltaY)
github ustbhuangyi / better-scroll / packages / mouse-wheel / src / index.ts View on Github external
private getEaseTime() {
    const DEFAULT_EASETIME = 300
    const SAFE_EASETIME = 100
    const easeTime = this.mouseWheelOpt.easeTime || DEFAULT_EASETIME
    // scrollEnd event will be triggered in every calling of scrollTo when easeTime is too small
    // easeTime needs to be greater than 100
    if (easeTime < SAFE_EASETIME) {
      warn(`easeTime should be greater than 100.
      If mouseWheel easeTime is too small, scrollEnd will be triggered many times.`)
    }
    return easeTime
  }
}
github ustbhuangyi / better-scroll / packages / core / src / scroller / Behavior.ts View on Github external
move(delta: number) {
    delta = this.hasScroll ? delta : 0
    this.movingDirection =
      delta > 0
        ? Direction.Negative
        : delta < 0
        ? Direction.Positive
        : Direction.Default

    let newPos = this.currentPos + delta

    // Slow down or stop if outside of the boundaries
    if (newPos > this.minScrollPos || newPos < this.maxScrollPos) {
      if (
        (newPos > this.minScrollPos && this.options.bounces[0]) ||
        (newPos < this.maxScrollPos && this.options.bounces[1])
      ) {
        newPos = this.currentPos + delta / 3
      } else {
        newPos =
          newPos > this.minScrollPos ? this.minScrollPos : this.maxScrollPos
github ustbhuangyi / better-scroll / packages / core / src / scroller / Behavior.ts View on Github external
let momentumInfo: {
      destination?: number
      duration?: number
    } = {
      duration: 0
    }

    const absDist = Math.abs(this.currentPos - this.startPos)
    // start momentum animation if needed
    if (
      this.options.momentum &&
      duration < this.options.momentumLimitTime &&
      absDist > this.options.momentumLimitDistance
    ) {
      const wrapperSize =
        (this.direction === Direction.Negative && this.options.bounces[0]) ||
        (this.direction === Direction.Positive && this.options.bounces[1])
          ? this.wrapperSize
          : 0

      momentumInfo = this.hasScroll
        ? this.momentum(
            this.currentPos,
            this.startPos,
            duration,
            this.maxScrollPos,
            this.minScrollPos,
            wrapperSize,
            this.options
          )
        : { destination: this.currentPos, duration: 0 }
    } else {
github ustbhuangyi / better-scroll / packages / pull-down / src / index.ts View on Github external
private _checkPullDown() {
    if (!this.scroll.options.pullDownRefresh) {
      return
    }

    const { threshold = 90, stop = 40 } = this.scroll.options
      .pullDownRefresh as PullDownRefreshConfig

    // check if a real pull down action
    if (
      this.scroll.directionY !== Direction.Negative ||
      this.scroll.y < threshold
    ) {
      return false
    }

    if (!this.pulling) {
      this.pulling = true
      this.scroll.trigger('pullingDown')

      this.originalMinScrollY = this.scroll.minScrollY
      this.scroll.minScrollY = stop
    }

    this.scroll.scrollTo(
      this.scroll.x,
      stop,