首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

原生js实现走马灯

Sonder
2020-09-23
11040字
28分钟
浏览 (2.6k)

代码如下:

复制代码
<html>
<head>
   <meta charset="utf-8" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <title></title>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>


#div1{
           height: 125px;
           position: relative;
           overflow: hidden;

       }
       #div1 ul{
           position:absolute;
           left: 0;
           top: 0;
       }
       #div1 ul li {
           float: left;
           width: 125px;
           height: 125px;
           list-style: none;
           margin-left: 80px;
       }
       #div1 img {
   height: auto;
   max-width: 100%;
   border: none;
   -webkit-border-radius: 0;
   border-radius: 0;
   -webkit-box-shadow: none;
   box-shadow: none;
}

   </style>
   <script>
   </script>
</head>
<body>
   <div class="swiper-wrapper" id="div1">
       <ul style="width: 3000px;">
           <li>
               <div class="swiper-slide">
                   <a href="https://www.ray52.com" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/raybet-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a href="https://5djlm.com/register.html?c=0DLFD" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/djlm-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/jingjibao-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/anggame-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/edj.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/thunderfire-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/uwin-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/vkgame-1.png">
                   </a>
               </div>
           </li>

           <li>
               <div class="swiper-slide">
                   <a href="https://www.ray52.com" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/raybet-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a href="https://5djlm.com/register.html?c=0DLFD" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/djlm-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/jingjibao-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/anggame-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/edj.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/thunderfire-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/uwin-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/vkgame-1.png">
                   </a>
               </div>
           </li>

           <li>
               <div class="swiper-slide">
                   <a href="https://www.ray52.com" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/raybet-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a href="https://5djlm.com/register.html?c=0DLFD" target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/djlm-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/jingjibao-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/anggame-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/edj.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/thunderfire-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/uwin-1.png">
                   </a>
               </div>
           </li>
           <li>
               <div class="swiper-slide">
                   <a target="_blank" rel="nofollow noopener">
                       <img src="https://iframe.stjjc.cn/static/images/vkgame-1.png">
                   </a>
               </div>
           </li>
       </ul>
   </div>
   <script type="text/javascript">
       window.onload = function(){
           var oDiv = document.getElementById('div1');
           var oUl = oDiv.getElementsByTagName('ul')[0];
           var aLi = oUl.getElementsByTagName('li');
           var aA = oDiv.getElementsByTagName('a');
           var iSpeed = 1;//正左负右
           var timer = null;
           //计算ul的宽为所有li的宽的和;
           oUl.innerHTML += oUl.innerHTML+oUl.innerHTML;
           oUl.style.width = aLi[0].offsetWidth*aLi.length+'px';
           function Slider() {
               if (oUl.offsetLeft<-oUl.offsetWidth/2) {
                   oUl.style.left = 0;
               }else if(oUl.offsetLeft>0){
                   oUl.style.left =-oUl.offsetWidth/2+'px';
               }
               oUl.style.left = oUl.offsetLeft-iSpeed+'px';//正负为方向
           }
           timer =setInterval(Slider,30);
           aA[0].onclick = function(){
               iSpeed = 1; //控制速度的正负
           }
           aA[1].onclick = function(){
               iSpeed = -1;
           }
           oDiv.onmouseover = function(){
               clearInterval(timer);
           }
           oDiv.onmouseout = function(){
               timer =setInterval(Slider,30);
           }
       };
   </script>
</body>
</html>

效果:
image.png

下一篇 / vue项目如何刷新当前页面?

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)