2.轮播图

 

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery-3.1.1.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
text-decoration: none;/*文本修饰无*/
}

body {
padding: 20px;/*内边距*/
}

#container {
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;/*隐藏*/
position: relative;/*相对定位*/
}

#list {
width: 4200px;
height: 400px;
position: absolute;/*绝对定位*/
/*z-index : auto | number  表示一个元素在叠加顺序上的上下立体关系。 对于未指定此属性的定位对象,
z-index 值为正数的对象会在其之上,而 z-index 值为负数的对象在其之下。*/
z-index: 1;
}

#list img {
width: 600px;
height: 400px;
float: left;
}

#buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;/*底部*/
left: 250px;
}

#buttons span {/*小圆按钮*/
cursor: pointer;/*光标指针*/
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;/*border的半径*/
background: #333;
margin-right: 5px;
}

#buttons .on {
background: orangered;
}

.arrow {/*左右箭头按钮*/
cursor: pointer;
display: none;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
/*RGBA:1.红 2.绿 3.蓝  可以为正整数0-255或百分比    4.透明度   取值0-1之间 越大越不透明*/
background-color: RGBA(0, 0, 0, 0.3);/*RGBA 不透明参数*/
color: #fff;
}

.arrow:hover {
background-color: RGBA(0, 0, 0, 0.7);
}

#container:hover .arrow {
display: block;
}

#prev {/*上一张*/
left: 20px;
}

#next {/*下一张*/
right: 20px;
}
</style>
</head>


<body>
<div id="container">
<!--无缝滚动,复制最后一张图片放置第一张图片前,同时复制第一张图片
放置最后一张图片的后面。并且,将第一张图片辅助图(实际上是实际显示的第5张
图片隐藏起来,故设置style="left: -600px-->
<div id="list" style="left: -600px;"><!--图片宽度600px-->
<img src="img/5.jpg" alt="1" /> 
<img src="img/1.jpg" alt="1" /> 
<img src="img/2.jpg" alt="2" /> 
<img src="img/3.jpg" alt="3" /> 
<img src="img/4.jpg" alt="4" /> 
<img src="img/5.jpg" alt="5" /> 
<img src="img/1.jpg" alt="5" /> 
</div>
<div id="buttons"> 
<span index="1" class="on"></span> 
<span index="2"></span> 
<span index="3"></span> 
<span index="4"></span> 
<span index="5"></span> 
</div>
<a href="javascript:;" id="prev" class="arrow"></a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>


<script type="text/javascript">
/* 知识点: */ 
/* this用法 */ 
/* DOM事件 */ 
/* 定时器 */
//加载顺序
window.onload = function() {
var container = document.getElementById('container');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
var timer;
//动画
function animate(offset) { 
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()将字符串取整转化为数字。
var newLeft = parseInt(list.style.left) + offset;//offset偏移
list.style.left = newLeft + 'px';
//无限滚动判断  一张图片宽度600px
if(newLeft > -600) {
list.style.left = -3000 + 'px';
}//-600px*5=3000
if(newLeft < -3000) {
list.style.left = -600 + 'px';
}
}


function play() {
//重复执行的定时器 
timer = setInterval(function() {
next.onclick();
}, 2000)
}


function stop() {
clearInterval(timer);
}


function buttonsShow() {
//将之前的小圆点的样式清除 
for(var i = 0; i < buttons.length; i++) {
if(buttons[i].className == "on") {
buttons[i].className = "";
}
}
//数组从0开始,故index需要-1 
buttons[index - 1].className = "on";
}
prev.onclick = function() {
index -= 1;
if(index < 1) {
index = 5
}
buttonsShow();
animate(600);
};
next.onclick = function() {
//由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,
//所以需要做出判断
index += 1;
if(index > 5) {
index = 1
}
animate(-600);//动画
buttonsShow();
};
for(var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function() {
//优化,当前图片点击当前的小圆点不执行以下代码。
if(this.className == "on") {
return;
}
/* 这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,
* 去谷歌this的用法 */
/* 由于这里的index是自定义属性,需要用到getAttribute()
* 这个DOM2级方法,去获取自定义index的属性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (clickIndex - index);
//这个index是当前图片停留时的index animate(offset); index = clickIndex;
//存放鼠标点击后的位置,用于小圆点的正常显示 
buttonsShow();
}
}
container.onmouseover = stop;
container.onmouseout = play;
play();
};
</script>


</body>


</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值