欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现可切换图片的幻灯切换效果示例

程序员文章站 2023-12-17 17:36:22
本文实例讲述了js实现可切换图片的幻灯切换效果。分享给大家供大家参考,具体如下:

本文实例讲述了js实现可切换图片的幻灯切换效果。分享给大家供大家参考,具体如下:

<!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>js切换图片幻灯切换效果</title>
  <style>
    body, div, ul, li { margin: 0; padding: 0; }
    ul { list-style-type: none; }
    body { background: #000; text-align: center; font: 12px/20px arial; }
    #box { position: relative; width: 322px; height: 172px; background: #fff; border-radius: 5px; border: 8px solid #fff; margin: 10px auto; }
    #box .list { position: relative; width: 320px; height: 240px; overflow: hidden; border: 1px solid #ccc; }
    #box .list li { position: absolute; top: 0; left: 0; width: 320px; height: 240px; opacity: 0; filter: alpha(opacity=0); }
    #box .list li.current { opacity: 1; filter: alpha(opacity=100); }
    #box .count { position: absolute; right: 0; bottom: 5px; }
    #box .count li { color: #fff; float: left; width: 20px; height: 20px; cursor: pointer; margin-right: 5px; overflow: hidden; background: #f90; opacity: 0.7; filter: alpha(opacity=70); border-radius: 20px; }
    #box .count li.current { color: #fff; opacity: 1; filter: alpha(opacity=100); font-weight: 700; background: #f60; }
    #tmp { width: 100px; height: 100px; background: red; position: absolute; }
  </style>
  <script type="text/javascript">
    window.onload = function() {
      var obox = document.getelementbyid("box");
      var aul = document.getelementsbytagname("ul");
      var aimg = aul[0].getelementsbytagname("li");
      var anum = aul[1].getelementsbytagname("li");
      var timer = play = null;
      var i = index = 0;
      var border = true;
      //切换按钮
      for(i = 0; i < anum.length; i++) {
        anum[i].index = i;
        anum[i].onmouseover = function() {
          show(this.index)
        }
      }
      //鼠标划过关闭定时器
      obox.onmouseover = function() {
        clearinterval(play)
      };
      //鼠标离开启动自动播放
      obox.onmouseout = function() {
        autoplay()
      };
      //自动播放函数
      function autoplay() {
        play = setinterval(function() {
          //判断播放顺序
          border ? index++ : index--;
          //正序
          index >= aimg.length && (index = aimg.length - 2, border = false);
          //倒序
          index <= 0 && (index = 0, border = true);
          //调用函数
          show(index)
        }, 2000);
      }
      autoplay();//应用
      function show(a) {
        index = a;
        var alpha = 0;
        for(i = 0; i < anum.length; i++)anum[i].classname = "";
        anum[index].classname = "current";
        clearinterval(timer);
        for(i = 0; i < aimg.length; i++) {
          aimg[i].style.opacity = 0;
          aimg[i].style.filter = "alpha(opacity=0)";
        }
        timer = setinterval(function() {
          alpha += 2;
          alpha > 100 && (alpha = 100);
          aimg[index].style.opacity = alpha / 100;
          aimg[index].style.filter = "alpha(opacity = " + alpha + ")";
          alpha == 100 && clearinterval(timer)
        }, 20);
      }
    };
  </script>
</head>
<body>
<div id="box">
  <ul class="list">
    <li class="current"><img src="img/3.jpg" width="320" height="240"/></li>
    <li><img src="img/1.jpg" width="320" height="240"/></li>
    <li><img src="img/2.jpg" width="320" height="240"/></li>
    <li><img src="img/3.jpg" width="320" height="240"/></li>
    <li><img src="img/4.jpg" width="320" height="240"/></li>
  </ul>
  <ul class="count">
    <li class="current">1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
  </ul>
</div>
</body>
</html>

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript切换特效与技巧总结》、《javascript查找算法技巧总结》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。

上一篇:

下一篇: