js实现图片无缝循环跑马灯

前端开发 作者: 2024-08-19 19:50:01
html 代码 css js代码 function mylsRunHorseLight() { if (mylsTimer != null) { clearInterval(mylsTimer); }

html 代码

<div class="myls-out-div" style="overflow: hidden;">

    <ul id="mylspaomadeng"  class="myls-ul"  >

        <li  class="myls-li">

            <img  class="myls-img" src="img/indexImg/myls1.png" />

        </li>

        <li  class="myls-li">

            <img  class="myls-img" src="img/indexImg/myls2.png" />

        </li>

        <li  class="myls-li">

            <img  class="myls-img" src="img/indexImg/myls3.png" />

        </li>

        <li  class="myls-li">

            <img  class="myls-img" src="img/indexImg/myls4.png"  />

        </li>

    </ul>

 </div>


css

.myls-out-div {

    width: 100%;

    height: 212px;

    background-color: #fafafa;

    float: left;
            
    overflow: hidden;

 }
      
 .myls-img {

    height: 100%;

}

.myls-ul{

    float: left;

    height: 100%;

    position: relative;

    margin: 0px;

    padding: 0px;

}

 .myls-li{

    list-style: none;

    display: inline-block;

    float: left;

    padding-right: 15px;

    padding-top: 15px;

    padding-bottom: 15px;

    height: 100%;

}

js代码

function mylsRunHorseLight() {

      if (mylsTimer != null) {

            clearInterval(mylsTimer);

      }

      var oUl = document.getElementById("mylspaomadeng");

      if(oUl != null){

            oUl.innerHTML += oUl.innerHTML;

            oUl.innerHTML += oUl.innerHTML;

            oUl.innerHTML += oUl.innerHTML;

            var lis = oUl.getElementsByTagName('li');

            var lisTotalWidth = 0;

            var resetWidth = 0;

            for (var i = 0; i < lis.length; i++) {

                  lisTotalWidth += lis[i].offsetWidth;

            }

            for (var i = 1; i <= lis.length / 4; i++) {

                  resetWidth += lis[i].offsetWidth;

            }

            oUl.style.width = lisTotalWidth + 'px';

            var left = 0;

            mylsTimer = setInterval(function() {

                  left -= 1;

                  if (left <= -resetWidth) {

                        left = 0;

                  }

                  oUl.style.left = left + 'px';

            },20)

            $("#mylspaomadeng").hover(function() {

                  clearInterval(mylsTimer);

            },function() {

                  clearInterval(mylsTimer);

                  mylsTimer = setInterval(function() {

                        left -= 1;

                        if (left <= -resetWidth) {

                              left = 0;

                        }

                        oUl.style.left = left + 'px';

                  },20);

            })

      }

}


注意事项

总结

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65114.html