【问题标题】:Auto Animate Scroll to Right of an Element that Overflows its Container?自动动画滚动到溢出其容器的元素的右侧?
【发布时间】:2018-07-18 22:18:17
【问题描述】:

我有一组元素在包含的 div (.info) 中淡入淡出(一个接一个)。一些元素在出现时会留在容器内,而另一些元素会溢出容器,这不是首选。

当这种情况发生时,我想应用某种水平/自动滚动效果,这样它就可以显示溢出文本元素的开始到结束,同时仍然保持在一行上。有没有办法用 JQuery 来完成这个?

这是我迄今为止所取得的进展的简要说明:

(function() {
  var tab = $(".info .tab");
  var tabIndex = -1;

  function showNextTab() {
    ++tabIndex;
    tab
      .eq(tabIndex % tab.length)
      .fadeIn(2000)
      .delay(2000)
      .fadeOut(2000, showNextTab);
  }
  showNextTab();
})();
.info {
  background: skyblue;
  display: inline-block;
  line-height: 1;
  width: 500px;
  padding: 20px;
  box-sizing: border-box;
}

.info .tab {
  display: none;
}

h2.tab {
  margin: 0;
  padding: 0;
  border: 0;
  white-space: nowrap;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="info">
  <h2 class="tab">This is the first line.</h2>
  <h2 class="tab">This is the second line.</h2>
  <h2 class="tab">This is the third line (which is longer than the first and second line.)</h2>
  <h2 class="tab">This is the fourth line (which is longer than the first, second, and third line.)</h2>
</div>

更新:添加了滚动效果/仍在排除故障

这是一个更新的 sn-p,应用了最近的建议:

var myVar = "";

(function() {
  var tab = $(".info .tab");
  var tabIndex = -1;

  function showNextTab() {
    ++tabIndex;
    myVar = setInterval(myTimer, 1000);
    tab
      .eq(tabIndex % tab.length)
      .fadeIn(2000)
      .delay(2000)
      .fadeOut(2000, showNextTab);
  }
  showNextTab();
})();

function myTimer() {
  var leftPos = $(".info").scrollLeft();
  $(".info").animate({
    scrollLeft: leftPos + 200
  }, 800);
  myStopFunction();
}

function myStopFunction() {
  clearInterval(myVar);
}
.info-wrap {
  background: skyblue;
  display: inline-block;
  line-height: 1;
  width: 500px;
  padding: 20px;
}

.info {
  display: inline-block;
  line-height: 1;
  width: 500px;
  overflow: hidden;
}

.info .tab {
  display: none;
}

h2.tab {
  margin: 0;
  padding: 0;
  border: 0;
  line-height: 1;
  white-space: nowrap;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<duv class="info-wrap">
  <div class="info">
    <h2 class="tab">This is the first line.</h2>
    <h2 class="tab">This is the second line.</h2>
    <h2 class="tab">This is the third line (which is longer than the first and second line.)</h2>
    <h2 class="tab">This is the fourth line (which is longer than the first, second, and third line.)</h2>
  </div>
  </div>

问题1:为什么第四个&lt;h2 class="tab"&gt;元素没有从头开始滚动?它似乎从中间点开始,向右。

问题2:如何修改左滑动画的速度?我试图了解myVar = setInterval(myTimer, 1000); 的目标是什么,以及scrollLeft: leftPos + 200}, 800);

【问题讨论】:

    标签: javascript jquery animation scroll jquery-animate


    【解决方案1】:

    解释:

    问题1 :因为滚动最大宽度是相对的,基于创建多少字符来确定滚动大小的宽度。就在函数初始化时,变量$('.info').scrollLeft() 似乎保存了前一点。因此,我通过添加以下代码重新初始化该代码:

    $(".info").animate({scrollLeft: 0}, 0); //$(.info) point = 0
    

    问题2 : 左边动画可以通过增加leftPos的值来加速。而这个myVar = setInterval(myTimer, 1000);就是确定起点。这意味着该功能将从 1 秒开始。

    反正下面就是这个例子

     
    
    var myVar = "";
    
    (function() {
      var tab = $(".info .tab");
      var tabIndex = -1;
      function showNextTab() {
        ++tabIndex;
        myVar = setInterval(myTimer, 1000); 
        tab
          .eq(tabIndex % tab.length)
          .fadeIn(2000)
          .delay(1000)
          .fadeOut(2000, showNextTab);
        $(".info").animate({scrollLeft: 0}, 0);
      }
      showNextTab();
    })(); 
    function myTimer() {
    var leftPos = $('.info').scrollLeft();
    $(".info").animate({scrollLeft: leftPos + 1500}, 800);
    myStopFunction();
    }
    function myStopFunction() {
    clearInterval(myVar);
    }
    .info {
      background: skyblue;
      display: inline-block;
      line-height: 1;
      width: 500px;
      padding: 20px;
      box-sizing: border-box;
      overflow:scroll;
    }
    
    .info .tab {
      display: none;
    }
    
    h2.tab {
      margin: 0;
      padding: 0;
      border: 0;
      white-space: nowrap;
      text-align: center;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="info">
      <h2 class="tab">This is the first line. testtesttesttesttesttesttttttttttt</h2>
      <h2 class="tab">This is the second line.</h2>
      <h2 class="tab">This is the third line (which is longer than the first and second line.)</h2>
      <h2 class="tab">This is the fourth line (which is longer than the first, second, and third line.)</h2>
    </div>

    【讨论】:

    • 感谢您的回答,尽管我想我应该更具体一些。当出现溢出容器的元素时,我希望该元素自动向右滚动/或选框。
    • 从某种意义上说,它会自行为卷轴设置动画。
    • 啊,我明白了,我编辑了答案。您要查找的脚本类似于 setInterval(function () { var leftPos = $('.info').scrollLeft(); $(".info").animate({scrollLeft: leftPos + 200}, 800);}, 800);没注意很抱歉
    • 感谢您的额外意见!我已将您的建议应用于我原始问题中更新的 sn-p。如果您能看一下,我将不胜感激,因为我还有一些关于滚动/平移右效果的问题。
    • 我编辑了答案部分来回答您关于滚动/平移右效果的几个问题。不客气,nrweb。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2019-12-09
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多