欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)橫向跑馬燈效果代碼

 更新時(shí)間:2020年04月20日 08:25:32   作者:經(jīng)典雞翅  
這篇文章主要介紹了JS實(shí)現(xiàn)橫向跑馬燈效果代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

首先我們需要一個(gè)html代碼的框架如下:

<div style="position: absolute; top: 0px; left: 168px; width: 100%; margin-left: auto; margin-right: auto; height: 47px; border: 0px solid red; overflow: hidden;">
    <ul id="syNoticeUlNew" style="margin: 0px;left:0; top:0;margin-bottom:0px;width:100%;height:47px;position:absolute;">

    </ul>
  </div>

我們的目的是實(shí)現(xiàn)ul中的內(nèi)容進(jìn)行橫向的一點(diǎn)一點(diǎn)滾動(dòng)。ul中的內(nèi)容應(yīng)該是動(dòng)態(tài)的。于是應(yīng)該發(fā)送ajax來(lái)進(jìn)行內(nèi)容的獲取拼接。

$.ajax({
      type:"post",
      dataType:"json",
      url:"${contextPath}/indexPage/getSyNoticeInfo",
      success:function(result){
        var totalStr = "";
        if(result.length>0){
          for(var i=0 ; i<result.length;i++){
            str = "<li style=\"list-style: none;display:inline-block;float:left;height:47px;padding-right:50px;line-height:47px;\">"+
             "<a style=\"color:white;\" class=\"sstzNoticeStyle\">"+result[i].peopleName+"</a>"+
             "</li>";
            totalStr +=str;
          }
        }
        $("#syNoticeUlNew").empty();
        $('#syNoticeUlNew').html(totalStr);
        syRunHorseLight();
      }
    });

拼接li時(shí)候有個(gè)class為sstzNoticeStyle。其樣式如下:

.sstzNoticeStyle{
  color:white; font-size:16px;text-decoration:none;
}
.sstzNoticeStyle:hover{
  color:white; font-size:16px;text-decoration:none;
}

ajax調(diào)用syRunHorseLight函數(shù),函數(shù)如下:

function syRunHorseLight() {
    if (syTimer != null) {
      clearInterval(syTimer);
    }
    var oUl = document.getElementById("syNoticeUlNew");
    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;
      syTimer = setInterval(function() {
        left -= 1;
        if (left <= -resetWidth) {
          left = 0;
        }
        oUl.style.left = left + 'px';
      }, 20)
      $("#syNoticeUlNew").hover(function() {
        clearInterval(syTimer);
      }, function() {
        clearInterval(syTimer);
        syTimer = setInterval(function() {
          left -= 1;
          if (left <= -resetWidth) {
            left = 0;
          }
          oUl.style.left = left + 'px';
        }, 20);
      })
    }
  }

跑馬燈效果就此實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論