从左到右绘制复选标记CSS动画

wor*_*ith 12 html javascript css jquery animation

我正试图从左边向下绘制一个复选标记,上面是CSS动画.我把左边的部分放下了,但是回去似乎没有用.它从右到下.有谁知道我怎么能让这个更顺畅,实际上看起来像一张支票?

setTimeout(function() {
  $('#kick').addClass('draw2');
}, 500);
Run Code Online (Sandbox Code Playgroud)
#kick {
  height: 150px;
  width: 20px;
  background: green;
  -webkit-transform: rotate(45deg);
  position: relative;
  top: -24px;
  left: 273px;
}
#stem {
  height: 60px;
  width: 20px;
  background: green;
  -webkit-transform: rotate(-45deg);
  position: relative;
  top: 100px;
  left: 200px;
}
@-webkit-keyframes draw1 {
  from {
    height: 0;
  }
  to {
    height: 60px
  }
}
@-webkit-keyframes draw2 {
  from {
    height: 0;
  }
  to {
    height: 150px;
  }
}
.draw1 {
  -webkit-animation-name: draw1;
  -webkit-animation-duration: 0.5s;
}
.draw2 {
  -webkit-animation-name: draw2;
  -webkit-animation-duration: 0.5s;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="checkmark">
    <div class="draw1" id="stem"></div>
    <div id="kick"></div>
</span>
Run Code Online (Sandbox Code Playgroud)

我非常感谢你帮助我制作这个动画!另外,既然我正在使用jQuery,如果可以在jQuery/JavaScript中更有效地完成,那也没关系.

Wea*_*.py 34

嗯,这是我使用<canvas>和JavaScript的方法.

小提琴演示-----> 方角 | 圆角

(注意:要更改动画速度增量或减少变量animationSpeed.更低的数字更高的速度)

var start = 100;
var mid = 145;
var end = 250;
var width = 20;
var leftX = start;
var leftY = start;
var rightX = mid - (width / 2.7);
var rightY = mid + (width / 2.7);
var animationSpeed = 20;

var ctx = document.getElementsByTagName('canvas')[0].getContext('2d');
ctx.lineWidth = width;
ctx.strokeStyle = 'rgba(0, 150, 0, 1)';

for (i = start; i < mid; i++) {
  var drawLeft = window.setTimeout(function() {
    ctx.beginPath();
    ctx.moveTo(start, start);
    ctx.lineTo(leftX, leftY);
    ctx.stroke();
    leftX++;
    leftY++;
  }, 1 + (i * animationSpeed) / 3);
}

for (i = mid; i < end; i++) {
  var drawRight = window.setTimeout(function() {
    ctx.beginPath();
    ctx.moveTo(leftX, leftY);
    ctx.lineTo(rightX, rightY);
    ctx.stroke();
    rightX++;
    rightY--;
  }, 1 + (i * animationSpeed) / 3);
}
Run Code Online (Sandbox Code Playgroud)
<canvas height="160"></canvas>
Run Code Online (Sandbox Code Playgroud)


您也可以使用svgCSS和CSS来完成此操作animation.

1.方角:

#check {
  fill: none;
  stroke: green;
  stroke-width: 20;
  stroke-dasharray: 180;
  stroke-dashoffset: 180;
  -webkit-animation: draw 2s infinite ease;
  animation: draw 2s infinite ease;
}
-webkit-@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<svg width="150" height="150">
  <path id="check" d="M10,30 l30,50 l95,-70" />
</svg>
Run Code Online (Sandbox Code Playgroud)


2.圆角:

#check {
  fill: none;
  stroke: green;
  stroke-width: 20;
  stroke-linecap: round;
  stroke-dasharray: 180;
  stroke-dashoffset: 180;
  animation: draw 2s infinite ease;
}
@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<svg width="150" height="150">
  <path id="check" d="M10,50 l25,40 l95,-70" />
</svg>
Run Code Online (Sandbox Code Playgroud)

  • @ Master.Deep你需要将animation-play-state属性设置为'forward'以使其保持在屏幕上:`animation`和`-webkit-animation`应设置为`draw 2s ease forwards' (2认同)

jdn*_*dnz 5

我想要一个看起来像材料完成图标的复选标记,所以我稍微调整了原来的答案。将其发布在这里,以防节省一些人的时间。

.animated-check {
  height: 10em;
  width: 10em;
}

.animated-check path {
  fill: none;
  stroke: #7ac142;
  stroke-width: 4;
  stroke-dasharray: 23;
  stroke-dashoffset: 23;
  animation: draw 1s linear forwards;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<svg class="animated-check" viewBox="0 0 24 24">
  <path d="M4.1 12.7L9 17.6 20.3 6.3" fill="none"/>
</svg>
Run Code Online (Sandbox Code Playgroud)