前端写原生轮播图

html页面如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./carousel.css">
    <script src="./carousel.js"></script>
    <title>轮播</title>
</head>
<body>
    <div id="box">
        <img  id="pic" src="./img/1.jpg" alt="">
        <ul id="list">
            <li>1</li>
            <li>2</li>
        </ul>
        <div class="btn" id="left">&lt;</div>
        <div class="btn" id="right">&gt;</div>
    </div>
</body>
</html>

CSS文件


*{
	margin: 0;
	padding: 0;
}
#box{
	width: 790px;
	height: 340px;
	margin: 0 auto;
	position: relative;
}
.btn{
	width: 50px;
	height: 100px;
	color: #fff;
	background-color: rgba(0,0,0,0.2);
	font-size: 40px;
	text-align: center;
	line-height: 100px;
	position: absolute;
	top: 120px;
	display: none;
}
 
#left{
	left: 0px;
}
#right{
	right: 0px;
}
ul {
	list-style: none;
	position: absolute;
	bottom: 20px;
	left: 230px;
}
ul li{
	float: left;
	width: 20px;
	height: 20px;
	margin-left: 10px;
	background-color: #aaa;
	text-align: center;
	line-height: 20px;
	border-radius: 50%;
}

JS文件

window.onload = function() {
	var box = document.getElementById("box");
	var img = document.getElementById("pic");
	var left_btn = document.getElementById("left");
	var right_btn = document.getElementById("right");
	var allLi = document.getElementsByTagName("li");
	//第一步 :第一个按钮设置为红色
	var currentNUM = 1;
	allLi[0].style.backgroundColor = "red";
    var timer = setInterval(startloop,2000);
    function startloop(){
        currentNUM++;
        changeImg()
    }
    function changeImg(){
        if(currentNUM==0){
            currentNUM = 2;
        }else if(currentNUM == 3){
            currentNUM = 1;
        }
        img.src = "img/" + currentNUM + '.jpg';
        for(let i=0;i<allLi.length;i++){
            allLi[i].style.backgroundColor = "#aaa";
        }
        allLi[currentNUM-1].style.backgroundColor = 'red';
    }
    box.addEventListener('mouseover',function(){
        left_btn.style.display = 'block';
        right_btn.style.display = 'block';
        window.clearInterval(timer)
    })
    box.addEventListener('mouseout',function(){
        left_btn.style.display = 'none';
        right_btn.style.display = 'none';
        timer = setInterval(startloop,2000)
    })
    left_btn.addEventListener('mouseover',deep);
    left_btn.addEventListener('mouseout',undeep);
    right_btn.addEventListener('mouseover',deep);
    right_btn.addEventListener('mouseout',undeep);
    left_btn.addEventListener('click',function(){
        currentNUM--;
        changeImg();
    })
    right_btn.addEventListener('click',startloop);
    function deep(){
        this.style.backgroundColor = "rgba(0,0,0,0.4)"
    }
    function undeep(){
        this.style.backgroundColor = "rgba(0,0,0,0.2)"
    }
    for(let i=0;i<allLi.length;i++){
        allLi[i].index = i+1
        allLi[i].addEventListener('mouseover',function(){
            // allLi[0]
            currentNUM = this.index
            changeImg();
        })
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值