requireJS模块化实现返回顶部功能的方法详解
网络编程 2021-07-04 17:32www.168986.cn编程入门
这篇文章主要介绍了requireJS模块化实现返回顶部功能的方法,结合实例形式详细分析了requireJS的使用步骤及返回顶部功能的相关操作技巧,需要的朋友可以参考下
本文实例讲述了requireJS模块化实现返回顶部功能的方法。分享给大家供大家参考,具体如下:
引用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="" class="btn"></div> </body> </html>
新建main.js
require.config({ paths:{ jquery:'jquery' } }); requirejs(['jquery','back'],function($,back){ $('#').back({ mode:"move", pos:100, dest:500, speed:20000 }) });
创建back模块 back.js
/** * Created by Administrator on 2016/3/24. */ define(["jquery","scrollTo"],function($, scroll){ function back(el,opts){ this.opts = $.extend({},back.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)) }; back.prototype._move = function(){ this.scroll.move() }; back.prototype._go = function(){ this.scroll.go() }; back.prototype._checkPostion = function(){ if($(window).scrollTop() > this.opts.pos){ this.$el.fadeIn(); }else{ this.$el.fadeOut(); } } $.fn.extend({ back:function(opts){ return this.each(function(){ new back(this,opts); }) } }); back.default = { mode:"move", pos:100, dest:0, speed:800 } return{ back:back } })
back 依赖 scrollTo模块
创建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); /
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程