CSS组合动画实现弧形轨迹_html/css_WEB-ITnose
CSS中的animations 以及 transitions都擅长实现从点A到点B的直线动画。无论你如何调整贝塞尔曲线,你都无法通过animation和transitions让元素沿着曲线运动。自定义线性方法可以产生弹性效果,但是X轴,Y轴上的相对运动还是相同的。
当然可以使用JavaScript来模拟动画,但其实有更简单的方式来绕过这个限制。
先看效果:
单纯对一个元素使用animation或者transition的话,那么计算机会自动选择从A点到B点之间最短的距离。那么如何实现我们想要的曲线效果呢?
拆分之后再组合
物理角度上分析,位移是矢量,我们把它分解成X方向和Y方向的运动。假设我们写了这样的动画:
@keyframes straightLine { 50% { transform: translate3D(100px, -100px, 0); }}.dot { animation: straightLine 2.5s infinite linear;}
从(0,0)移动到(-100, -100)的位置,拆分出来就是从(0,0)到(0, -100)与(0,0)到(-100, 0)的组合。
可能你的第一反应会跟我一样,这样写:
.dot { animation: xAxis 2.5s infinite linear, yAxis 2.5s infinite linear;}@keyframes xAxis { 50% { transform: translateX(100px); }}@keyframes yAxis { 50% { transform: translateY(-100px); } }
拆分成x,y轴方向上的运动,然后在animation中组合,然而效果是最后声明的动画有效,因为transform属性不能重复定义。
如何组合?
你可以试试这种方式:同一元素上没办法做到,那就拆分成父元素和子元素的动画。
父元素执行从左到右,子元素执行从下到上的动画。
为了让代码看上去更简洁,我们使用伪元素。
<div class="demo-dot"></div>
接着就是解决两个方向上的同步问题,为了让动画更明显,Demo使用自定义贝塞尔曲线,你也可以使用ease-in, ease-out这类内置的时间性方法。
.demo-dot { animation: xAxis 2.5s infinite cubic-bezier(0.02, 0.01, 0.21, 1);}.demo-dot::after { content: ''; display: block; width: 20px; height: 20px; border-radius: 20px; background-color: #fff; animation: yAxis 2.5s infinite cubic-bezier(0.3, 0.27, 0.07, 1.64);}@-webkit-keyframes yAxis { 50% { animation-timing-function: cubic-bezier(0.02, 0.01, 0.21, 1); transform: translateY(-100px); }}@keyframes yAxis { 50% { animation-timing-function: cubic-bezier(0.02, 0.01, 0.21, 1); transform: translateY(-100px); }}@-webkit-keyframes xAxis { 50% { animation-timing-function: cubic-bezier(0.3, 0.27, 0.07, 1.64); transform: translateX(100px); }}@keyframes xAxis { 50% { animation-timing-function: cubic-bezier(0.3, 0.27, 0.07, 1.64); transform: translateX(100px); }}
一波操作之后,你就能实现让设计师赞不绝口的动画。
这里使用都是keyframe的动画,你也可以使用transitions,通过left,bottom等属性来实现,但是注意这种方式会每次都触发redraw,你可以看这篇文章。 前端性能优化之更平滑的动画
ps:伪元素动画在低端android机子上可能会失效。
结尾
这篇文章 涂鸦码龙 已经翻译过,翻译得非常不错。重新整理一遍,一来是效果确实很不错,二则是为了加深理解,做了些自己的笔记,作为学习查阅之用。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...
