使用原生js写的一个简单slider


Posted in Javascript onApril 29, 2014
<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Document</title> 
<style type="text/css"> 
body,div,ul,li { 
margin: 0; 
padding: 0; 
} 
ul,li { 
list-style: none; 
} 
a img { 
border: none; 
} 
.wrap { 
width: 100%; 
overflow: hidden; 
position: relative; 
} 
.wrap .prev, 
.wrap .next { 
height: 320px; 
position: absolute; 
left: -50%; 
top: 0; 
background-color: #999; 
opacity: 0.7; 
width: 100%; 
} 
.wrap .next { 
left: auto; 
right: -50%; 
} 
.wrap .prev:hover, 
.wrap .next:hover { 
opacity: 0.5; 
} 
.container { 
width: 100%; 
height: 320px; 
} 
.container ul { 
height: 100%; 
} 
.container li { 
width: 1000px; 
height: 100%; 
float: left; 
} 
.container li a, 
.container li img { 
display: block; 
width: 100%; 
height: 100%; 
} 
.page { 
position: absolute; 
left: 50%; 
bottom: 10px; 
display:none; 
} 
.page span { 
float: left; 
margin-right: 10px; 
width: 20px; 
height: 20px; 
display: block; 
cursor: pointer; 
background: #999; 
border-radius: 50%; 
text-align: center; 
color: #fff; 
} 
.page .select { 
background: #f00; 
} 
</style> 
</head> 
<body> 
<div class="wrap"> 
<div class="container" id="container"> 
<ul> 
<li><a href="1"><img src="http://www.hengqijy.com/uploadfile/2013/1031/20131031084228263.jpg"></a> </li> 
<li><a href="2"><img src="http://www.hengqijy.com/uploadfile/2014/0315/20140315023711196.jpg"></a></li> 
<li><a href="3"><img src="http://www.hengqijy.com/uploadfile/2014/0228/20140228091207797.jpg"></a></li> 
<li><a href="4"><img src="http://www.hengqijy.com/statics/images/new_img/ksss.jpg"></a></li> 
</ul> 
</div> 
<div class="prev" id="prev"></div> 
<div class="next" id="next"></div> 
<div id="page" class="page"></div> 
</div> 
<script type="text/javascript"> 
/* 
* Slider constructor 
* @param {Node} ele 容器节点 
* @param {Int} index 默认显示第几张 
*/ 
var Slider = function(ele){ 
this.ele = ele; 
this.oList = ele.children[0]; 
this.items = this.oList.getElementsByTagName("li"); 
this.itemWidth = parseInt(this.items[0].offsetWidth, 10); 
this.page = document.getElementById("page"); 
this.prevBtn = document.getElementById("prev"); 
this.nextBtn = document.getElementById("next"); 
this.init(); 
} 
Slider.prototype = { 
constructor: Slider, 
init: function(){ 
this.oList.style.position = 'absolute'; 
this.oList.style.top = 0; 
this.oList.style.left = 0; this.going = 0; 
this.current = 1; 
this.speed = 100; 
this.timer = null; 
this.wrapWidth = parseInt(this.ele.offsetWidth, 10); 
this.pageCircles = null; 
this.prevBtn.style.marginLeft = -this.itemWidth/2 + 'px'; 
this.nextBtn.style.marginRight = -this.itemWidth/2 + 'px'; 
if(this.items.length > 2) { 
this.setUp(); 
} else { 
this.oList.style.left = (this.wrapWidth - this.itemWidth * this.items.length)/2 + 'px'; 
this.prevBtn.style.display = "none"; 
this.nextBtn.style.display = "none"; 
} 
this.oList.style.width = this.itemWidth * this.items.length + 'px'; 
}, 
setUp:function(){ 
var first1 = this.items[0].cloneNode(); 
first1.innerHTML = this.items[0].innerHTML; 
first2 = this.items[1].cloneNode(); 
first2.innerHTML = this.items[1].innerHTML; 
last1 = this.items[this.items.length-1].cloneNode(); 
last1.innerHTML = this.items[this.items.length-1].innerHTML; 
last2 = this.items[this.items.length-2].cloneNode(); 
last2.innerHTML = this.items[this.items.length-2].innerHTML; 
this.oList.appendChild(first1); 
this.oList.appendChild(first2); 
this.oList.insertBefore(last1, this.items[0]); 
this.oList.insertBefore(last2, this.items[0]); 
this.buildPage(); 
this.bindEvent(); 
this.slientGoTo(); 
}, 
buildPage:function(){ 
for(var i = 0,len = this.items.length - 4; i<len; i++) { 
var circle = document.createElement('span'); 
circle.innerHTML = i + 1; 
this.page.appendChild(circle); 
} 
this.pageCircles = this.page.getElementsByTagName('span'); 
this.addEvent(this.page, 'click', 'gotoIndex'); 
this.page.style.display = 'block'; 
}, 
bindEvent:function(){ 
this.addEvent(this.prevBtn, 'click', 'prev'); 
this.addEvent(this.nextBtn, 'click', 'next'); 
}, 
addEvent:function(ele, type, fn, context) { 
context = context || this; 
var innerFun = function(event) { 
var evt = event || window.event; 
if(typeof fn === 'string'){ 
context[fn].call(context, this, evt); 
} else { 
fn.call(context, this, evt); 
} 
} 
if(window.addEventListener) { 
ele.addEventListener(type, innerFun, false); 
} else { 
ele.attachEvent('on'+type, innerFun); 
} 
}, 
index:function(ele){ 
var parentNode = ele.parentNode; 
var eles = parentNode.getElementsByTagName(ele.tagName); 
for(var i = 0, len = eles.length; i<len; i++){ 
if(eles[i] == ele) { 
return i; 
} 
} 
}, 
hasClass: function(obj, cls) { 
return !!obj.className && obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); 
}, 
addClass: function(obj, cls) { 
if (!this.hasClass(obj, cls)) obj.className += " " + cls; 
}, 
removeClass:function (obj, cls) { 
if (this.hasClass(obj, cls)) { 
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); 
obj.className = obj.className.replace(reg, ' '); 
} 
}, 
gotoIndex:function(eleContext, evt){ 
var ele = evt.target || evt.srcElement; 
var index = this.index(ele); 
if(this.current === this.items.length-4 && index === 0) { 
this.current = 0; 
this.slientGoTo(); 
} else if(this.current === 1 && index ===this.items.length-5) { 
this.current = this.items.length-3; 
this.slientGoTo(); 
} 
this.current = index + 1; 
this.doAnimate(); 
}, 
prev:function(eleContext, evt){ 
this.current--; 
this.doAnimate(); 
}, 
next:function(eleContext, evt){ 
this.current++; 
this.doAnimate(); 
}, 
doAnimate:function(){ 
this.distance = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 - parseInt(this.oList.style.left, 10); 
if(this.distance < 0){ 
this.speed = -Math.abs(this.speed); 
} else { 
this.speed = Math.abs(this.speed); 
} 
this.distance = Math.abs(this.distance); 
this.animate(); 
}, 
animate:function(){ 
var that = this; 
this.timer = setTimeout(function() { 
var left = parseInt(that.oList.style.left, 10) || 0; 
if (that.going+Math.abs(that.speed) >= that.distance) { 
if (that.speed > 0) { 
that.oList.style.left = left + that.distance - that.going + 'px'; 
} else { 
that.oList.style.left = left - that.distance + that.going + 'px'; 
} 
clearTimeout(that.timer); 
that.going = 0; 
that.onceEnd(); 
} else { 
that.going += Math.abs(that.speed); 
that.oList.style.left = left + that.speed + 'px'; 
that.animate(); 
} 
}, 25); 
}, 
slientGoTo:function(){ 
console.log(this.current); 
this.oList.style.left = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 + 'px'; 
}, 
setCircleSelect:function(){ 
for(var i=0,len = this.pageCircles.length; i<len; i++) { 
var ele= this.pageCircles[i]; 
if(this.hasClass(ele, 'select')) { 
this.removeClass(ele, 'select'); 
} 
} 
this.addClass(this.pageCircles[this.current-1], 'select'); 
}, 
correctCurrent:function(){ 
if(this.current === 0) { 
this.current = this.items.length - 4; 
} else if(this.current === this.items.length - 3) { 
this.current = 1; 
} else { 
return false; 
} 
this.slientGoTo(); 
}, 
onceEnd:function(){ 
this.correctCurrent(); 
this.setCircleSelect(); 
} 
} 
var con = document.getElementById("container"); 
var s = new Slider(con); 
</script> 
</body> 
</html>
Javascript 相关文章推荐
JavaScript 撑出页面文字换行
Jun 15 Javascript
javascript 客户端验证上传图片的大小(兼容IE和火狐)
Aug 15 Javascript
关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
Oct 18 Javascript
jQuery事件 delegate()使用方法介绍
Oct 30 Javascript
让你的博客飘雪花超出屏幕依然看得见
Jan 04 Javascript
javascript-表格排序(降序/反序)实现介绍(附图)
May 30 Javascript
jQuery检测某个元素是否存在代码分享
Jul 09 Javascript
js实现人才网站职位选择功能的方法
Aug 14 Javascript
JS中sort函数排序用法实例分析
Jun 16 Javascript
bootstrap vue.js实现tab效果
Feb 07 Javascript
JS条形码(一维码)插件JsBarcode用法详解【编码类型、参数、属性】
Apr 19 Javascript
Vue单页面应用保证F5强刷不清空数据的解决方案
Jan 31 Javascript
js判断横竖屏及禁止浏览器滑动条示例
Apr 29 #Javascript
使用jquery写个更改表格行顺序的小功能
Apr 29 #Javascript
百度移动版的url编码解码示例
Apr 29 #Javascript
通过url查找a元素应用案例
Apr 29 #Javascript
jquery实现的图片点击滚动效果
Apr 29 #Javascript
Extjs的FileUploadField文件上传出现了两个上传按钮
Apr 29 #Javascript
jquery xMarquee实现文字水平无缝滚动效果
Apr 29 #Javascript
You might like
PHP+jQuery+Ajax实现用户登录与退出
2015/04/27 PHP
PHP微信支付实例解析
2016/07/22 PHP
为指定元素增加样式的js代码
2009/12/09 Javascript
控制页面按钮在后台执行期间不重复提交的JS方法
2013/06/24 Javascript
对于Form表单reset方法的新认识
2014/03/05 Javascript
jQuery操作表格(table)的常用方法、技巧汇总
2014/04/12 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
JS实现适合于后台使用的动画折叠菜单效果
2015/09/21 Javascript
基于JavaScript如何实现私有成员的语法特征及私有成员的实现方式
2015/10/28 Javascript
学习JavaScript设计模式之中介者模式
2016/01/14 Javascript
JS遍历数组及打印数组实例分析
2016/01/21 Javascript
jQuery简单实现提交数据出现loading进度条的方法
2016/03/29 Javascript
JS从非数组对象转数组的方法小结
2018/03/26 Javascript
Vue全局分页组件的实现代码
2018/08/10 Javascript
详解小程序之简单登录注册表单验证
2019/05/13 Javascript
ES6 Proxy实现Vue的变化检测问题
2019/06/11 Javascript
vue+koa2搭建mock数据环境的详细教程
2020/05/18 Javascript
vue实现禁止浏览器记住密码功能的示例代码
2021/02/03 Vue.js
JSONLINT:python的json数据验证库实例解析
2017/11/28 Python
pandas值替换方法
2018/07/10 Python
pygame游戏之旅 添加游戏界面按键图形
2018/11/20 Python
基于Tensorflow读取MNIST数据集时网络超时的解决方式
2020/06/22 Python
通过实例解析Python RPC实现原理及方法
2020/07/07 Python
Pycharm配置autopep8实现流程解析
2020/11/28 Python
Python try except else使用详解
2021/01/12 Python
python 如何用urllib与服务端交互(发送和接收数据)
2021/03/04 Python
Belstaff英国官方在线商店:Belstaff.co.uk
2021/02/09 全球购物
惠而浦美国官网:Whirlpool.com
2021/01/19 全球购物
应届毕业生就业自荐信
2013/10/26 职场文书
售后服务承诺书怎么写
2014/05/21 职场文书
小学班级口号
2014/06/09 职场文书
软件研发工程师岗位职责
2014/09/30 职场文书
介绍信的写法
2015/01/31 职场文书
爱心捐款倡议书:点燃希望,传递温暖
2019/11/04 职场文书
如何搭建 MySQL 高可用高性能集群
2021/06/21 MySQL
为什么MySQL 删除表数据 磁盘空间还一直被占用
2021/10/16 MySQL