首页 > 其他分享 >js实现轮播图(下面附有高清图)

js实现轮播图(下面附有高清图)

时间:2022-12-09 12:36:17浏览次数:36  
标签:style 轮播 img getElementsByTagName 高清 js li backgroundColor document


我所实现的和别的博主大大不太一样,我所采用的的是z-index方式以及浮动的方式,实现的轮播图

实现的效果图为:

js实现轮播图(下面附有高清图)_css

html主要实现代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/轮播图.css"/>
<script src="js/轮播图.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="all">
<img src="img/girl2.jpg"/>
<img src="img/girl3.jpg"/>
<img src="img/girl4.jpg"/>
<img src="img/girl5.jpg"/>
<img src="img/girl6.jpg"/>
<span id="left"><</span>
<span id="right">></span>
<div id="spot">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</body>
</html>

css样式代码:

#all{
height: 500px;
width: 350px;
margin: 0px auto;
border: 1px solid gainsboro;
position: relative;
}
img{
height: 500px;
width: 350px;
/*float: left;*/
position: absolute;
z-index: -2;
}
span{
display: inline-block;
/*z-index: 5;*/
height: 40px;
width: 25px;
line-height: 40px;
text-align: center;
background-color: lightgray;
}
#left{
border-radius: 0px 40px 40px 0px;
float: left;
/*margin-left: auto;*/
margin-top: 230px;
cursor: pointer;
}
#right{
border-radius: 40px 0px 0px 40px;
float: right;
margin-top: 230px;
cursor: pointer;
}
ul,li{
list-style-type: none;
}
li{
height: 20px;
width: 20px;
background-color: #CCCCCC;
display: inline-block;
border-radius: 20px;
margin-right: 10px;
}
#spot{
text-align: center;
margin-top: 460px;
}

js效果代码:

window.onload=function(){
var i = 0;
var img;
var li;
var time1;
function time(){
time1=setInterval(function(){//自动拨动

img = document.getElementsByTagName("img")[i];
img.style.zIndex=-2;

li = document.getElementsByTagName("li")[i];
li.style.backgroundColor="#CCCCCC";
i++;
if(i==5){
i=0;
}
if(i>=0){
li=document.getElementsByTagName("li")[i];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-1;
}else if(i==0){
li=document.getElementsByTagName("li")[4];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[4];
img.style.zIndex=-1;
}
},1500);
};
time();
var left = document.getElementById("left");
left.onclick = function(){
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-2;
li = document.getElementsByTagName("li")[i];
li.style.backgroundColor="#CCCCCC";
i--;
if(i>=0){
li=document.getElementsByTagName("li")[i];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-1;
}else if(i==-1){
li=document.getElementsByTagName("li")[4];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[4];
img.style.zIndex=-1;
}
if(i==-1){
i=4;
}
}
var right = document.getElementById("right");
right.onclick = function(){
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-2;
li = document.getElementsByTagName("li")[i];
li.style.backgroundColor="#CCCCCC";
i++;
if(i==5){
i=0;
}
if(i>=0){
li=document.getElementsByTagName("li")[i];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-1;
}else if(i==0){
li=document.getElementsByTagName("li")[0];
li.style.backgroundColor="red";
img = document.getElementsByTagName("img")[0];
img.style.zIndex=-1;
}
}
var list =document.getElementsByTagName('li');
for(var j=0;j<5;j++){
(function(j){
list[j].onmouseover=function () {
clearInterval(time1);
li=document.getElementsByTagName("li")[i];
li.style.backgroundColor="#CCCCCC";
img = document.getElementsByTagName("img")[i];
img.style.zIndex=-2;
this.style.backgroundColor="red";
img = document.getElementsByTagName("img")[j];
img.style.zIndex=-1;
}
list[j].onmouseout=function () {
i=j;
time();
}
})(j);
}
}

采用的图片:

js实现轮播图(下面附有高清图)_css_02


js实现轮播图(下面附有高清图)_轮播图_03

js实现轮播图(下面附有高清图)_css_04


js实现轮播图(下面附有高清图)_轮播图_05

js实现轮播图(下面附有高清图)_html_06


标签:style,轮播,img,getElementsByTagName,高清,js,li,backgroundColor,document
From: https://blog.51cto.com/u_15907536/5924858

相关文章

  • js实现点名系统
    实现效果图为:<html><head><metacharset="UTF-8"><title></title><styletype="text/css">#p2{font-size:28px;/*margin:0pxauto;*......
  • jsp当中web程序的调试与排错
    常出现的有4中:1.常见错误:未启动Tomcat:2.常见错误:未部署Web应用:3.常见错误:URL输入错误:localhost可以使得127.0.0.1进行代替!!!4.常见错误:目录不能被引用:......
  • Python对json的操作总结
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • springmvc案例(依赖,插件、配置文件、controller层、jsp页面展示)
    第一步:创建一个maven的web工程第二步:进行导入依赖(spring的依赖应该也需要)<!--mvc的依赖--><dependency><groupId>org.springframework</groupId><artifac......
  • json的使用(对象转字符串、字符串转对象)以及具体的使用(结合ajax使用)
    为了方便地处理JSON数据,JSON提供了json.js包,​​下载地址​​​注意:GSON为json的升级版,更容易使用,​​下载地址​​JSON结构有两种结构:json简单说就是javascript中的对象......
  • threeJs 创建文字方式
    一,使用 TextGeometry创建3D文字1.需加载字体配合使用,使用THREE.FontLoader//加载字体loadFont(){returnnewPromise(function(resolve,reject){......
  • PHP 发送application/json POST请求
    PHP用CURL发送Content-type为application/json的POST请求方法functionjson_post($url,$data=NULL){$curl=curl_init();curl_setopt($cur......
  • 用Python代码将XML转为JSON(或dict,字典)
    1.下面的Python代码将任意XML格式文件转化为JSON格式(字典)。除Python自带的模块外,不需要依赖其他任何第三方库。2.XML文件的读取使用Python自带的XML模块。3.关键代码如下......
  • 【1】认识NodeJS
    1、什么是NodeJS?大家都知道,JavaScript是运行在浏览器上的脚本语言,简称JS,代码由浏览器解释后执行。JS不能脱离浏览器运行在服务器上,但NodeJS把JS的战场从浏览器转移到......
  • websocket(二)——基于node js 的同步聊天应用
     ​​https://github.com/sitegui/nodejs-websocket​​1.用node搭建本地服务npminstallnodejs-websocket varws=require("nodejs-websocket");varserver=ws.cr......