scrollView

function(w, $) {
    var ScrollView = function(opt) {
        var self = this;
        $.extend(this, opt);
        var start;
        this.$wrap.addClass("am-touchable").bind({
            vtouchstart: function(e, p) {
                return start = [p.x, p.y],
                self._start(),
                self.bubble ? void 0 : !1
            },
            vtouchmove: function(e, p) {
                return self._move([p.x - start[0], p.y - start[1]]),
                self.bubble ? void 0 : !1
            },
            vtouchend: function(e, p) {
                return self._end(),
                self.bubble ? void 0 : !1
            }
        }),
        this.direction = this.direction || [!1, !0],
        this._startPos = [0, 0],
        this._currentPos = [0, 0],
        this._endPos = [0, 0],
        this._speed = [0, 0],
        this._lastTimeStamp = null ,
        this._timer = null ,
        this.setMax(),
        this.vt = [new MomentumTracker, new MomentumTracker]
    }
    ;
    $.extend(ScrollView.prototype, {
        _start: function() {
            this._stopAnimation(),
            $.extend(this._startPos, this._currentPos),
            this._speed = [0, 0],
            this.overTop = 0,
            this.overBottom = 0,
            this._overT = 0,
            this._overB = 0,
            this.deraction = 0
        },
        _move: function(displacement) {
            var nowTimeStamp = new Date
              , dt = nowTimeStamp - this._lastTimeStamp;
            dt = 10 > dt ? 10 : dt;
            for (var newPos = [0, 0], ds = [], min = $.extend([], this._min), max = $.extend([], this._max), i = 0; i < this.direction.length; i++)
                if (this.direction[i]) {
                    if (newPos[i] = this._startPos[i] + displacement[i],
                    ds[i] = newPos[i] - this._currentPos[i],
                    newPos[i] > max[i])
                        if (this._originalMax && this.touchTop)
                            newPos[i] = this._currentPos[i],
                            ds[i] = 0;
                        else {
                            var n = 1 - this._currentPos[i] / 55;
                            n = 0 > n ? 0 : n,
                            newPos[i] = this._currentPos[i] + ((newPos[i] + max[i]) / 2 - this._currentPos[i]) * n,
                            this.touchTop && !this.pauseTouchTop && (!this.overTop && .1 > n ? (this.overTop = (new Date).getTime(),
                            this.touchTop && this.touchTop(1)) : this.overTop > 0 && (new Date).getTime() - this.overTop > 100 && (this.touchTop && this.touchTop(2),
                            this.overTop = -1),
                            this._overT || (this._overT = 1,
                            this.touchTop && this.touchTop(0)))
                        }
                    else if (newPos[i] < min[i])
                        if (this._originalMin && this.touchBottom)
                            newPos[i] = this._currentPos[i],
                            ds[i] = 0;
                        else {
                            var n = 1 - (min[i] - this._currentPos[i]) / 55;
                            n = 0 > n ? 0 : n,
                            newPos[i] = this._currentPos[i] + ((newPos[i] + min[i]) / 2 - this._currentPos[i]) * n,
                            this.touchBottom && !this.pauseTouchBottom && (!this.overBottom && .1 > n ? (this.overBottom = (new Date).getTime(),
                            this.touchBottom && this.touchBottom(1)) : this.overBottom > 0 && (new Date).getTime() - this.overBottom > 100 && (this.touchBottom && this.touchBottom(2),
                            this.overBottom = -1),
                            this._overB || (this._overB = 1,
                            this.touchBottom && this.touchBottom(0)))
                        }
                    this._speed[i] = ds[i] / dt
                }
            if (this._lastTimeStamp = nowTimeStamp,
            0 == this.deraction) {
                var x = newPos[0] - this._startPos[0]
                  , y = newPos[1] - this._startPos[1];
                Math.abs(x) > Math.abs(y) ? (newPos[1] = this._startPos[1],
                this._speed[1] = 0,
                this.deraction = "x") : (this.deraction = "y",
                newPos[0] = this._startPos[0],
                this._speed[0] = 0)
            } else
                "x" == this.deraction ? (newPos[1] = this._startPos[1],
                this._speed[1] = 0) : "y" == this.deraction && (newPos[0] = this._startPos[0],
                this._speed[0] = 0);
            this._currentPos[0] = newPos[0],
            this._currentPos[1] = newPos[1],
            this._onupdate(newPos)
        },
        _end: function() {
            return this.touchEndTimer && clearTimeout(this.touchEndTimer),
            $.extend(this._endPos, this._currentPos),
            this.touchTop && !this.pauseTouchTop && this.overTop > 0 && (new Date).getTime() - this.overTop > 100 && (this.overTop = -1,
            this.touchTop && this.touchTop(2)),
            this.touchBottom && !this.pauseTouchBottom && this.overBottom > 0 && (new Date).getTime() - this.overBottom > 100 && (this.overBottom = -1,
            this.touchBottom && this.touchBottom(2)),
            !this.touchTop || this.pauseTouchTop || -1 != this.overTop || this._originalMax ? !this.touchBottom || this.pauseTouchBottom || -1 != this.overBottom || this._originalMin ? void this._startAnimation() : (this._originalMin = [this._min[0], this._min[1]],
            this._min = [0, this._min[1] - 55],
            this.overBottom = -2,
            void this.touchBottom(3)) : (this._originalMax = [this._max[0], this._max[1]],
            this._max = [0, 55],
            this.overTop = -2,
            void this.touchTop(3))
        },
        _startAnimation: function() {
            for (var self = this, min = $.extend([], this._min), max = $.extend([], this._max), startDisplacement = this._endPos, startSpeed = $.extend([], this._speed), vt = [], direction = this.direction, i = 0; i < direction.length; i++)
                direction[i] && (startSpeed[i] = startSpeed[i] <= .4 && startSpeed[i] >= -.4 ? 0 : startSpeed[i],
                vt[i] = this.vt[i],
                vt[i].start(startDisplacement[i], startSpeed[i], 2e3, min[i], max[i]));
            this._timer && clearInterval(this._timer),
            this._timer = setInterval(function() {
                for (var done = [!0, !0], p = [0, 0], i = 0; i < direction.length; i++)
                    direction[i] && (p[i] = vt[i].update(),
                    done[i] = vt[i].done());
                self._onupdate(p),
                self._currentPos = p,
                done[0] && done[1] && self._stopAnimation()
            }, 10)
        },
        _stopAnimation: function() {
            this._timer ? clearInterval(this._timer) : this._timer = null
        },
        _onupdate: function(pos) {
            this.$inner.setTransformPos(pos, "xy", this.hasInput)
        },
        scrollTo: function(pos) {
            var min = $.extend([], this._min)
              , max = $.extend([], this._max);
            switch (pos) {
            case "top":
                pos = max;
                break;
            case "bottom":
                pos = min
            }
            pos[0] = pos[0] < min[0] ? min[0] : pos[0],
            pos[1] = pos[1] < min[1] ? min[1] : pos[1],
            pos[0] = pos[0] > max[0] ? max[0] : pos[0],
            pos[1] = pos[1] > max[1] ? max[1] : pos[1],
            this._stopAnimation(),
            this._currentPos = pos,
            this._onupdate(pos)
        },
        refresh: function(cb) {
            if (this.isRefreshing)
                return !1;
            this.isRefreshing = !0;
            var self = this
              , last = [0, 0, 0, 0]
              , lastTime = 0
              , timer = null
              , step = function() {
                var now = (new Date).getTime()
                  , w = self.$inner.outerWidth(!0)
                  , h = self.$inner.outerHeight(!0)
                  , ow = self.$wrap.width();
                self.$wrap.height();
                w != last[0] && (last[0] = w,
                lastTime = now),
                h != last[1] && (last[1] = h,
                lastTime = now),
                ow != last[2] && (last[2] = ow,
                lastTime = now),
                ow != last[3] && (last[3] = ow,
                lastTime = now),
                now - lastTime > 150 ? (self.isRefreshing = !1,
                self.setMax(),
                clearTimeout(timer),
                timer = null ,
                cb && cb()) : setTimeout(step, 50)
            }
            ;
            step()
        },
        setMax: function() {
            var dx = this.$wrap.width() - this.$inner.outerWidth(!0)
              , dy = this.$wrap.height() - this.$inner.outerHeight(!0);
            this._originalMin ? this._originalMin = [dx > 0 ? 0 : dx, dy > 0 ? 0 : dy] : this._min = [dx > 0 ? 0 : dx, dy > 0 ? 0 : dy],
            this._originalMax ? this._originalMax = [0, 0] : this._max = [0, 0]
        }
    }),
    $.am.ScrollView = ScrollView;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值