requireJS模塊化實(shí)現(xiàn)返回頂部功能的方法詳解
本文實(shí)例講述了requireJS模塊化實(shí)現(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
}
});
希望本文所述對(duì)大家基于requireJS的程序設(shè)計(jì)有所幫助。
- 在Html中使用Requirejs進(jìn)行模塊化開(kāi)發(fā)實(shí)例詳解
- 基于RequireJS和JQuery的模塊化編程日常問(wèn)題解析
- 使用requirejs模塊化開(kāi)發(fā)多頁(yè)面一個(gè)入口js的使用方式
- 基于RequireJS和JQuery的模塊化編程——常見(jiàn)問(wèn)題全面解析
- JavaScript模塊化之使用requireJS按需加載
- 一篇文章掌握RequireJS常用知識(shí)
- SeaJS 與 RequireJS 的差異對(duì)比
- RequireJS多頁(yè)面應(yīng)用實(shí)例分析
- 在JavaScript應(yīng)用中使用RequireJS來(lái)實(shí)現(xiàn)延遲加載
- angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例
- 一個(gè)極為簡(jiǎn)單的requirejs實(shí)現(xiàn)方法
相關(guān)文章
javascript實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
淺談JS中String()與 .toString()的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談JS中String()與 .toString()的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
JavaScript實(shí)現(xiàn)消消樂(lè)的源代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)消消樂(lè)-源代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
three.js著色器材質(zhì)的內(nèi)置變量示例詳解
這篇文章主要給大家介紹了關(guān)于three.js著色器材質(zhì)內(nèi)置變量的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用three.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
json屬性名為什么要雙引號(hào)(個(gè)人猜測(cè))
json屬性名為什么要雙引號(hào)?更加規(guī)范,利于解析、避免class等關(guān)鍵字引起的不兼容問(wèn)題,需要的朋友可以參考下2014-07-07
中國(guó)地區(qū)三級(jí)聯(lián)動(dòng)下拉菜單效果分析
主要的數(shù)據(jù)和功能實(shí)現(xiàn)都是在js文件中,網(wǎng)上找的地區(qū)數(shù)據(jù)有的地方不完整,需要自己添加,本文將詳細(xì)介紹2012-11-11

