javascript - jQuery animate 一直在跳

标签 javascript jquery html css

我有一个文本列表,我正试图在屏幕顶部滚动。 我正在使用 jQuery .animate() 来执行此操作。 然而,它只是一直在接近尾声时跳过,并没有向我展示大部分动画。

理想情况下,它会一直循环滚动。

$('document').ready(function() {
  $('#scroller').click(function() {
    $('#scroller *').animate({
      right: "0"
    }, 1, "linear");
    $('#scroller *').animate({
      left: "0"
    }, 1000, "linear");
  });
});
* {
  font-family: Helvetica;
  padding: 0;
  margin: 0;
}
#scroller {
  position: relative;
  width: 100%;
  height: 5%;
  background-color: black;
  color: white;
  white-space: nowrap;
}
#scroller * {
  position: relative;
  font-size: 2em;
  text-decoration: none;
  display: inline;
  left: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="scroller">
  <li>This is the first list item with text</li>
  <li>&nbsp;-&nbsp;</li>
  <li>This is list item number 2</li>
  <li>&nbsp;-&nbsp;</li>
  <li>List item 3</li>
  <li>&nbsp;-&nbsp;</li>
</ul>

最佳答案

我认为这就是您要找的东西? https://jsfiddle.net/hysgvjnk/5/

HTML:

<ul id="scroller">
  <li>This is the first list item with text</li>
  <li>&nbsp;-&nbsp;</li>
  <li>This is list item number 2</li>
  <li>&nbsp;-&nbsp;</li>
  <li>List item 3</li>
  <li>&nbsp;-&nbsp;</li>
</ul>

CSS:

* {
  font-family: Helvetica;
  padding: 0;
  margin: 0;
}
#scroller {
  position: relative;
  width: 100%;
  height: 5%;
  background-color: black;
  color: white;
  white-space: nowrap;
  overflow: hidden;
}
#scroller * {
  position: relative;
  font-size: 2em;
  text-decoration: none;
  display: inline;
  left: 100%;
}

JS/JQUERY:

$('document').ready(function() {
  $('#scroller').click(function() {
    $('#scroller *').animate({
      left: "1200"
    }, 1, "linear");
   $('#scroller *').animate({
      left: "-1200"
    }, 4000, "linear");
  });
});

编辑: 小解释:

你通过将 tekst 推到右边来开始你的动画(这就是为什么它突然跳进盒子里),它没有完成动画的部分是因为你没有把它从一直显示在屏幕上(如果一个元素的宽度为 2000 像素,而您的动画在左侧 1000 像素,则屏幕上仍会留下 1000 像素)。

关于javascript - jQuery animate 一直在跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39975148/

相关文章:

javascript - 如何在不冲突的情况下进行多个json请求

jquery - 堆叠包含在分离的 div 中的图像

javascript - Skrollr 图像闪烁。 Firefox 预加载问题

javascript - 使用 jQuery 检测动态生成的单选按钮的变化

html - 以 SVG 作为数据 URL 的 CSS 背景在 Chrome 中不起作用

javascript - 抖动对 JQuery 中文本语法的影响

javascript - 我可以在 Backbone.collections 中创建自定义方法吗?

html - 使用 CSS 将文本添加到顶部栏

javascript - 从 http 而不是 https 加载图像时出现 403 错误

javascript - 在 Javascript 中移动并替换带有 2 个 div 标签的单元格