【问题标题】:Implementing horizontal scrolling images using JQuery使用 JQuery 实现水平滚动图像
【发布时间】:2020-09-03 10:19:45
【问题描述】:

我想使用 JQuery 实现水平滚动图像。基本上我想重复 animate() 无限次。这是我的代码:

HTML:

<div class="testDiv">
    <h1 class="heading">Test Text</h1>
    <ul class="reasons">
        <li class="item">
           <img>
        </li>
        <li class="item">
          <img>
        </li>
        <li class="item">
           <img>
        </li>  
    </ul>       
</div>

JQuery:*

let itemsLength = $('.item').length;

$(document).ready(loop());

函数循环(){

for (i = 1; i <= itemsLength; i++) {
      $('.reasons').animate({"scrollLeft": "+=150px"},"slow", function(){
                    loop();
                          }/*function*/     
                          });/*animate*/
        }/*for*/
    }/*function loop()*/

CSS:

.testDiv {
    margin-top: 2vw;
    overflow-x: hidden;
    overflow-y: hidden;
    z-index: 40;        
}

.reasons {
    height: 200px;
    background: #ffffff;
    box-sizing: border-box;
    white-space: nowrap;
    overflow-x: hidden;
    overflow-y: hidden;
}

.item {
    display: inline-block;
    height: 100%;
    box-sizing: border-box;
}

我尝试过使用$('.reasons').append($('.item')[0]);。这只是附加列表项但不滚动。我认为 scrollLeft 值需要更改但不知道如何更改。

提前致谢。

【问题讨论】:

    标签: html jquery jquery-animate horizontal-scrolling


    【解决方案1】:

    我不太确定,你想做什么,但也许这个想法会以某种方式帮助你:

      function loop(){
        for (i = 1, j=0; i <= itemsLength; i++, j+=150) 
        {
           $('.reasons').animate({"scrollLeft": j},"slow", function(){
                                                  
           });
        }
        loop();
      }
    

    【讨论】:

    • 感谢您的回复。我需要在无限循环中水平向左滚动图像。您的代码向左滚动,但由于 j 的值再次初始化为 0,因此它将整个 ul 向右滚动,然后再次向左滚动。它来回移动。
    • 任何想法..如何使用 jQuery 实现左滚动图像?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多