JavaScript_banner轮播图

让我们一起来学习一下用js怎么实现banner轮播图呢?

直接看代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>banner轮播</title>
<style>
#banner{width:820px;height:430px;margin:0 auto;position:relative;}
#banner img{width:100%;height:100%;}
ul{position:absolute;top:83%;left:290px;list-style:none;}
ul li{width:25px;height:25px;border-radius:50%;float:left;margin-right:15px;text-align:center;line-height:25px;}
#Left,#Right{position:absolute;top:45%;width:60px;height:60px;display:none;}
#banner:hover #Left{display:block;}
#banner:hover #Right{display:block;}
#Left{left:0;}
#Right{right:0;}
</style>
</head>
<!--页面加载的时候直接加载它-->
<body onload="lunbo()">
<div id="banner">
<img src="img/banner0.jpg" id="img">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="Left">
<img src="img/07_箭头_向左.png" id="left">
</div>
<div id="Right">
<img src="img/07_箭头_向右 (1).png" id="right">
</div>
</div>
<script type="text/javascript">
//首先我们要获取到他们,便于接下来操作
var Img=document.getElementById("img");
var Lis=document.getElementsByTagName("li");
var Left=document.getElementById("left");
var Right=document.getElementById("right");
var index=-1;
var Banner=document.getElementById("banner");
//定时器(需要定义的函数,它的毫秒数)
var timer=setInterval("lunbo()",1800);
//利用定时器使图片达到轮播效果
function lunbo(){
index++;
resetColor();
if(index == 4){
index=0;
}
Img.src="img/banner"+index+".jpg";
Lis[index].style.background="orchid";
}
//小原点初始值颜色(定义函数,在定时器去调用它)
function resetColor(){
for(var i=0;i<Lis.length;i++){
Lis[i].style.background="rgba(100,100,100,.5)";
}
}
//鼠标移入和移出
Banner.onmouseover=function(){
clearInterval(timer);
}
Banner.onmouseout=function(){
//变量作用域,因为这边已经给它清除了,所以必须重新声明它.
timer=setInterval("lunbo()",1800);
}
//点击小圆点切换图片到指定位置
for (var i=0;i<Lis.length;i++) {
Lis[i].onclick = function(){
clearInterval(timer);
index = this.innerHTML-1;
Img.src="img/banner"+index+".jpg";
resetColor();
Lis[index].style.background = "orchid";
timer = setInterval("lunbo()",1800);
}
}
//左边和右边按钮切换
Left.onclick = function(){
index--;
if (index == -1) {
index = 3;
}
Img.src="img/banner"+index+".jpg";
resetColor();
Lis[index].style.background = "orchid";
}
Right.onclick = function(){
if (index == 3) {
index = -1;
}
index++;
Img.src="img/banner"+index+".jpg";
resetColor();
Lis[index].style.background = "orchid";
}
</script>
</body>
</html>

希望对大家有帮助~~如果有更好的方法,可以一起学习交流哦!

最新文章

  1. Spring(二)__bean的装配
  2. 【代码笔记】iOS-获取系统完成任务所需的后台时间
  3. Excel2016右键新建工作表,打开时提示“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决办法
  4. org.apache.commons.lang.StringUtils类
  5. Java for LeetCode 189 Rotate Array
  6. python_way day12 sqlalchemy,原生mysql命令
  7. WP8 学习 ApplicationBar 的创建 XAML代码
  8. C#委托的语法
  9. socket编程里的read和recv函数【转载】
  10. 在X64系统中PowerDesigner无法连接MySQL的解决方法
  11. Flask第三方工具组件介绍
  12. 了解Vue.js
  13. 【Java并发编程】Callable、Future和FutureTask的实现
  14. SpringMVC之编程式校验
  15. 【代码笔记】iOS-只让textField使用键盘通知
  16. UITextField in a UITableViewCell
  17. 安装cartographer
  18. 超全面的JavaWeb笔记day09&lt;Servlet&amp;GenericServlet&amp;HttpServlet&amp;ServletContext&gt;
  19. Python开发【Django】:中间件、CSRF
  20. hdu4678 Mine 规律或者博弈。(博弈的sg函数不懂我是找的规律)

热门文章

  1. JSP官方文档(JavaServer Pages Specification)下载
  2. Egiht(八种方法)
  3. 【Python爬虫】第四课(查询照片拍摄地址)
  4. 让API实现版本管理的实践
  5. 无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”
  6. WordPress对接微信小程序遇到的问题
  7. jenkins自动化部署项目7 -- 新建job(将服务代码部署在windows上)
  8. 第十一周java课堂测试
  9. Android Studio [相对布局RelativeLayout]
  10. flask+layui+echarts实现前端动态图展示数据