SoFunction
Updated on 2025-04-10

JS implements horizontal marionette effect code

First we need a framework for html code as follows:

<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  style="margin: 0px;left:0; top:0;margin-bottom:0px;width:100%;height:47px;position:absolute;">

    </ul>
  </div>

Our goal is to achieve horizontal scrolling of content in ul. The content in ul should be dynamic. So ajax should be sent to obtain and splice content.

$.ajax({
      type:"post",
      dataType:"json",
      url:"${contextPath}/indexPage/getSyNoticeInfo",
      success:function(result){
        var totalStr = "";
        if(>0){
          for(var i=0 ; i<;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();
      }
    });

When splicing li, there is a class as sstzNoticeStyle. Its style is as follows:

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

ajax calls the syRunHorseLight function, the function is as follows:

function syRunHorseLight() {
    if (syTimer != null) {
      clearInterval(syTimer);
    }
    var oUl = ("syNoticeUlNew");
    if(oUl != null){
       += ;
       += ;
       += ;
      var lis = ('li');
      var lisTotalWidth = 0;
      var resetWidth = 0;
      for (var i = 0; i < ; i++) {
        lisTotalWidth += lis[i].offsetWidth;
      }
      for (var i = 1; i <=  / 4; i++) {
        resetWidth += lis[i].offsetWidth;
      }
       = lisTotalWidth + 'px';
      var left = 0;
      syTimer = setInterval(function() {
        left -= 1;
        if (left <= -resetWidth) {
          left = 0;
        }
         = left + 'px';
      }, 20)
      $("#syNoticeUlNew").hover(function() {
        clearInterval(syTimer);
      }, function() {
        clearInterval(syTimer);
        syTimer = setInterval(function() {
          left -= 1;
          if (left <= -resetWidth) {
            left = 0;
          }
           = left + 'px';
        }, 20);
      })
    }
  }

The marquee effect is realized.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.