今天给大家带来的是我自己做的一个轮播图效果,让我们一起来学习一下吧。
这是我的页面所有代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>轮播图</title>
<style type="text/css">
*{padding: 0;margin: 0;}
#ban_box{width: 1000px;height: 400px;margin: 5% auto;position: relative;overflow: hidden;}
#banner{position: absolute;z-index: 1;width:4000px;height: 400px;}
#banner img{width: 1000px;height: 400px;float: left;}
#but {position: absolute;left: 450px;bottom: 20px;z-index: 99;height: 20px;width: 110px;}
#but span {float: left;margin-right:25px;width: 20px;height: 20px;border-radius: 50%;background: #ddd;cursor: pointer;}
#but span:last-child{margin: 0;}
#but .on{background: #C9251D;}
.arrow {position: absolute;top: 180px;z-index: 2;text-decoration: none;font-size: 50px;font-weight: bold;color: #000;cursor: pointer;display: none;}
#prev{left: 20px;}
#next{right: 20px;}
#ban_box:hover .arrow{display: block;}
</style>
</head>
<body>
<div id="ban_box">
<div id="banner" style="left:-1000px;">
<img src="img/7.jpg"/>
<img src="img/11.jpg"/>
<img src="img/383708577581202919.jpg"/>
<img src="img/7.jpg"/>
</div>
<div id="but">
<span value="1" class="on"></span>
<span value="2"></span>
<span value="3"></span>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
<script type="text/javascript" src="js/fz.js" ></script>
<script type="text/javascript">
var box = $("ban_box");
var banner = $("banner");
var prev = $("prev");
var next = $("next");
var but = $('but').getElementsByTagName('span');
var timer;
var value=1;
function animation(lenghts){
var left = parseInt(banner.style.left) + lenghts;
banner.style.left = left + 'px';
banner.style.transition = ".5s";
//无限滚动判断
if (left > -1000) {
banner.style.left = -3000 + 'px';
}
if (left < -3000) {
banner.style.left = -1000 + 'px';
}
}
function butShow() {
//将之前的小圆点的样式清除
for (var i = 0; i < but.length; i++) {
if (but[i].className == "on") {
but[i].className = "";
}
}
//数组从0开始,故index需要-1
but[value - 1].className = "on";
}
//上一页
prev.onclick = function() {
value -= 1;
if (value < 1) {
value = 3;
}
butShow();
animation(1000);
}
//下一页
next.onclick = function() {
value += 1;
if (value > 3){
value = 1;
}
butShow();
animation(-1000);
}
for (var i = 0; i < but.length; i++) {
but[i].onclick = function(){
console.log(i);
var click = parseInt(this.getAttribute('value'));
var lenghts = 1000 * (value - click);
animation(lenghts);
value = click;
butShow();
}
}
//定时器
function play() {
timer = setInterval(function () {
next.onclick();
}, 1000)
}
play();
//鼠标悬浮停止
function stop() {
clearInterval(timer);
}
box.onmouseover = stop; //鼠标移到目标上让它停止
box.onmouseout = play; //鼠标移开时让它播放
</script>
</body>
</html>
这其中有我自己封装的一些东西,需要引进去,或者把其中需要找的ID用dom找出来就可以了哦
这是我的效果图,大家可以找一些漂亮的图片哦,效果一定会很漂亮哦:
大家如果有什么问题可以在下面问,或者我的代码有什么bug也可以帮我提出,有更好的办法也可以分享给我哦。