all files / src/util/ momentum.js

93.33% Statements 14/15
68.75% Branches 11/16
100% Functions 1/1
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                           
export function momentum(current, start, time, lowerMargin, upperMargin, wrapperSize, options, scroll) {
  let distance = current - start
  let speed = Math.abs(distance) / time
 
  let {deceleration, itemHeight, swipeBounceTime, wheel, swipeTime} = options
  let duration = swipeTime
  let rate = wheel ? 4 : 15
 
  let destination = current + speed / deceleration * (distance < 0 ? -1 : 1)
 
  Iif (wheel && itemHeight) {
    destination = scroll._findNearestValidWheel(destination).y
  }
 
  if (destination < lowerMargin) {
    destination = wrapperSize ? Math.max(lowerMargin - wrapperSize / 4, lowerMargin - (wrapperSize / rate * speed)) : lowerMargin
    duration = swipeBounceTime
  } else if (destination > upperMargin) {
    destination = wrapperSize ? Math.min(upperMargin + wrapperSize / 4, upperMargin + wrapperSize / rate * speed) : upperMargin
    duration = swipeBounceTime
  }
 
  return {
    destination: Math.round(destination),
    duration
  }
}