在前面的文章中,我们讲到了js中数组的具体操作,详见(Js对Array的各种操作总结),这里就不再做详细的讲解。今天主要的目的是如何用数组进行图片的简单左右切换。
本文中的图片切换具体步骤如下:
第1步:简单的布局并设计基本的显示样式;
第2步:通过js获取相关元素;
第3步: 通过数组进行图片url和对应文字描述的存储;
第4步:初始化:包括图片的初始化,显示图片数字以及对应文字的初始化等;
第5步:点击按钮切换图片,编写对应的函数,其实就是数组的简单应用。
接下来先看看效果图,然后进行对应的代码讲解。
实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片切换</title>
<style>
.box{
width: 600px;
height:400px;
border: 10px solid #ccc;
position: relative;
margin: 40px auto 0;
}
.box a{
width: 30px;
height: 30px;
background-color: #000;
border: 5px solid #fff;
position: absolute;
top:180px;
text-align: center;
font-size:25px;
font-weight: bold;
line-height: 30px;
color:#fff;
text-decoration: none;
filter: alpha(opacity:40);
opacity: 0.4;
}
.box a:hover{
filter:alpha(opacity:80);
opacity:0.8;
}
.box #prev{
left: 10px;
}
.box #next{
right: 10px;
}
#text,#num{
height: 30px;
line-height:30px;
width: 600px;
color:#fff;
position: absolute;
left: 0;
background-color: #000;
text-align: center;
filter: alpha(opacity:80);
opacity: 0.8;
margin:0;
}
.box #text{
bottom: 0;
}
.box #num{
top:0;
}
.box #img1{
width: 600px;
height: 400px;
}
</style>
<script>
window.onload = function () {
var oPrev = document.getElementById("prev");
var oNext = document.getElementById("next");
var oText = document.getElementById("text");
var oNum = document.getElementById("num");
var oImg = document.getElementById("img1");
var arrUrl = ['images/1.jpg','images/2.jpg','images/3.jpg','images/4.jpg'];
var arrText = ['文字1','文字2','文字3','文字4'];
//初始化
var num = 0;
function fnTab(){
oNum.innerHTML = num + 1 + '/' + arrText.length;
oImg.src = arrUrl[num];
oText.innerHTML = arrText[num];
};
fnTab();
oPrev.onclick = function(){
num --;
if( num == -1){
num = arrUrl.length -1;
}
fnTab();
};
oNext.onclick = function(){
num ++;
if(num == arrUrl.length){
num = 0;
}
fnTab();
};
};
</script>
</head>
<body>
<div class="box">
<a id="prev" href="javascript:;"> < </a>
<a id="next" href="javascript:;"> > </a>
<p id="text">图片正在加载中……</p>
<span id="num">数量正在统计中……</span>
<img id="img1" src="../images/1.jpg" alt="">
</div>
</body>
</html>
这个例子很简单,主要就是对数组的简单读写以及对html的属性内容读写操作。其中需要注意的是,当我们点击下一张到最后一张图片或者点击上一张到第一张时,需要设置具体的数值变化,不=否则会越界,那么图片、数字和对应的问题就没有内容。
另外做了一个可以顺序播放和循环播放图片的例子。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片切换</title>
<style>
body,p{
margin:0px;
padding:0px;
}
input{
outline: none;
border:none;
padding: 0;
}
.box{
width:320px;
height:320px;
position: relative;
margin:50px auto;
}
#img_list{
width:320px;
height:200px;
}
#btns{
text-align: center;
}
#btn1,#btn2,#pre,#next{
background-color: #727272;
color: #fff;
width:70px;
height:22px;
line-height:22px;
border-radius:4px;
text-align: center;
margin:auto auto;
font-size: 12px;
}
#btn1:hover,#btn2:hover{
background-color: #aeaeae;
color: #feffa8;
}
#btns #title,#num{
width:320px;
height: 30px;
line-height:30px;
font-size: 16px;
display: block;
}
#content{
width:320px;
text-align: center;
}
#content #con_title{
width: 320px;
height: 30px;
line-height:30px;
text-align: center;
background-color: #000;
filter: alpha(opacity:40);
opacity: 0.4;
position: absolute;
top:223px;
color: #fff;
}
#changeBtn{
width: 320px;
text-align: center;
}
</style>
<script>
window.οnlοad= function () {
var oBtn1 = document.getElementById("btn1");
var oBtn2 = document.getElementById("btn2");
var oTitle = document.getElementById("title");
var oImg = document.getElementById("img_list");
var oCon_title = document.getElementById("con_title");
var oNum = document.getElementById("num");
var oPre = document.getElementById("pre");
var oNext= document.getElementById("next");
var imgList = ['images/1.jpg','images/2.jpg','images/3.jpg','images/4.jpg','images/5.jpg'];
var titleList=['这是第一张图片','这是第二张图片','这是第三张图片','这是第四张图片','这是第五张图片'];
oNum.innerHTML=1+'/'+imgList.length;
oCon_title.innerHTML="这是第一张图片";
var num = 0;
var onoff = true;//true 顺序播放 false 循环播放
oBtn1.οnclick= function () {
onoff=true;
oTitle.innerHTML="图片顺序播放";
};
oBtn2.οnclick= function () {
onoff=false;
oTitle.innerHTML="图片循环播放";
};
oPre.οnclick= function () {
num--;
if(num<0){
if(onoff){//true 顺序播放
num=0;
alert("已经是第一张啦!");
}else{
num=imgList.length-1;
}
}
changeImg(num)
};
oNext.οnclick= function () {
num++;
if(num>imgList.length-1){
if(onoff){//true 顺序播放
num=imgList.length-1;
alert("已经是最后一张啦!");
}else{
num = 0;
}
}
changeImg(num);
};
function changeImg(num){
oImg.src= imgList[num];
oCon_title.innerHTML=titleList[num];
oNum.innerHTML=num+1+'/'+imgList.length;
};
};
</script>
</head>
<body>
<div class="box">
<div id="btns">
<input id="btn1" type="button" value="顺序播放"/>
<input id="btn2" type="button" value="循环播放"/>
<p id="title">图片顺序播放</p>
</div>
<div id="content">
<img id="img_list" src="images/1.jpg" alt="">
<p id="con_title"></p>
<span id="num"></span>
</div>
<div id="changeBtn">
<input id="pre" type="button" value="上一张">
<input id="next" type="button" value="下一张">
</div>
</div>
</body>
</html>