requireJS模塊化實現(xiàn)返回頂部功能的方法詳解
更新時間:2017年10月16日 12:05:59 作者:抽象思維
這篇文章主要介紹了requireJS模塊化實現(xiàn)返回頂部功能的方法,結(jié)合實例形式詳細(xì)分析了requireJS的使用步驟及返回頂部功能的相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了requireJS模塊化實現(xiàn)返回頂部功能的方法。分享給大家供大家參考,具體如下:
引用requireJs
<script src="require.js" data-main="main"></script>
html部分
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{padding: 0; margin: 0; height: 3000px}
.btn{width: 80px; height: 80px;
position: fixed; bottom: 0; left: 50%; background: #ddd}
</style>
<script src="require.js" data-main="main"></script>
</head>
<body>
<div id="top" class="btn"></div>
</body>
</html>
新建main.js
require.config({
paths:{
jquery:'jquery'
}
});
requirejs(['jquery','backtop'],function($,backtop){
$('#top').backtop({
mode:"move",
pos:100,
dest:500,
speed:20000
})
});
創(chuàng)建backtop模塊 backtop.js
/**
* Created by Administrator on 2016/3/24.
*/
define(["jquery","scrollTo"],function($, scroll){
function backtop(el,opts){
this.opts = $.extend({},backtop.default,opts);
this.$el = $(el);
this.scroll = new scroll.scrollTo({
dest:this.opts.dest,
speed:this.opts.speed
});
this._checkPostion();
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._checkPostion,this))
};
backtop.prototype._move = function(){
this.scroll.move()
};
backtop.prototype._go = function(){
this.scroll.go()
};
backtop.prototype._checkPostion = function(){
if($(window).scrollTop() > this.opts.pos){
this.$el.fadeIn();
}else{
this.$el.fadeOut();
}
}
$.fn.extend({
backtop:function(opts){
return this.each(function(){
new backtop(this,opts);
})
}
});
backtop.default = {
mode:"move",
pos:100,
dest:0,
speed:800
}
return{
backtop:backtop
}
})
backtop 依賴 scrollTo模塊
創(chuàng)建scrollTo.js
define(['jquery'],function($){
function scrollTo(opts){
this.opts = $.extend({},scrollTo.DEFAULTS,opts);
this.$el = $("html,body");
}
scrollTo.prototype.move = function(){
if($(window).scrollTop() != this.opts.dest){
//if(!this.$el.is(":animated")){
this.$el.animate({scrollTop:this.opts.dest},this.opts.speed);
//}
}
};
scrollTo.prototype.go = function(){
this.$el.scrollTop(this.opts.dest)
};
scrollTo.DEFAULTS = {
dest:0,
speed:800
};
return {
scrollTo:scrollTo
}
});
希望本文所述對大家基于requireJS的程序設(shè)計有所幫助。
您可能感興趣的文章:
- 在Html中使用Requirejs進(jìn)行模塊化開發(fā)實例詳解
- 基于RequireJS和JQuery的模塊化編程日常問題解析
- 使用requirejs模塊化開發(fā)多頁面一個入口js的使用方式
- 基于RequireJS和JQuery的模塊化編程——常見問題全面解析
- JavaScript模塊化之使用requireJS按需加載
- 一篇文章掌握RequireJS常用知識
- SeaJS 與 RequireJS 的差異對比
- RequireJS多頁面應(yīng)用實例分析
- 在JavaScript應(yīng)用中使用RequireJS來實現(xiàn)延遲加載
- angularJS+requireJS實現(xiàn)controller及directive的按需加載示例
- 一個極為簡單的requirejs實現(xiàn)方法
相關(guān)文章
JavaScript實現(xiàn)倒計時跳轉(zhuǎn)頁面功能【實用】
本文分享了JavaScript實現(xiàn)倒計時跳轉(zhuǎn)頁面功能的具體實例代碼,頁面代碼簡單,直接拷貝就能運(yùn)行,頁面可以自己美化下哦。需要的朋友一起來看下吧2016-12-12
JavaScript中Object基礎(chǔ)內(nèi)部方法圖
本篇文章通過一張詳細(xì)的JavaScript中Object基礎(chǔ)內(nèi)部方法圖介紹了其基本用法,需要的朋友參考下。2018-02-02
使用js判斷TextBox控件值改變?nèi)缓蟪霭l(fā)事件
這篇文章主要介紹了使用js判斷TextBox控件值改變?nèi)缓蟪霭l(fā)事件。需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03
javascript過濾數(shù)組重復(fù)元素的實現(xiàn)方法
這篇文章主要介紹了javascript過濾數(shù)組重復(fù)元素的實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-05-05

