Javascript 实现的轮播图(原理及实现)


prtyaa
prtyaa 2023-12-26 18:48:43 52539
分类专栏: 资讯
 // 兼容模式获取样式(不仅仅是内联样式)
           function getStyle(obj, name) { // 兼容模式获取样式(不仅仅是内联样式)
                               if (window.getComputedStyle) {
                                       return getComputedStyle(obj, null)[name];
                               } else {
                                       return obj.currentStyle[name];
                               }
                       }

 

轮播图处理:(功能实现但是比较粗糙,后续优化)

<!DOCTYPE html>
<html>
    <head>
        <title>js轮播图--刻意练习</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            *{
                margin:0px;
                padding:0px;
            }
            #dynamic_pic{
                width:500px; /*固定图片的大小是 480 * 480  上下左右各有10px 的内边距*/
                height:480px;
                margin:20px auto;
                background-color:greenyellow;
                padding:10px 0;
                position:relative;
                overflow: hidden;
            }
            #dynamic_pic ul{
                list-style: none;
                position:absolute;
                top:10px;
                left:0px;
                width:1500px;
            }
            #dynamic_pic ul li{
                padding:0px 10px;
                float:left;
            }
            #dynamic_pic ul li img{
                width:480px;
                height:480px;
            }
            #dynamic_pic div{
                position:absolute;
                bottom:15px;
                left:212px;
            }
            #dynamic_pic div a{
                display:block;
                width:15px;
                height:15px;
                float:left;/* 设置超链接浮动 */
                margin:0 5px;
                background-color:red;
                opacity:0.8;
                /*兼容 IE8 设置透明度*/
                filter:alpha(opacity=80);
            }
            #dynamic_pic div a:hover{
                background-color:black;
            }



        </style>

        <script>

            window.onload = function () {

                var pic_index = 0;//图片数组的索引
                var timer; //定时器
                var images = ["testimg/1.jpg", "testimg/2.jpg", "testimg/3.jpg", "testimg/4.jpg", "testimg/5.jpg"]
                var rootDom = document.getElementById("dynamic_pic");
                var imgList = rootDom.getElementsByTagName("ul")[0];

                var navId = rootDom.getElementsByTagName("div")[0];

                for (i = 0; i < images.length; i++) {
                    var lidom = document.createElement("li");
                    var img = document.createElement("img");
                    img.setAttribute("src", images[i]);
                    lidom.appendChild(img);
                    imgList.appendChild(lidom);
                    var adom = document.createElement("a");
                    adom.setAttribute("href", "javascript:void(0)");
                    adom.setAttribute("class", "link");
                    if (i == 0) {
                        adom.style.backgroundColor = 'black';
                    }
                     adom.num=i;
                     adom.onclick=function(){
                         clearInterval(timer);
                         pic_index=this.num;
                         setFocus();
                         moveImage(imgList, "left", -500 * pic_index, 20, function () {
                                       autoChange();
                                });
                       return false;//阻止事件冒泡
                     }

                    navId.appendChild(adom);
                }
                /*中间的过渡效果*/
                var lidom = document.createElement("li");
                var img = document.createElement("img");
                img.setAttribute("src", images[0]);
                lidom.appendChild(img);
                imgList.appendChild(lidom);
                /*中间的过渡效果*/

                navId.style.left = Math.floor((500 - images.length * 15) / 2) + "px";
                imgList.style.width = 500 * (images.length+1) + "px";
                
                /*鼠标移动图片上的事件*/
                imgList.onmouseover=function(){
                      clearInterval(timer);
                }
                 /*鼠标移动图片上的事件*/
                imgList.onmouseout=function(){
                    autoChange();
                }
                
                 autoChange();
            function autoChange() {
                    timer = setInterval(function () {
                           pic_index++;
                            pic_index %= (images.length+1);
                           moveImage(imgList, "left", -500 * pic_index, 20, function () {
                                        setFocus();
                                });
                    }, 2000); 
            }
            


                 function setFocus() {
                                if (pic_index >= images.length) {
                                        pic_index = 0;
                                        imgList.style.left = 0;
                                } 
                                for (var i = 0; i < images.length; i++) {
               
                                        navId.getElementsByTagName("a")[i].style.backgroundColor = "";
                                }
                                navId.getElementsByTagName("a")[pic_index].style.backgroundColor = "black";
                        }


                 function moveImage(obj, attr, target, speed, callback) {
                              var current = parseInt(getStyle(obj, attr));
          
                              if (current > target) {
                                      speed = -speed;
                              }
                              //自定义对象定时器 防止对象之间的混乱操作 
                              clearInterval(obj.timer);
                              //alert(oldValue);
                              obj.timer = setInterval(function () {
                                      var oldValue = parseInt(getStyle(obj, attr));
                                      var newVal = oldValue + speed;
                                      //如果移动的越界 进行重置
                                      if ((speed < 0 && newVal <= target) || (speed > 0 && newVal >= target)) {
                                              newVal = target;
                                      }
                                      obj.style[attr] = newVal + "px";
                                      if (newVal == target) {
                                              clearInterval(obj.timer);
                                              callback && callback();//回掉函数 先判断 有就执行 没有不执行
                                      }  
                              }, 30);
                      }
      
                      //obj:获取样式元素
                       //name:获取样式名
                       function getStyle(obj, name) { // 兼容模式获取样式(不仅仅是内联样式)
                               if (window.getComputedStyle) {
                                       return getComputedStyle(obj, null)[name];
                               } else {
                                       return obj.currentStyle[name];
                               }
                       }
            }

        </script>
    </head>
    <body>
        <div id="dynamic_pic">
             <ul>
            </ul>
              <div>
            </div>
        </div>

    </body>
</html>

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=31046
赞同 0
评论 0 条
prtyaaL2
粉丝 1 发表 2553 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2959
【软件正版化】软件正版化工作要点  2878
统信UOS试玩黑神话:悟空  2843
信刻光盘安全隔离与信息交换系统  2737
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1270
grub引导程序无法找到指定设备和分区  1235
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
点击报名 | 京东2025校招进校行程预告  164
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!