js编写无缝轮播图效果
2017-04-22 21:38
453 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;}
.banner{width:1000px;height:350px;margin:50px auto;}
#list img{width:100%;height:100%;}
#list li {list-style: none;width:1000px;height:350px;float: left;}
li .active{z-index: 1;}
#span{width:400px;height:40px;margin:0 auto;}
#span span{width:40px;height:40px;background:#f99;float:left;display: block;
border-radius: 50%;margin-left:30px;}
#span .active{background:red;}
</style>
</head>
<body>
<button id="left">上一张</button>
<button id="right">下一张</button>
<div class="banner">
<ul id="list">
<li class="active"><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/7a3e58f6c39f00000148f06a7b92.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/995558f62a0900000148f0a9ac3f.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
</ul>
</div>
<div id="span">
<span class="active"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</body>
<script src="js/move.js"></script>
<script>
function $(select){
return document.querySelector(select);
}
var oList = $("#list");
var aLi = oList.children ;
var oSpan = $("#span").children;
var oLeft = $("#left");
var oRight = $("#right");
var index = 0 ;
oList.style.width = aLi[0].offsetWidth * aLi.length + "px" ;
oRight.onclick = function(){ //点击右键切换下一张图
if(index==aLi.length-1){
index = 1 ;
oList.style.marginLeft = 0 ;
}else{
index++;
}
movep();
}
oLeft.onclick = function(){ //点击左键切换上一张图
if(index==0){
index = aLi.length-2 ;
oList.style.marginLeft = -(aLi.length-1)*aLi[0].offsetWidth +"px";
}else{
index--;
}
movep();
}
function movep(){ //切图控制函数
move(oList,{marginLeft:-index*aLi[0].offsetWidth});
for(var i = 0 ; i < oSpan.length ; i++){
oSpan[i].className = "" ;
}
if(index == aLi.length-1){
oSpan[0].className = "active" ;
}else{
oSpan[index].className = "active" ;
}
}
for(var i = 0 ; i < oSpan.length ; i++){
oSpan[i].onclick = (function(n){
return function(){
index = n ;
movep();
}
})(i)
}
var timer = null ;
timer = setInterval(oLeft.onclick,2000);
for(var j = 0 ; j < aLi.length ; j++){ //循环内控制鼠标划过按钮体质自动按轮播
oLeft.onmouseover = oRight.onmouseover = oSpan[j].onmouseover = function(){
clearInterval(timer);
}
oLeft.onmouseout = oRight.onmouseout = oSpan[j].onmouseout = function(){
timer = setInterval(oLeft.onclick,2000);
}
}
</script>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;}
.banner{width:1000px;height:350px;margin:50px auto;}
#list img{width:100%;height:100%;}
#list li {list-style: none;width:1000px;height:350px;float: left;}
li .active{z-index: 1;}
#span{width:400px;height:40px;margin:0 auto;}
#span span{width:40px;height:40px;background:#f99;float:left;display: block;
border-radius: 50%;margin-left:30px;}
#span .active{background:red;}
</style>
</head>
<body>
<button id="left">上一张</button>
<button id="right">下一张</button>
<div class="banner">
<ul id="list">
<li class="active"><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/7a3e58f6c39f00000148f06a7b92.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/995558f62a0900000148f0a9ac3f.jpg" alt=""></li>
<li><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
</ul>
</div>
<div id="span">
<span class="active"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</body>
<script src="js/move.js"></script>
<script>
function $(select){
return document.querySelector(select);
}
var oList = $("#list");
var aLi = oList.children ;
var oSpan = $("#span").children;
var oLeft = $("#left");
var oRight = $("#right");
var index = 0 ;
oList.style.width = aLi[0].offsetWidth * aLi.length + "px" ;
oRight.onclick = function(){ //点击右键切换下一张图
if(index==aLi.length-1){
index = 1 ;
oList.style.marginLeft = 0 ;
}else{
index++;
}
movep();
}
oLeft.onclick = function(){ //点击左键切换上一张图
if(index==0){
index = aLi.length-2 ;
oList.style.marginLeft = -(aLi.length-1)*aLi[0].offsetWidth +"px";
}else{
index--;
}
movep();
}
function movep(){ //切图控制函数
move(oList,{marginLeft:-index*aLi[0].offsetWidth});
for(var i = 0 ; i < oSpan.length ; i++){
oSpan[i].className = "" ;
}
if(index == aLi.length-1){
oSpan[0].className = "active" ;
}else{
oSpan[index].className = "active" ;
}
}
for(var i = 0 ; i < oSpan.length ; i++){
oSpan[i].onclick = (function(n){
return function(){
index = n ;
movep();
}
})(i)
}
var timer = null ;
timer = setInterval(oLeft.onclick,2000);
for(var j = 0 ; j < aLi.length ; j++){ //循环内控制鼠标划过按钮体质自动按轮播
oLeft.onmouseover = oRight.onmouseover = oSpan[j].onmouseover = function(){
clearInterval(timer);
}
oLeft.onmouseout = oRight.onmouseout = oSpan[j].onmouseout = function(){
timer = setInterval(oLeft.onclick,2000);
}
}
</script>
</html>
相关文章推荐
- js轮播图无缝滚动效果
- js实现图片无缝轮播效果
- 原生js实现无缝轮播图效果
- (原生JS) 图片无缝滚动、图片转化、轮播图、折叠效果
- JS编写兼容IE6,7,8浏览器无缝自动轮播
- 原生JS实现简单的无缝自动轮播效果
- js 无缝滚动效果学习
- 无缝滚动效果 js可以实现
- 在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
- JS图片滚动(无缝、平滑、上下左右滚动)效果
- 扶植版做出拓展版950通栏轮播效果无缝连接
- JS轮播图效果实现
- js基础练习---图片无缝左右滚动效果(主要以复制删除为主)
- js实现淘宝首页图片轮播效果(修正图片滚动顺序的问题)
- js向上无缝滚动,网站公告效果 具体代码
- js向上无缝滚动,网站公告效果 具体代码
- JS--图片轮播效果
- 在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
- js实现图片轮播效果
- marquee+js实现某个区域的“无缝滚动效果”