gpt4 book ai didi

css - 返回 CSS 动画起始位置

转载 作者:行者123 更新时间:2023-11-28 10:46:19 24 4
gpt4 key购买 nike

本想结束动画,顺利回到原来的状态,但是有一个跳跃。

必填条件,动画时长可以任意。

我尝试使用 CSS 功能来做到这一点。

setTimeout(function() {
$('div').addClass('stop');
}, 2500);
div {
width: 50px;
height: 300px;
margin: 50px 150px;
background-color: green;
-webkit-animation: wiggle 2s linear infinite;
animation: wiggle 2s linear infinite;
}
.stop {
-webkit-animation: none;
animation: none;
}

@-webkit-keyframes wiggle {
0% {
-webkit-transform: rotate(0);
transform: rotate(0);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes wiggle {
0% {
-webkit-transform: rotate(0);
transform: rotate(0);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>

最佳答案

我创建了一个停止和开始动画,它会旋转回到开头。它通过按钮启动和停止,并且可以轻松启动负载。这个答案的底部有一个完整的演示:)

div 看起来像这样:

<div class="rotate"></div>

无限动画

.play {
-webkit-animation: wiggle 2s linear infinite;
animation: wiggle 2s linear infinite;
}

jQuery
  1. 开始动画:

    $(".start").on("click", function() {            
    //Add infinite rotation animation classes
    $('div').addClass('play rotate');
    });
  2. 停止动画:

    获取元素的当前旋转度。改编自 this answer here通过 @twist

    function getRotationDegrees(obj) {
    var matrix = obj.css("-webkit-transform") ||
    obj.css("-moz-transform") ||
    obj.css("-ms-transform") ||
    obj.css("-o-transform") ||
    obj.css("transform");
    if (matrix !== 'none') {
    var values = matrix.split('(')[1].split(')')[0].split(',');
    var a = values[0];
    var b = values[1];
    var angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
    } else {
    var angle = 0;
    }
    return (angle < 0) ? angle += 360 : angle;
    }

    获取当前 Angular ,然后通过移除类来停止当前动画:

    $(".stop").on("click", function() {

    //Get the current rotation value
    angle1 = getRotationDegrees($('.rotate'));

    //Stop current animation
    $('div').removeClass('play rotate');
  3. 新建停止动画:看起来有点乱。基本上,它是以当前旋转为起点创建一个新动画。然后动画将其带回 0deg .

    //Create stop animation and apply to new class "rotated"
    var animation = '<style type="text/css" title="stopAnimation">.rotated { -webkit-animation: stop 2s forwards; animation: stop 2s forwards; } @-webkit-keyframes stop { 0% { transform: rotate(' + angle1 + 'deg);} 100% { transform: rotate(-0deg); } } @keyframes stop { 0% { transform: rotate(' + angle1 + 'deg);} 100% { transform: rotate(-0deg); } }</style>';

    //Append new styles to the header
    $('head').append(animation);
  4. 重启动画:

    $(".start").on("click", function() {

    //Remove stopping animation class
    $('div').removeClass('rotated');

    //Add infinite rotation animation classes
    $('div').addClass('play rotate');
    });

添加的<style>动画完成后标签被移除:

  //Garbage man - Remove the style tags after the animation is done
// Important - The timeout should match the duration of the stop animation.
setTimeout(
function()
{
$('style[title="stopAnimation"]').remove();

}, 2000);

完整示例

function getRotationDegrees(obj) {
var matrix = obj.css("-webkit-transform") ||
obj.css("-moz-transform") ||
obj.css("-ms-transform") ||
obj.css("-o-transform") ||
obj.css("transform");
if (matrix !== 'none') {
var values = matrix.split('(')[1].split(')')[0].split(',');
var a = values[0];
var b = values[1];
var angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
} else {
var angle = 0;
}
return (angle < 0) ? angle += 360 : angle;
}


$(".stop").on("click", function() {

//Get the current rotation value
angle1 = getRotationDegrees($('.rotate'));

//Stop current animation
$('div').removeClass('play rotate');

//Add class "rotated" for new animation
$('div').addClass('rotated');


//Create stop animation and apply to new class "rotated"
var animation = '<style type="text/css" title="stopAnimation">.rotated { -webkit-animation: stop 2s linear forwards; animation: stop 2s linear forwards; } @-webkit-keyframes stop { 0% { transform: rotate(' + angle1 + 'deg);} 100% { transform: rotate(-0deg); } } @keyframes stop { 0% { transform: rotate(' + angle1 + 'deg);} 100% { transform: rotate(-0deg); } }</style>';


//Append new styles to the header
$('head').append(animation);

//Garbage man - Remove the style tags after the animation is done
// Important - The timeout should match the duration of the stop animation.
setTimeout(
function()
{
$('style[title="stopAnimation"]').remove();

}, 2000);




});

$(".start").on("click", function() {

//Remove stopping animation class
$('div').removeClass('rotated');

//Add infinite rotation animation classes
$('div').addClass('play rotate');
});
div {
width: 50px;
height: 300px;
margin: 50px 150px;
background-color: green;
}
.play {
-webkit-animation: wiggle 2s linear infinite;
animation: wiggle 2s linear infinite;
}
@-webkit-keyframes wiggle {
0% {
-webkit-transform: rotate(0);
transform: rotate(0);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes wiggle {
0% {
-webkit-transform: rotate(0);
transform: rotate(0);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button class="stop">Stop!</button>
<button class="start">Start!</button>

<div class="rotate"></div>

关于css - 返回 CSS 动画起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26932536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com