使用Require.js封装原生js轮播图的实现代码
程序员文章站
2022-07-05 20:35:28
index.html页面:
index.html页面:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>require.js封装轮播图</title> <style type="text/css"> *{ margin: 0; padding: 0; list-style: none; } #banner{ width: 830px; height: 440px; border: solid 1px; margin: 50px auto; position: relative; overflow: hidden; } #banner ul{ position: absolute; left: 0; top: 0; } #banner ul li{ width: 830px; height: 440px; float: left; } #banner p{ position: absolute; left: 50%; bottom: 10px; margin-left: -30px; } #banner p span{ display: block; float: left; width: 15px; height: 15px; margin-right: 6px; background: #ccc; border-radius: 50%; } #banner p .on{ background: red; } </style> <script type="text/javascript" src="require.js" data-main='init'></script> <!--<script type="text/javascript"> require(['slider'],function(mod){ mod.slide() }) </script>--> </head> <body> <div id="banner"> <ul> <li><img src="images/1.jpg"/></li> <li><img src="images/2.jpg"/></li> </ul> <p> <span class="on"></span> <span></span> <span></span> </p> </div> </body> </html>
get.js代码如下:
define(function(require,exports,module){ exports.getstyle = function (obj,name){ if(obj.currentstyle){ return obj.currentstyle[name]; }else{ return getcomputedstyle(obj,false)[name]; }; }; });
init.js代码如下
require(['slider'],function(mod){ mod.slide(); });
slider.js代码如下
define(function(require, exports, module) { var move = require('startmove'); var abtn = document.getelementbyid('banner').getelementsbytagname('span'); var oul = document.getelementbyid('banner').getelementsbytagname('ul')[0]; var ali = oul.children; //三张图片所占的宽度,length返回的是字符串中的字符数 oul.style.width = ali.length * ali[0].offsetwidth + 'px'; exports.slide = function() { for(var i = 0; i < abtn.length; i++) { abtn[i].index = i; abtn[i].onclick = function() { for(var i = 0; i < abtn.length; i++) { abtn[i].classname = ''; } abtn[this.index].classname = 'on'; move.move(oul, { left: -this.index * ali[0].offsetwidth; }); }; }; }; });
startmove.js代码如下
define(function(require, exports, module) { var get = require('get'); exports.move = function move(obj, json, complete) { clearinterval(obj.timer); var complete = complete || {}; complete.dur = complete.dur || 1000; complete.easing = complete.easing || 'ease-out'; var count = parseint(complete.dur / 30); //总次数 var start = {}; //{width:300,height:300} var dis = {}; //{width:300,height:300} for(var name in json) { start[name] = parsefloat(get.getstyle(obj, name)); dis[name] = json[name] - start[name]; } var n = 0; //当前步数 obj.timer = setinterval(function() { n++; for(var name in json) { var a = n / count; switch(complete.easing) { case 'linear': var cur = start[name] + a * dis[name]; break; case 'ease-in': var cur = start[name] + math.pow(a, 3) * dis[name]; break; case 'ease-out': var a = 1 - n / count; var cur = start[name] + (1 - math.pow(a, 3)) * dis[name]; break; }; if(name == 'opacity') { obj.style[name] = cur; obj.style.filter = 'alpha(' + cur * 100 + ')'; } else { obj.style[name] = cur + 'px'; }; }; if(n == count) { clearinterval(obj.timer) complete.fn && complete.fn(); }; }, 30); }; });
以上所述是小编给大家介绍的使用require.js封装原生js轮播图,希望对大家有所帮助