用js实现图片轮播

news/2024/11/13 4:24:22/

项目收获
封装getElementById(),使用更方便。
Function (id){
Return typeof(id)===”string”?Document.getElementById(id):id;
}
getElementsByClassName IE8以下版本不支持
图片与圆点的联动通过index,和一个方法来实现
自定义属性
item[i].setAttribute(“data-index”,i);
dix=this.getAttribute(“data-index”);

步骤
1.封装getElementById()
2.实现定时器(用定时器使index变换,使图片浮现)
3.实现圆点点击(给id命为数字方法,但只能用一次id只能有一个)
4.实现><点击(给index自增或自减)
5.实现菜单浮现(用setAttribute,getAttribute),对className,dispaly,background运用

代码  分为html ,css ,js
html部分:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="css/style.css">
</head>
<body><div class="main" id="main"><div class="sub-menu hide" id="sub-menu"><div class="inner-box "><div class="sub-inner-box"><div class="title">手机 、配件</div><div class="sub-row"><span class="bold mr10">手机通讯:</span><a href="#">手机</a><span class="mr10 ml10">/</span><a href="#">手机维修</a><span class="mr10 ml10">/</span><a href="#">以旧换新</a></div><div class="sub-row"><span class="bold mr10">手机配件:</span><a href="#">手机壳</a><span class="mr10 ml10">/</span><a href="#">手机储存卡</a><span class="mr10 ml10">/</span><a href="#">数据线</a><span class="mr10 ml10">/</span><a href="#">充电器</a><span class="mr10 ml10">/</span><a href="#">电池</a></div><div class="sub-row"><span class="bold mr10">运营商:</span><a href="#">中国联通</a><span class="mr10 ml10">/</span><a href="#">中国移动</a><span class="mr10 ml10">/</span><a href="#">中国电信</a></div><div class="sub-row"><span class="bold mr10">智能设备:</span><a href="#">智能手环</a><span class="mr10 ml10">/</span><a href="#">智能家居</a><span class="mr10 ml10">/</span><a href="#">智能手表</a><span class="mr10 ml10">/</span><a href="#">其他配件</a></div><div class="sub-row"><span class="bold mr10">娱乐:</span><a href="#">耳机</a><span class="mr10 ml10">/</span><a href="#">收音机</a><span class="mr10 ml10">/</span><a href="#">麦克风</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">电脑</div><div class="sub-row"><span class="bold mr10">电脑:</span><a href="">笔记本</a><span class="ml10 mr10">/</span><a href="">平板</a><span class="ml10 mr10">/</span><a href="">一体机</a></div><div class="sub-row"><span class="bold mr10">电脑配件:</span><a href="">显示器</a><span class="ml10 mr10">/</span><a href="">CPU</a><span class="ml10 mr10">/</span><a href="">主板</a><span class="ml10 mr10">/</span><a href="">硬盘</a><span class="ml10 mr10">/</span><a href="">电源</a><span class="ml10 mr10">/</span><a href="">显卡</a><span class="ml10 mr10">/</span><a href="">其他配件</a></div><div class="sub-row"><span class="bold mr10">游戏设备:</span><a href="">游戏机</a><span class="ml10 mr10">/</span><a href="">耳机</a><span class="ml10 mr10">/</span><a href="">游戏软件</a></div><div class="sub-row"><span class="bold mr10">网络产品:</span><a href="">路由器</a><span class="ml10 mr10">/</span><a href="">网络机顶盒</a><span class="ml10 mr10">/</span><a href="">交换机</a><span class="ml10 mr10">/</span><a href="">存储卡</a><span class="ml10 mr10">/</span><a href="">网卡</a></div><div class="sub-row"><span class="bold mr10">外部产品:</span><a href="">鼠标</a><span class="ml10 mr10">/</span><a href="">键盘</a><span class="ml10 mr10">/</span><a href="">U盘</a><span class="ml10 mr10">/</span><a href="">移动硬盘</a><span class="ml10 mr10">/</span><a href="">鼠标垫</a><span class="ml10 mr10">/</span><a href="">电脑清洁</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">家用电器</div><div class="sub-row"><span class="bold mr10">电视:</span><a href="">国产品牌</a><span class="ml10 mr10">/</span><a href="">韩国品牌</a><span class="ml10 mr10">/</span><a href="">欧美品牌</a></div><div class="sub-row"><span class="bold mr10">空调:</span><a href="">显示器</a><span class="ml10 mr10">/</span><a href="">柜式</a><span class="ml10 mr10">/</span><a href="">中央</a><span class="ml10 mr10">/</span><a href="">壁挂式</a></div><div class="sub-row"><span class="bold mr10">冰箱:</span><a href="">多门</a><span class="ml10 mr10">/</span><a href="">对开门</a><span class="ml10 mr10">/</span><a href="">三门</a><span class="ml10 mr10">/</span><a href="">双门</a></div><div class="sub-row"><span class="bold mr10">洗衣机:</span><a href="">滚筒式洗衣机</a><span class="ml10 mr10">/</span><a href="">迷你洗衣机</a><span class="ml10 mr10">/</span><a href="">洗烘一体机</a></div><div class="sub-row"><span class="bold mr10">厨房电器:</span><a href="">油烟机</a><span class="ml10 mr10">/</span><a href="">洗碗机</a><span class="ml10 mr10">/</span><a href="">燃气灶</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">家具</div><div class="sub-row"><span class="bold mr10">家纺:</span><a href="">被子</a><span class="ml10 mr10">/</span><a href="">枕头</a><span class="ml10 mr10">/</span><a href="">四件套</a><span class="ml10 mr10">/</span><a href="">床垫</a></div><div class="sub-row"><span class="bold mr10">灯具:</span><a href="">台灯</a><span class="ml10 mr10">/</span><a href="">顶灯</a><span class="ml10 mr10">/</span><a href="">节能灯</a><span class="ml10 mr10">/</span><a href="">应急灯</a></div><div class="sub-row"><span class="bold mr10">厨具:</span><a href="">烹饪锅具</a><span class="ml10 mr10">/</span><a href="">餐具</a><span class="ml10 mr10">/</span><a href="">菜板刀具</a></div><div class="sub-row"><span class="bold mr10">家装:</span><a href="">地毯</a><span class="ml10 mr10">/</span><a href="">沙发垫套</a><span class="ml10 mr10">/</span><a href="">装饰字画</a><span class="ml10 mr10">/</span><a href="">照片墙</a><span class="ml10 mr10">/</span><a href="">窗帘</a></div><div class="sub-row"><span class="bold mr10">生活日用:</span><a href="">收纳用品</a><span class="ml10 mr10">/</span><a href="">浴室用品</a><span class="ml10 mr10">/</span><a href="">雨伞雨衣</a></div></div></div></div><div class="menu-content" id="menu-content"><div class="menu-item"><a href="#"><span>手机 、配件</span><i class="iconfont">&#xe665;</i></a></div><div class="menu-item"><a href="#"><span>电脑</span><i  class="iconfont">&#xe665;</i></a></div><div class="menu-item"><a href="#"><span>家用电器</span><i  class="iconfont">&#xe665;</i></a></div><div class="menu-item"><a href="#"><span>家具</span><i  class="iconfont">&#xe665;</i></a></div></div><!-- 图片轮播 --><div class="banner" id="banner"><a href="#"><div class="banner-slide slide1 slide-active"></div></a><a href="#"><div class="banner-slide slide2"></div></a><a href="#"><div class="banner-slide slide3"></div></a></div><!-- 上一张,下一张按钮 --><a href="javascript:void(0)" class="button prev" id="prev"></a><a href="JavaScript:void(0)" class="button next" id="next"></a><!-- 远点导航 --><div class="dot" id="dot"><span class="active"></span><span></span><span></span></div></div>
</body>
<script src="js/carousel.js"></script>
</html>
Css部分:
*{margin: 0px;padding: 0px;
}
ul{list-style: none;
}
a{text-decoration: none;color: black;
}
@font-face {font-family: 'iconfont';src: url('../img/font/iconfont.eot');src: url('../img/font/iconfont.eot?#iefix') format('embedded-opentype'),url('../img/font/iconfont.woff') format('woff'),url('../img/font/iconfont.ttf') format('truetype'),url('../img/font/iconfont.svg#iconfont') format('svg');
}
body{font-family: "微软雅黑";color: #14191E;
}
.main{width: 1200px;height: 460px;margin: 40px auto;overflow: hidden;position: relative;
}
.banner{width: 1200px;height: 460px;position: relative;
}
.banner .banner-slide{width: 1200px;height: 460px;background-repeat: no-repeat;position: absolute;display: none;
}
.banner .slide-active{display: block;
}
.banner  .slide1{background-image: url(../img/bg1.jpg);
}
.banner  .slide2{background-image: url(../img/bg2.jpg);
}
.banner  .slide3{background-image: url(../img/bg3.jpg);
}
.main .button{width: 40px;height: 80px;background-color: black;position: absolute;left: 244px;top: 50%;margin-top: -40px;background: url(../img/arrow.png) no-repeat center center;
}
.button:hover{background-color: #000000;opacity: 0.8;filter:alpha(opacity=80);
}
.main .prev{transform: rotate(180deg);
}
.main .next{left: auto;right: 0;
}
.main .dot{position: absolute;right: 10px;bottom: 24px;}
.main .dot span{width: 12px;height: 12px;border-radius: 50%;background-color: rgba(7,17,27,0.4);box-shadow: 0 0 0 2px rgba(255,255,255,0.8) inset;display: inline-block;margin-left: 8px;cursor: pointer;
}
.main .dot .active{box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;background: #FFFFFF;
}
.main .menu-box{width: 244px;height: 460px;position: absolute;left: 0;top: 0;background: rgb(7,17,27,0.5);opacity: 0.5;z-index: 1;
}
.main .menu-content{width: 244px;height: 454px;position: absolute;left: 0;top: 0;padding-top: 6px;z-index: 2;background: rgba(192,196,200,0.5);
}
.menu-item{height:64px;line-height: 66px;font-size: 16px;padding: 0 24px;position: relative;
}
.menu-item a:link,.menu-item a:visited{color: white;
}
.menu-item a{display: block;border-bottom: 1px solid rgba(255,255,255,0.2);padding: 0 8px;height: 63px;
}.menu-item i{position: absolute;right: 32px;font-size: 24px;top: 2px;font-family:"iconfont";font-style: normal;font-weight: normal;color: rgba(255,255,255,0.5);
}
.sub-menu{width: 730px;height: 458px;border: 1px solid #d9dde1;background: white;position: absolute;left: 244px;top: 0;z-index: 999;box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1);
}
.inner-box{width: 100%;height: 100%;background: url(../img/fe.png) no-repeat;display: none;  
}
.sub-inner-box{width: 652px;margin-left: 40px;overflow: hidden;
}
.title{color: #f01414;font-size: 16px;margin: 28px 0 30px 0;font-weight: bold;
}
.sub-row{margin-top: 25px;
}
.sub-row .bold{font-weight: bold;
}
.sub-row .mr10{margin-right: 10px;
}
.sub-row .ml10{margin-left: 10px;
}
.hide{display: none;
}
Js部分:
//封装getElementById()
function ById(id){return typeof(id)==="string"?document.getElementById(id):id;
}
main=ById(main);
banner=ById(banner);
pics=banner.getElementsByTagName("div");
dot=ById(dot).getElementsByTagName("span");
timer=null;
index=0;
len=pics.length;
menu=ById("menu-content");
item=menu.getElementsByClassName("menu-item");
sub=ById("sub-menu");
inner=sub.getElementsByClassName("inner-box");function img(){//菜单显示for(j=0;j<item.length;j++){item[j].setAttribute("data-index",j);item[j].onmouseover=function(){dix=this.getAttribute("data-index");console.log(dix);sub.className="sub-menu";for(i=0;i<item.length;i++){item[i].style.background="none";inner[i].style.display="none";}this.style.background="rgba(0,0,0,0.1)";inner[dix].style.display="block";}}//菜单消失menu.onmouseout=function(){sub.className="sub-menu hide";for(i=0;i<item.length;i++){item[i].style.background="none";}}//移动到二级菜单时不会消失sub.onmouseover=function(){sub.className="sub-menu";}//鼠标不在二级菜单时消失sub.onmouseout=function(){sub.className="sub-menu hide";}//清除定时器main.onmouseover=function(){if(timer) clearInterval(timer);}//不在main范围时,自动轮播main.onmouseout=function(){timer=setInterval(function(){for(i=0;i<pics.length;i++){index++;if(index>=len) index=0;changeImg(index);}}, 2000);}main.onmouseout();//圆点点击for(i=0;i<len;i++){dot[i].id=i;dot[i].onclick=function(){index=this.id;changeImg(index);}}//点击<prev=ById(prev);prev.onclick=function(){index--;if(index<0) index=len-1;changeImg(index);}//点击>next=ById(next)next.onclick=function(){index++;if(index>=len) index=0;changeImg(index);}//鼠标移动菜单浮现}
function changeImg(index){for(i=0;i<len;i++){pics[i].style.display='none';dot[i].className="";}pics[index].style.display='block';dot[index].className="active";
}
img();

http://www.ppmy.cn/news/317788.html

相关文章

计算机毕业设计(附源码)python校园洗衣机预约系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

【附源码】计算机毕业设计java校园洗衣机预约系统设计与实现

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

STM32状态机编程实例——全自动洗衣机(下)

上篇文章&#xff0c;通过状态机编程&#xff0c;实现了全自动洗衣机的逻辑控制&#xff0c;并通过串口打印的方式显示各个状态。 本篇&#xff0c;为了更加直观的感受状态机的运行&#xff0c;使用0.96寸OLED来显示各个状态&#xff0c;并搭配对应的动态图标来体现洗衣机工作…

模糊控制洗衣机的设计(MATLAB)

模糊控制洗衣机的设计 一、背景 模糊控制洗衣机的实现方法&#xff1a;如图1为给出模糊控制洗衣机的控制结构图, 它是利用负载 它是利用负载、质料质料、水位水位、水温以及洗涤剂等检测水所得到的信息, 进行分段评估计算, 使其模糊化, 再根据模糊规则进行推理, 以决定最适当…

(附源码)SSM校园洗衣机预约系统JAVA计算机毕业设计项目

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

highlight clock tree 高亮时钟树的方法

当分析clock tree需要在图形界面highlight clock tree时&#xff0c;最朴实无华的方法就是贴报告&#xff0c;除此之外这里也分享一下用命令的方法。 1.Imported Path Pins 1&#xff09;Highlight > Color By > Imported Path Pins 2&#xff09;report_timing icc2…

关于多媒体计算机的手抄报,有关科技的手抄报图片大全

科学技术的飞速发展&#xff0c;使整个社会生活的物质文明达到了空前的高度。科技手抄报你知道怎么做吗?小编分享科技的手抄报图片及资料&#xff0c;希望可以帮助大家。 科技的手抄报的图片欣赏 科技的手抄报图片1 科技的手抄报图片2 科技的手抄报图片3 科技的手抄报图片4 科…

金字塔的思维--思维的格式化与体系化

一个最出色的思考者&#xff0c;他能看到一个以目标为核心向四周进行辐射的立体结构。所以出色的思考者的思考方式永远都是像是在架构一个最具说服力的逻辑组合以及体系。 不过多数人并不具备完整的结构化思考的能力&#xff0c;我们常常停留在感官化思维、平面化思维或者线性思…