按照老師的代碼寫的,只是兩個js文件寫在一起了.
define(["jquery"],?function?($)?{
????function?BackTop(el,?opts)?{
????????this.opts?=?$.extend({},?BackTop.DEFAULTS,?opts);
????????this.$el?=?el;
????????this.scroll?=?$("html,body");
????????console.log(this.opts.mode);
????????if?(this.opts.mode?===?"move")?{
????????????this.$el.on("click",?$.proxy(this._move,?this));
????????}?else?{
????????????this.$el.on("click",?$.proxy(this._go,?this));
????????}
????????$(window).on("scroll",?$.proxy(this._check,?this));
????????$(function(){this._check;});
????};
????BackTop.DEFAULTS?=?{
????????mode:?"move",
????????pos:?0,
????????speed:?800
????};
????BackTop.prototype._move?=?function?()?{
????????if?($(window).scrollTop()?!==?0)?{
????????????if?(!this.scroll.is(":animated"))?{
????????????????this.scroll.animate({
????????????????????scrollTop:?0
????????????????},?this.opts.speed);
????????????}
????????}
????};
????BackTop.prototype._go?=?function?()?{
????????if?($(window).scrollTop()?!==?0)?{
????????this.scroll.scrollTop(0);
????????}
????};
????BackTop.prototype._check?=?function?()?{
????????var?$el?=?this.$el;
????????if?($(window).scrollTop()?>?this.opts.pos)?{
????????????$el.fadeIn();
????????}?else?{
????????????$el.fadeOut();
????????}
????};
????return?{
????????BackTop:?BackTop
????};
});
2016-01-04
全部源碼