javascript - 悬停时如何制作选取框 - 文本滚动和无限循环,悬停 - 回到起始位置?

标签 javascript jquery css

这是我的 jsfiddle http://jsfiddle.net/9XdV7/ , hover in - 文本滚动和无限循环,out - 回到开始位置时如何?现在它不能无限循环滚动

for (var i = 0; i < $('.list').length; i++) {
    var this_el = $('.list').eq(i);
    var interval = null;

    $(this_el).hover(function() {
      var that = $(this);
      var this_indent = 0;

      interval = setInterval(function(){
        this_indent--;
        if (this_indent == -($(that).find('.text').width())) {
          clearInterval(interval);
          this_indent = 0;

          // how to loop scroll
        }
        $(that).css('text-indent', this_indent);
      },20);

    }, function() {
        clearInterval(interval);
        $(this).css('text-indent', 0);
    });
}

HTML 和 CSS

<div class="list"><div class="text">stringstringstringstring</div></div>
<div class="list"><div class="text">stringstringstring</div></div>
<div class="list"><div class="text">stringstringstringstring</div></div>    

.list {
width: 100px;
overflow: hidden;
white-space:nowrap;

height: 15px;
background-color: red;
margin: 10px;
}
.text {
text-align: left;
background-color: purple;
display: inline;
}

最佳答案

这是您要找的吗? Fiddle

  1. 我使用 mouseenter 和 mouseleave 而不是悬停。 $(elem).on("mouseenter",function() { ... });
  2. 我将属于该元素的标识符存储在它的数据中:$(this).data("interval",interval);
  3. 我加了

    if(this_indent < -150) {
       this_indent = 100;
    }
    

    使效果无限。 -150 是我从开发者工具中得到的一个值。 100 是纯测试。

关于javascript - 悬停时如何制作选取框 - 文本滚动和无限循环,悬停 - 回到起始位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24679112/

相关文章:

Javascript:如果语句包含未解析的函数

javascript - 无需 ng-click 即可更新 $scope 并对数组进行动态排序

javascript - focusin 事件中的alert() 继续弹出

html - 如何防止CSS网格列超过网格容器的最大高度?

css - <a> 元素导致点击移动

javascript - jqGrid清空并重新显示

javascript - Material-UI 文本字段和 Redux 状态尝试设置默认值的奇怪行为?

Jquery:使用window.location获取特定的url路径

javascript - 有没有办法用 jQuery 为 textarea 的不透明度设置动画?

jquery - 使用 CSS 选择没有子列表的列表项