使用原生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 相关文章推荐
JQuery Tab选项卡效果代码改进版
Apr 01 Javascript
jQuery获取样式中的背景颜色属性值/颜色值
Dec 17 Javascript
JavaScript:Div层拖动效果实例代码
Aug 06 Javascript
js判断字符长度以及中英文数字等
Dec 31 Javascript
js数组与字符串的相互转换方法
Jul 09 Javascript
jQuery使用Selectator插件实现多选下拉列表过滤框(附源码下载)
Apr 08 Javascript
Bootstrap页面布局基础知识全面解析
Jun 13 Javascript
Bootstrap编写一个同时适用于PC、平板、手机的登陆页面
Jun 30 Javascript
JS中Select下拉列表类(支持输入模糊查询)功能
Jan 17 Javascript
详解通过JSON数据使用VUE.JS
May 26 Javascript
原生js实现Flappy Bird小游戏
Dec 24 Javascript
layui加载表格,绑定新增,编辑删除,查看按钮事件的例子
Sep 06 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也能干大事之PHP中的编码解码详解
2015/04/20 PHP
Yii2.0高级框架数据库增删改查的一些操作
2015/11/16 PHP
PHP 返回数组后处理方法(开户成功后弹窗提示)
2017/07/03 PHP
phpStudy2016 配置多个域名期间遇到的问题小结
2017/10/19 PHP
Laravle eloquent 多对多模型关联实例详解
2017/11/22 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
2020/04/06 PHP
学习js所必须要知道的一些
2007/03/07 Javascript
cookie丢失问题(认证失效) Authentication (用户验证信息)也会丢失
2009/06/04 Javascript
利用函数的惰性载入提高javascript代码执行效率
2014/05/05 Javascript
jquery 按键盘上的enter事件
2014/05/11 Javascript
jquery幻灯片插件bxslider样式改进实例
2014/10/15 Javascript
JavaScript省市区三级联动菜单效果
2016/09/21 Javascript
关于验证码在IE中不刷新的快速解决方法
2016/09/23 Javascript
JavaScript如何实现图片懒加载(lazyload) 提高用户体验(增强版)
2016/11/30 Javascript
JS基于面向对象实现的拖拽功能示例
2016/12/20 Javascript
微信小程序图片选择、上传到服务器、预览(PHP)实现实例
2017/05/11 Javascript
js实现城市级联菜单的2种方法
2017/06/23 Javascript
angular.js中解决跨域问题的三种方式
2017/07/12 Javascript
vue监听input标签的value值方法
2018/08/27 Javascript
详解CommonJS和ES6模块循环加载处理的区别
2018/12/26 Javascript
python中实现数组和列表读取一列的方法
2018/04/03 Python
python pandas 组内排序、单组排序、标号的实例
2018/04/12 Python
Python数据类型之Tuple元组实例详解
2019/05/08 Python
python用match()函数爬数据方法详解
2019/07/23 Python
简单了解Django ORM常用字段类型及参数配置
2020/01/07 Python
Python Scrapy多页数据爬取实现过程解析
2020/06/12 Python
python3环境搭建过程(利用Anaconda+pycharm)完整版
2020/08/19 Python
css3实现多个元素依次显示效果
2017/12/12 HTML / CSS
荷兰皇家航空公司官方网站:KLM Royal Dutch Airlines
2017/12/07 全球购物
ReVive利维肤美国官网:RéVive Skincare
2018/04/18 全球购物
6PM官网:折扣鞋、服装及配饰
2018/08/03 全球购物
哪些情况下不应该使用索引
2015/07/20 面试题
C#的几个面试问题
2016/05/22 面试题
药学专业个人自我评价
2013/11/11 职场文书
英语自我评价范文
2014/01/24 职场文书
2014党员四风对照检查材料思想汇报
2014/09/17 职场文书