JavaScript实现图片顺序与循环切换功能详解

作者:函数茶馆

时间:2026-10-22

浏览:500

本文介绍基于JavaScript实现的图片切换功能,支持顺序播放与循环播放两种模式。代码包含HTML结构、CSS样式及JS逻辑,演示了如何控制图片索引及边界提示,适用于前端开发参考。

文章目录

JavaScript图片切换功能实现

本文通过具体示例,详细解析如何利用JavaScript实现图片的顺序切换与循环切换功能,旨在为读者提供可直接参考的实现方案。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>图片切换</title>
<style type="text/css">
#box{width:400px;margin:0 auto;}
#box>div{text-align: center;margin:10px auto;display: block;}
#box div>input{margin-left: 5px;border-radius: 3px;border:1px solid #ccc;}
#box>p{text-align: center;}
#content{width:400px;height:400px;margin:0 auto;position: relative;border:10px solid #ccc;}
#content a{width:40px;height:40px;background:#000;border:5px solid #fff;position: absolute;top:175px;text-align: center;text-decoration: none;line-height: 40px;color:#ccc;font-weight: 900;filter: alpha(opacity:40);opacity: 0.4;}
#content a:hover{filter: alpha(opacity:90);opacity: 0.9;}
#prev{left:10px;}
#next{right:10px;}
#text,#span{position: absolute;bottom: 0;left:0;width:400px;height:30px;background:#000;line-height: 30px;text-align: center;color: #fff;filter: alpha(opacity:80);opacity: 0.8;}
#text{margin:0;bottom: 0;}
#span{top:0;}
#img{width:400px;height:400px;}
</style>
<script type="text/javascript">
   window.onload=function(){
   var oPrev=document.getElementById('prev');
   var oNext=document.getElementById('next');
   var oText=document.getElementById('text');
   var oSpan=document.getElementById('span');
   var oImg=document.getElementById('img');
   var oBtn1=document.getElementById('btn1');
   var oBtn2=document.getElementById('btn2');
   var oP1=document.getElementById('p1');
   var arrUrl=['images/1.jpg','images/2.jpg','images/3.jpg','images/4.jpg'];
   var arrText=['baby','冰冰','唐嫣','杨幂'];
   var num=0;
   //初始化
   function fnTab(){
   oSpan.innerHTML=num+1+'/'+ arrText.length;
   oText.innerHTML=arrText[num];
   oImg.src=arrUrl[num];
      };
      fnTab();
   oBtn1.onclick=function(){
    oNext.onclick=function(){
    num++;
    if(num==arrText.length){
     num=0;
    }
     fnTab();
   };
    oPrev.onclick=function(){
    num--;
    if(num==-1){
     num=arrText.length-1;
    }
     fnTab();
   };
    /*oPrev.onclick=function(){
     if(0<num){
      num--;
      fnTab();
     }else{
      num=arrText.length;
      num--;
     }
    };*/
   };
    oBtn2.onclick=function(){
    oP1.innerHTML = '图片只能到最后一张或第一张切换';
    oNext.onclick=function(){
    if(num==arrText.length-1){
     alert('这是最后一张了。。');
    }else{
     num++;
    }
    /*if(num==arrText.length){
     alert('这是最后一张了。。');
    }*/
     fnTab();
   };
    oPrev.onclick=function(){
    if(num==0){
    alert('这已经是第一张了,不能再切换了。。');
    }else{
     num--;
    }
     fnTab();
   };
   };
   /* oNext.onclick=function(){
    num++;
    if(num==arrText.length){
    num=0;
    }
    fnTab();
   };
   oPrev.onclick=function(){
    num--;
    if(num==-1){
    num=arrText.length-1;
    }
    fnTab();
   };*/
   };
</script>
</head>
<body>
  <div id="box">
   <div>
    <input id="btn1" type="button" value="循环切换">
    <input id="btn2" type="button" value="顺序切换">
   </div>
   <p id="p1">图片可以从最后一张跳到第一张循环切换</p>
  </div>
  <div id="content">
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="prev"><</a>
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="next">></a>
    <p id="text">图片文字加载中......</p>
    <span id="span">数量正在计算中......</span>
    <img id="img" />
  </div>
</body>
</html>

该实现支持用户选择切换模式,包括顺序切换和循环切换。当采用顺序切换模式且播放至最后一张图片时,程序会给出相应提示。

以下展示了该功能在本机环境下的实际运行效果。

JavaScript图片切换功能实现

如需进一步了解JavaScript相关知识,建议参考本站相关专题,包括《JavaScript图片操作技巧大全》、《JavaScript切换特效与技巧总结》、《JavaScript运动效果与技巧汇总》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》。

希望上述内容能对从事JavaScript程序设计的读者提供有益帮助。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章