js面向对象编程2

定义BackTop,引用scrollto

define(['jquery','scrollto'],function($,scrollto){

  function BackTop(el,opts){//定义构造函数

    this.opts=$.extend({},BackTop.DEFAULTS,opts);

    this.$el=$(el);

 

    this.scroll=new scroll.ScrollTo({

      dest:0,

      speed:this.opts.speed

    })

 

    this._checkPosition();

    $(window).on('scroll',$.proxy(this._checkPosition,this));    

 

 

 

    if(this.opts.mode=='move'){

      this.$el.on('click',$.proxy(this._move,this));

    }else{

      this.$el.on('click',$.proxy(this._go,this));

    }

  }

 

  BackTop.prototype._move=function(){

    this.scroll.move();

  }

 

  BackTop.prototype._go=function(){

    this.scroll.go();

  }

 

  BackTop.prototype._checkPosition=function(){

    var $el=this.$el;

    if($(window).scrollTop()>this.opts.pos){

      $el.fadeIn();

    }else{

      $el.fadeOut();

    }

  };

 

  return{

    BackTop:BackTop;

  }

 

 

})

posted @ 2015-07-08 16:49  bjsunhe  阅读(157)  评论(0)    收藏  举报