js中的轮播图案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
800px;
height: 300px;
position: relative;
overflow: hidden;/*隐藏子元素*/
margin: 100px auto;
}
.box .slider{
100%;
height: 100%;
}
.box .slider ul{
1000%;
list-style: none;
position: absolute;
top: 0;
left: 0;
}
.box .slider img{
vertical-align: top;
}
.box .slider ul li{
float: left;
}
.box .scroll_nav{
list-style: none;
position: absolute;
right: 20px;
bottom: 10px;
}
.box .scroll_nav li{
float: left;
20px;
height: 20px;
background-color: #000;
color: #fff;
text-align: center;
font-size: 12px;
font-family: "微软雅黑";
line-height: 20px;
cursor: pointer;
border-radius: 50%;
margin-right: 5px;
}
.box .scroll_nav li.current{
background-color: #356acb;
}
.box .arr{
40px;
height: 50px;
font-size: 35px;
font-family: "黑体";
line-height: 50px;
background-color: rgba(0,0,0,0.3); /*这种方式能让背景透明而文本不收影响*/
color: #fff;
text-align: center;
position: absolute;
top: 125px;
font-weight: bold;
display: none;
cursor: pointer;
/* opacity: 0.1; opacity属性会让盒子半透明,与此同时它也会让黑子内部的文本半透明*/
}
.box .arr_prev{
left: 5px;
}
.box .arr_next{
right: 5px;
}
</style>
<script type="text/javascript">
//
function $(id){
return document.getElementById(id);
}
window.onload=function(){
//1.鼠标进入轮播图区域显示左右箭头
$('scroll').onmouseover=function(){
var arrs = this.getElementsByTagName('span');
for (var i = 0; i < arrs.length; i++) {
arrs[i].style.display = 'block';
}
}
$('scroll').onmouseout=function(){
var arrs = this.getElementsByTagName('span');
for (var i = 0; i < arrs.length; i++) {
arrs[i].style.display = 'none';
}
}
//2.动态生成轮播图导航栏里的Li
var imgCount = $('sl_ul').getElementsByTagName('img').length;//获取图的个数
for (var i = 0; i < imgCount; i++) {
var li=document.createElement('li');//动态创建一个Li标签
li.innerHTML=i+1;
if(i==0){
li.className='current';
}
//给每个创建出来的li动态添加索引值,为下面的序号定值,再点击序号按钮时以便找到下标换图
li.setAttribute("index", i);
//2.2注册li的点击事件:轮播图滚动
li.onclick=function(){
//排他思想,搞定li的样式
var lis=$('sc_nav').getElementsByTagName('li');
for (var i = 0; i < lis.length; i++) {
lis[i].className="";
}
this.className="current";
//设置动画的target让轮播图滚动起来
target=this.getAttribute('index')* -800;
}
//
$('sc_nav').appendChild(li);
}
//3.启动缓动动画定时器在window.onload理
var leader =0;
var target =0;
setInterval(function(){
leader=leader+(target-leader)/20;
$('sl_ul').style.left=leader+"px";
},10);
//4.右箭头点击事件:next
$('next').onclick=function(){
if(target>-800*4){
target-=800;
}else{
target=0;//如果已经是最后一张图,就直接滚动回第一张图
}
setNavCurrent();
}
//.左箭头点击事件:prev
$('prev').onclick=function(){
if(target<0){
target+=800;
}
setNavCurrent();
}
//让nav中的li跟随箭头的点击改变选中状态
function setNavCurrent(){
var index = Math.abs(target/800);//拿到索引
var lis=$('sc_nav').getElementsByTagName('li');
for (var i = 0; i < lis.length; i++) {
lis[i].className="";
}
lis[index].className='current';
}
}
</script>
</head>
<body>
<div class="box" id="scroll">
<div class="slider" id="sl">
<ul id="sl_ul">
<li><img src="imgs/1.jpg" alt=""></li>
<li><img src="imgs/2.jpg" alt=""></li>
<li><img src="imgs/3.jpg" alt=""></li>
<li><img src="imgs/4.jpg" alt=""></li>
<li><img src="imgs/5.jpg" alt=""></li>
</ul>
</div>
<ul class="scroll_nav" id="sc_nav">
<!-- <li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li> -->
</ul>
<span class="arr arr_prev" id="prev"><</span>
<span class="arr arr_next" id="next">></span>
</div>
</body>
</html>
bootstrap中的轮播图不需要写js脚本,我们可以使用bootstrap中的定义好的属性进行编写
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<script type="application/javascript" src="js/jquery-1.11.1.min.js"></script>
<script language="JavaScript" src="js/bootstrap.min.js"></script>
<script language="JavaScript">
</script>
<style type="text/css">
#turn img{
800px;
height: 400px;
}
</style>
</head>
<body>
<!--
焦点轮播图:data-ride="carousel"页面加载完毕后立马开始轮播
-->
<div class="container">
<div class="row">
<div class="col-md-9">
<div data-ride="carousel" id="carousel_container" class="carousel slide">
<!--图片容器-->
<!--carousel-inner:让图片水平排列-->
<div class="carousel-inner">
<div class="item" id="turn">
<img src="img/pic01.jpg" class="text-center" />
<h4 class="carousel-caption">hello</h4>
</div>
<div class="item active"><img src="img/pic02.jpg" /></div>
<div class="item"><img src="img/pic03.jpg" /></div>
</div>
<!--小圆圈容器-->
<ol class="carousel-indicators">
<li data-slide-to="0" data-target="#carousel_container"></li>
<li data-slide-to="1" data-target="#carousel_container"></li>
<li data-slide-to="2" data-target="#carousel_container"></li>
</ol>
<!--左右按钮-->
<a href="#carousel_container" data-slide="prev" class="left carousel-control">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a href="#carousel_container" data-slide="next" class="right carousel-control">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
<div class="col-md-3"></div>
</div>
</div>
</body>
</html>