Javascript HammerJS拉刷新

Javascript HammerJS拉刷新,javascript,hammer.js,Javascript,Hammer.js,我对Javascript知之甚少,但需要在我正在进行的项目中使用hammer.js 我尝试使用pull to refresh scrit刷新页面,而不是替换/刷新图像 我试着加上 location.reload(); 但是没有用,任何人都可以解释这一点 这是我使用的代码 /** * requestAnimationFrame and cancel polyfill */ (function() { var lastTime = 0; var vend

我对Javascript知之甚少,但需要在我正在进行的项目中使用hammer.js

我尝试使用pull to refresh scrit刷新页面,而不是替换/刷新图像

我试着加上

   location.reload();
但是没有用,任何人都可以解释这一点

这是我使用的代码

        /**
 * requestAnimationFrame and cancel polyfill
 */
(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame =
                window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
                    timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());


/**
 * pull to refresh
 * @type {*}
 */
var PullToRefresh = (function() {
    function Main(container, slidebox, slidebox_icon, handler) {
        var self = this;

        this.breakpoint = 80;

        this.container = container;
        this.slidebox = slidebox;
        this.slidebox_icon = slidebox_icon;
        this.handler = handler;

        this._slidedown_height = 0;
        this._anim = null;
        this._dragged_down = false;

        this.hammertime = Hammer(this.container)
            .on("touch dragdown release", function(ev) {
                self.handleHammer(ev);
            });
    };


    /**
     * Handle HammerJS callback
     * @param ev
     */
    Main.prototype.handleHammer = function(ev) {
        var self = this;

        switch(ev.type) {
            // reset element on start
            case 'touch':
                this.hide();
                break;

            // on release we check how far we dragged
            case 'release':
                if(!this._dragged_down) {
                    return;
                }

                // cancel animation
                cancelAnimationFrame(this._anim);

                // over the breakpoint, trigger the callback
                if(ev.gesture.deltaY >= this.breakpoint) {
                    container_el.className = 'pullrefresh-loading';
                    pullrefresh_icon_el.className = 'icon loading';

                    this.setHeight(60);
                    this.handler.call(this);
                }
                // just hide it
                else {
                    pullrefresh_el.className = 'slideup';
                    container_el.className = 'pullrefresh-slideup';

                    this.hide();
                }
                break;

            // when we dragdown
            case 'dragdown':
                this._dragged_down = true;

                // if we are not at the top move down
                var scrollY = window.scrollY;
                if(scrollY > 5) {
                    return;
                } else if(scrollY !== 0) {
                    window.scrollTo(0,0);
                }

                // no requestAnimationFrame instance is running, start one
                if(!this._anim) {
                    this.updateHeight();
                }

                // stop browser scrolling
                ev.gesture.preventDefault();

                // update slidedown height
                // it will be updated when requestAnimationFrame is called
                this._slidedown_height = ev.gesture.deltaY * 0.4;
                break;
        }
    };


    /**
     * when we set the height, we just change the container y
     * @param   {Number}    height
     */
    Main.prototype.setHeight = function(height) {
        if(Modernizr.csstransforms3d) {
            this.container.style.transform = 'translate3d(0,'+height+'px,0) ';
            this.container.style.oTransform = 'translate3d(0,'+height+'px,0)';
            this.container.style.msTransform = 'translate3d(0,'+height+'px,0)';
            this.container.style.mozTransform = 'translate3d(0,'+height+'px,0)';
            this.container.style.webkitTransform = 'translate3d(0,'+height+'px,0) scale3d(1,1,1)';
        }
        else if(Modernizr.csstransforms) {
            this.container.style.transform = 'translate(0,'+height+'px) ';
            this.container.style.oTransform = 'translate(0,'+height+'px)';
            this.container.style.msTransform = 'translate(0,'+height+'px)';
            this.container.style.mozTransform = 'translate(0,'+height+'px)';
            this.container.style.webkitTransform = 'translate(0,'+height+'px)';
        }
        else {
            this.container.style.top = height+"px";
        }
    };


    /**
     * hide the pullrefresh message and reset the vars
     */
    Main.prototype.hide = function() {
        container_el.className = '';
        this._slidedown_height = 0;
        this.setHeight(0);
        cancelAnimationFrame(this._anim);
        this._anim = null;
        this._dragged_down = false;
    };


    /**
     * hide the pullrefresh message and reset the vars
     */
    Main.prototype.slideUp = function() {
        var self = this;
        cancelAnimationFrame(this._anim);

        pullrefresh_el.className = 'slideup';
        container_el.className = 'pullrefresh-slideup';

        this.setHeight(0);

        setTimeout(function() {
            self.hide();
        }, 500);
    };


    /**
     * update the height of the slidedown message
     */
    Main.prototype.updateHeight = function() {
        var self = this;

        this.setHeight(this._slidedown_height);

        if(this._slidedown_height >= this.breakpoint){
            this.slidebox.className = 'breakpoint';
            this.slidebox_icon.className = 'icon arrow arrow-up';
        }
        else {
            this.slidebox.className = '';
            this.slidebox_icon.className = 'icon arrow';
        }

        this._anim = requestAnimationFrame(function() {
            self.updateHeight();
        });
    };

    return Main;
})();



function getEl(id) {
    return document.getElementById(id);
}

var container_el = getEl('container');
var pullrefresh_el = getEl('pullrefresh');
var pullrefresh_icon_el = getEl('pullrefresh-icon');
var image_el = getEl('random-image');

var refresh = new PullToRefresh(container_el, pullrefresh_el, pullrefresh_icon_el);

// update image onrefresh
refresh.handler = function() {
    var self = this;
    // a small timeout to demo the loading state
    setTimeout(function() {
        var preload = new Image();
        preload.onload = function() {
            image_el.src = this.src;
            self.slideUp();
        };
        preload.src = 'http://lorempixel.com/800/600/?'+ (new Date().getTime());
    }, 1000);
};
/**
*请求动画帧并取消多边形填充
*/
(功能(){
var lastTime=0;
var供应商=['ms','moz','webkit','o'];
对于(var x=0;x=此.breakpoint){
container_el.className='pullrefresh loading';
pullrefresh_icon_el.className='图标加载';
这是设定高度(60);
this.handler.call(this);
}
//把它藏起来
否则{
pullrefresh_el.className='slideup';
container_el.className='pullrefresh slideup';
this.hide();
}
打破
//当我们下山的时候
案例“dragdown”:
这个._drawed_down=true;
//如果我们不在顶部,向下移动
var scrollY=window.scrollY;
如果(滚动>5){
返回;
}else if(滚动!==0){
滚动到(0,0);
}
//没有运行requestAnimationFrame实例,请启动一个实例
如果(!这个._anim){
this.updateHeight();
}
//停止浏览器滚动
ev.signate.preventDefault();
//更新向下滑动高度
//它将在调用requestAnimationFrame时更新
这个。_slidedown_height=ev.signature.deltaY*0.4;
打破
}
};
/**
*设置高度时,只需更改容器y
*@param{Number}高度
*/
Main.prototype.setHeight=功能(高度){
if(现代化CSTRANSFORMS3D){
this.container.style.transform='translate3d(0',+height+'px,0');
this.container.style.oTransform='translate3d(0,+height+'px,0');
this.container.style.msTransform='translate3d(0,+height+'px,0');
this.container.style.mozTransform='translate3d(0,+height+'px,0');
this.container.style.webkitttransform='translate3d(0',+height+'px,0)scale3d(1,1,1)';
}
else if(现代化CSTRANSFORMS){
this.container.style.transform='translate(0,+height+'px');
this.container.style.oTransform='translate(0,+height+'px');
this.container.style.msTransform='translate(0,+height+'px');
this.container.style.mozTransform='translate(0,+height+'px');
this.container.style.webkitttransform='translate(0,+height+'px)';
}
否则{
this.container.style.top=高度+px;
}
};
/**
*隐藏pullrefresh消息并重置VAR
*/
Main.prototype.hide=函数(){
容器类名称=“”;
这是。_slidedown_height=0;
此值为0.setHeight(0);
取消动画帧(此动画);
这是。_anim=null;
这个._draugh_down=false;
};
/**
*隐藏pullrefresh消息并重置VAR
*/
Main.prototype.slideUp=函数(){
var self=这个;
取消动画帧(此动画);
pullrefresh_el.className='slideup';
container_el.className='pullrefresh slideup';
此值为0.setHeight(0);
setTimeout(函数(){
self.hide();
}, 500);
};
/**
*更新向下滑动消息的高度
*/
Main.prototype.updateHeight=函数(){
v
// update image onrefresh
refresh.handler = function() {
    var self = this;
    // a small timeout to demo the loading state
    setTimeout(function() {
        window.location.reload();
    }, 1000);
};