当前位置:  开发笔记 > 编程语言 > 正文

Javascript暂停和恢复文本上的过渡动画

如何解决《Javascript暂停和恢复文本上的过渡动画》经验,为你挑选了1个好方法。

我尝试在文本上暂停/恢复过渡动画.我已经为这个类似的问题尝试了很多解决方案,但仍然没有成功.

我想在点击暂停按钮时暂停'颜色效果',当我点击恢复按钮时,'颜色效果'将根据剩余持续时间完成整个文本的作业着色.

这是我的代码.

$(document).ready(function(){
  $('#start').on('click', function() {
    $(this).text('Resume');
    $(this).attr('disabled',true);

    $('span').addClass('active');
    $('span').dequeue();
  });
  $("#stop").click(function() {
    $('#start').attr('disabled',false);

    $('#start').clearQueue();
      $("span").stop();

      /*
    * it similiar like below,
    * but still not perfect.
    * when the duration of background-position-x has finished,
    * the transition start again. and yet still not perfect
      */
      /*$('span').animate({
      'background-position-x': '10%',
      'background-position-y': '20%'
    }, 10000, 'linear');*/
  });
})
body {
		background: #ccc;
	}
	span {
	    height: 25px;
	    background-size: 200% 100%;
	    background-image: linear-gradient(to right, orange 50%, white 50%), linear-gradient(to right, transparent 50%, transparent 50%);
	    text-indent: 28px;
	    font-size: 30px;
	    background-size: 200% 100%;
	    background-repeat: no-repeat;
	    background-position: 100% top, 100% top;
	    -webkit-background-clip: text, border-box;
	    background-clip: text, border-box;
	    color: transparent;
	}
	.active {
		background-position: 0% top, 0% top;
	    transition: background-position 10s; 
	}

Lorem ipsum dolor sit amet

注意:我知道有很多'javascript暂停/恢复'问题,我已经尝试用我的案例来实现,但仍然没有成功.



1> repzero..:

使用Jquery切换css animation-play-state

确保为动画属性添加前缀以实现跨浏览器兼容性(-moz,-webkit等)

片段

$(document).ready(function() {
  $('#start').on('click', function() {
    $(this).text('Resume');
    $(this).attr('disabled', true);

    $('span').addClass('active');
    $('span').dequeue();
  });
  $("#stop").click(function() {
    $('#start').attr('disabled', false);

    $('#start').clearQueue();
    $('span').removeClass('active');
    $('span').addClass('stop');
    $("span").stop();

    /*
     * it similiar like below,
     * but still not perfect.
     * when the duration of background-position-x has finished,
     * the transition start again. and yet still not perfect
     */
    /*$('span').animate({
      'background-position-x': '10%',
      'background-position-y': '20%'
    }, 10000, 'linear');*/
  });
})
$("span").on('animationend webkitAnimationEnd oAnimationEnd', function() {
  $("#start").attr('disabled', false);
})
@keyframes anime {
  from {
    background-position: auto
  }
  to {
    background-position: 0% top, 0% top;
  }
}

body {
  background: #ccc;
}

span {
  height: 25px;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, orange 50%, white 50%), linear-gradient(to right, transparent 50%, transparent 50%);
  text-indent: 28px;
  font-size: 30px;
  background-size: 200% 100%;
  background-repeat: no-repeat;
  background-position: 100% top, 100% top;
  -webkit-background-clip: text, border-box;
  background-clip: text, border-box;
  color: transparent;
}

.active {
  animation: anime 10s;
  animation-play-state: running !important;
}

.stop {

  animation: anime 10s;
  animation-play-state: paused;
}

Lorem ipsum dolor sit amet

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有