javascript – 如何在按钮单击时进行一次选取框循环?

前端之家收集整理的这篇文章主要介绍了javascript – 如何在按钮单击时进行一次选取框循环?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要实现的是当我点击按钮时,选取框会播放一次.我的问题是如果我将循环设置为1然后它只播放一次然后什么也不做.我的另一个问题是,如果我放开鼠标左键,它会在当前代码中途停止.或者它停在原处.是否有任何方法可以在按下按钮时播放一次,然后在再次按下按钮时再次播放并允许它完全完成循环.这是代码,我愿意使用 java脚本而不是html.这是我目前的代码
  1. <marquee behavior="scroll" direction="up" scrollamount="30" id="marquee" height="40">
  2. <p>+1</p>
  3. </marquee>
  4.  
  5. <input type="button" id="gather" class="build" Value="play" onmousedown="document.getElementById('marquee').start()." onmouseup="document.getElementById('marquee').stop()" onload="document.getElementById('marquee').stop()">

解决方法

您可以使用CSS keyframes和JQuery(或Javascript)来实现这一目标.

在下面的示例中,我使用CSS规则来实现动画效果,并应用它使用JQuery从te DOM添加/删除span元素.

代码示例:

  1. var marquee = '<span class="anim">+1</span>';
  2.  
  3. $('.btn').click(function(){
  4. $('.anim').remove(); //remove the node if its there
  5. $('.marquee').append(marquee); //append the node
  6. });
  1. .marquee{
  2. width: 20px;
  3. height: 20px;
  4. overflow:hidden;
  5. }
  6.  
  7. .marquee > span {
  8. position:relative;
  9. top:-100%;
  10. left:0;
  11. }
  12.  
  13. .anim{
  14. animation-name: example;
  15. animation-duration: 2s;
  16. }
  17.  
  18.  
  19. @keyframes example {
  20. 0%,100% {
  21. opacity:0;
  22. top:100%;
  23. }
  24. 50% {
  25. opacity:1;
  26. top:0;
  27. }
  28. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div class="marquee"></div>
  3. <button class="btn">Click here!</button>

猜你在找的JavaScript相关文章