啊鑫
7 天以前 fca192d3c38c5dcfbb6ace8bc71d6078f6a079b2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"use strict";
 
exports.__esModule = true;
exports.scrollLeftTo = scrollLeftTo;
exports.scrollTopTo = scrollTopTo;
 
var _raf = require("../utils/dom/raf");
 
var _scroll = require("../utils/dom/scroll");
 
function scrollLeftTo(scroller, to, duration) {
  var count = 0;
  var from = scroller.scrollLeft;
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
 
  function animate() {
    scroller.scrollLeft += (to - from) / frames;
 
    if (++count < frames) {
      (0, _raf.raf)(animate);
    }
  }
 
  animate();
}
 
function scrollTopTo(scroller, to, duration, callback) {
  var current = (0, _scroll.getScrollTop)(scroller);
  var isDown = current < to;
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  var step = (to - current) / frames;
 
  function animate() {
    current += step;
 
    if (isDown && current > to || !isDown && current < to) {
      current = to;
    }
 
    (0, _scroll.setScrollTop)(scroller, current);
 
    if (isDown && current < to || !isDown && current > to) {
      (0, _raf.raf)(animate);
    } else if (callback) {
      (0, _raf.raf)(callback);
    }
  }
 
  animate();
}