正在回答 回答被采纳积分+1
2回答
丶DamonChan
2018-02-14 22:27:16
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript轮播图</title> <link rel="stylesheet" type="text/css" href="css/index.css" /> </head> <body> <div class="banner" id="banner"> <!--导航--> <div class="nav" id="nav"> <div class="hover">首页</div> <div>点击看看</div> <div>会自动的</div> <div>我的网站</div> </div> <!--轮播图--> <div class="navImg" id="navImg"> <div class="navImg-list1"></div> <div class="navImg-list2"></div> <div class="navImg-list3"></div> <div class="navImg-list4"></div> </div> </div> <script src="js/index.js" type="text/javascript" charset="utf-8"></script> </body> </html>
* { padding: 0; margin: 0; font-family: "微软雅黑"; font-size: 22px; } /*轮播图整体*/ .banner { width: 1200px; height: 500px; margin: 0 auto; border: 1px solid rgba(188, 188, 188, 0.3); } /*导航*/ .nav { width: 1200px; height: 40px; line-height: 40px; background-color: #FFFFFF; } .nav div { width: 300px; height: 40px; float: left; text-align: center; border-radius: 4px; cursor: pointer; } /*轮播图*/ .navImg { width: 1200px; height: 460px; position: relative; } .navImg div { width: 1200px; height: 460px; float: left; cursor: pointer; overflow: hidden; position: absolute; display: none; } .navImg .navImg-list1 { background-image: url(../img/1.jpg); display: block; } .navImg .navImg-list2 { background-image: url(../img/3.jpg); } .navImg .navImg-list3 { background-image: url(../img/4.jpg); } .navImg .navImg-list4 { background-image: url(../img/5.jpg); } /*选中效果*/ .hover { background-color: #FFCC00; color: #666666; font-weight: bold; }
window.onload = function() { var banner = document.getElementById('banner'), nav = document.getElementById('nav'), childNav = nav.getElementsByTagName('div'), navImg = document.getElementById("navImg"), childNavImg = navImg.getElementsByTagName('div'), index = 0, timer = null; //切换显示 function setNavListHover() { for(var i = 0; i < childNav.length; i++) { childNav[i].className = ""; childNavImg[i].style.display = "none"; } childNav[index].className = "hover"; childNavImg[index].style.display = "block"; } //开始自动轮播 function startAutoPlay() { timer = setInterval(function() { index++; if(index == childNav.length) { index = 0; } setNavListHover(); }, 1000); } //结束自动轮播 function stopAutoPlay() { clearInterval(timer); } function slideImg() { banner.onmouseout = function() { startAutoPlay() }; banner.onmouseover = function() { stopAutoPlay() }; banner.onmouseout(); //点选导航切换轮播图 for(var i = 0; i < childNav.length; i++) { childNav[i].id = i; childNav[i].onclick = function() { index = this.id; setNavListHover(); }; } } slideImg(); };
前端小白入门系列课程
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36713 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星