2016-11-11 145 views
2

我一直在尝试使用jquery动画来执行正在运行的文本。但我似乎无法让它无限循环地运行。它总是只运行一次..如何使用jquery设置无限循环的动画?

/* js: */ 
 

 
$(document).ready(function(){ 
 
    function scroll() { 
 
    $('.scroll').animate({ 
 
     right: $(document).width() 
 
    }, 8000, scroll); 
 
    } 
 
    scroll(); 
 
});
/* css: */ 
 

 
.scroll { 
 
    position: absolute; 
 
    right: -200px; 
 
    width: 200px; 
 
}
<!-- html: --> 
 

 
<div class="scroll">This text be scrollin'!</div>

此为演示: https://jsfiddle.net/y9hvr9fa/1/

你们是否知道如何解决它?

+0

我想尝试使用jquery动画来做选取框 – Coolguy

回答

0

CSS备选:

另外,您可以在此CodePen使用CSS转换,如: https://codepen.io/jamesbarnett/pen/kfmKa

更先进:

$(document).ready(function(){ 
 
    var scroller = $('#scroller'); // scroller $(Element) 
 
    var scrollerWidth = scroller.width(); // get its width 
 
    var scrollerXPos = window.innerWidth; // init position from window width 
 
    var speed = 1.5; 
 
    scroller.css('left', scrollerXPos); // set initial position 
 
    
 
    function moveLeft() { 
 
    \t if(scrollerXPos <= 0 - scrollerWidth) scrollerXPos = window.innerWidth; 
 
    \t scrollerXPos -= speed; 
 
     scroller.css('left', scrollerXPos); 
 
     window.requestAnimationFrame(moveLeft); 
 
    } 
 
\t \t window.requestAnimationFrame(moveLeft); 
 
});
.scroll { 
 
    display: block; 
 
    position: absolute; 
 
    overflow: visible; 
 
    white-space: nowrap; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="scroller" class="scroll">This text be scrollin'!</div>

肮脏的解决方案(我原来的答复):

文本运行到左边没有人停下:

在这个例子中,这将是速战速决。在这里你会告诉文本总是从那个位置开始。 (时间已经跑起来之后 - 这意味着不一定只是当它已经离开了屏幕)

$(document).ready(function(){ 
    function scroll() { 
     $('.scroll').css('right', '-200px').animate({ 
     right: $(document).width() 
     }, 8000, scroll); 
    } 
    scroll(); 
}); 
+0

它看起来好像它在几秒钟内消失了,只剩下它重新出现在右边......如何使它在左侧消失,它会立即出现再次合适吗? – Coolguy

+0

是的,那是真的,这就是为什么我把它称为“快速修复”。你建立的功能只让文本向左运行8秒,然后停止。你必须考虑如何重新运行这条路径 - 参见我的编辑。我使用requestAnimationFrame(查找它)来执行动画并控制确切的位置。然后,您可以检查文本的位置并重新定位。 – lynx

2

所以这是我做过什么:

  1. 预先计算$(document).width()仿佛出现水平滚动时,宽度将在下次迭代

  2. 更改取消已设置scrollwidth,使宽度仅为只要内容 - 你将不得不给white-space:nowrap保持TEX t在一行中。

  3. animate使用使用$('.scroll').outerWidth()

观看演示低于scroll文本的宽度和update fiddle here

$(document).ready(function() { 
 
    
 
    // initialize 
 
    var $width = $(document).width(); 
 
    var $scrollWidth = $('.scroll').outerWidth(); 
 
    $('.scroll').css({'right': -$scrollWidth + 'px'}); 
 
    
 
    // animate 
 
    function scroll() { 
 
    $('.scroll').animate({ 
 
     right: $width 
 
    }, 8000, 'linear', function() { 
 
     $('.scroll').css({'right': -$scrollWidth + 'px'}); 
 
     scroll(); 
 
    }); 
 
    } 
 
    scroll(); 
 
});
body { 
 
    overflow: hidden; 
 
} 
 
.scroll { 
 
    position: absolute; 
 
    white-space: nowrap; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="scroll">This text be scrollin'!</div>

让我知道这是你的意见,谢谢!

+0

@Coolguy让我知道你对此的反馈,谢谢! – kukkuz

+0

不错,你还可以添加线性过渡类型(我会说'...},8000,'linear',function(){...'),所以它具有更多的字符文本 – lynx

+0

@lynx感谢您指出 - 现在看起来好多了:) :) – kukkuz