css3出现之前,我们实现一个对象的一组连续动画需要通过JavaScript或Jquery编写,脚本代码较为复杂;
若需要实现倾斜、旋转之类的动画难度将更高(我还没试过用JavaScript或Jquery如何实现),而且即使能实现估计花的时间代价及维护难度是很大的,很多时候只能依靠画图工具制作此类动画文件;
有时候在想如果不用脚本语言,也不用画图工作制作动画文件,就能在网页上实现倾斜、旋转之类的动画效果多好。
最近挤出一些业余时间学习CSS3,其中就包含很多动画示例,花了点时间学习和整理
今天分享使用html+css3实现skew倾斜、rotate旋转动画,我们先看最终效果图(我这里为了演示效果,就用QQ屏幕截成多张图片,然后制作成gif动画给大家简单展示下,效果不好请大家多多包涵)
图1
具体步骤如下:
1、放置两个div,一个作为容器(图1中绿色背景部分 id="warp"),另一个作为动画元素(图1中黄色背景部分 id="box")
HTML代码:
<div id="warp"> <div id="box">WEB</div></div>
CSS代码(设置容器及动画元素默认样式):
#warp { width: 320px; height: 320px; background: #6FDE82; margin: 20px auto;}#box { height: 40px; width: 40px; background: yellow; position: relative; top: 280px; left: 0; }
注意:#box{position: relative;}是为该元素接下来做动画做准备,因为动画过程中需要改变其位置,故这里使用相对定位
上述代码为基本的html css,大家应该没问题吧
此时效果如下:
2、使用CSS3 @keyframes自定义动画
CSS代码:
@keyframes move { 0% { top: 280px; left: 0; transform: skewX(0deg); width: 40px; height: 40px; } 20% { top: 0; left: 0; transform: skewX(50deg); width: 60px; height: 20px; background: red; } 22% { top: 0; left: 0; transform: skewX(0deg); width: 40px; height: 40px; } 30% { top: 0; left: 0; transform: skewX(0deg); width: 320px; height: 40px; } 40% { top: 0; left: 280px; transform: skewX(0deg); width: 40px; height: 40px; background: green; } 50% { top: 0; left: 280px; transform: skewX(0deg); width: 20px; height: 320px; } 55% { top: 280px; left: 280px; transform: skewX(0deg); width: 40px; height: 40px; background: blue; } 60% { transform: rotate(-90deg); transform-origin: left bottom; } 65% { transform: rotate(-180deg); transform-origin: left top; }}
代码解析:
css3自定义动画需要使用@keyframes规则,具体请看CSS3 @keyframes 规则
-
设置动画执行进度
本示例中定义了 0% 20% 22% 30% 40% 50% 55% 60% 65%几个动画进度,
百分比到底代表什么意思?
举例说明:如果整个动画执行10秒,那么20%就代表当动画执行到2秒时的效果。所以整个自定义动画翻译过来就是在每个执行进度中定义css样式(比如 width,height,color等),这样就形成了连贯的动画效果。执行进度百分比根据实际情况可自行调整。
注意:该执行进度并非一定要设置100%,如本示例中仅设置到65%,这就意味着剩下的35%就由css3自行完成动画回归到原始状态(大家可以试试)
-
元素倾斜(元素变形)、旋转(元素不变形)
本示例中用到倾斜及旋转动画,倾斜使用skew(),参数为倾斜度数;旋转使用rotate(),参数为旋转度数;
倾斜原理示意图如下:
X轴逆时针转为正;Y轴顺时针转为正;
举例:
skew(30deg),在本示例中效果将成这样
skew(-30deg),在本示例中效果将成这样
Y轴的这里就不举例了,根据上述提示大家可自行测试效果
transform-origin,元素倾斜或转动中心点,具体属性值可看CSS3 transform-origin 属性
这里的中心点,指的是动画元素的中心点,元素围绕该点转动或倾斜,该点坐标是不会改变的;个人觉得这里需要头脑中产生一些几何图形以便于理解,或者用一张纸(手机之类的物品)放在桌子上,用手固定该物品的某个点演示一下转动效果,就明白了
3、执行自定义动画
CSS代码:
#box { height: 40px; width: 40px; background: yellow; position: relative; top: 280px; left: 0; animation: move 5s infinite;}
代码解析:
animation: move 5s infinite;
代表该自定义动画完成耗时5秒,并且无限循环执行
animation属性的详细说明请看这里CSS3 animation(动画) 属性
OK,上述步骤及原理大家都清楚了吧。本示例完整代码如下,各位可以直接复制去执行看看效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>HTML5+CSS3 skew倾斜、rotate旋转动画</title> <style type="text/css"> #warp { width: 320px; height: 320px; background: #6FDE82; margin: 20px auto; } #box { height: 40px; width: 40px; background: yellow; position: relative; top: 280px; left: 0; animation: move 5s infinite; } @keyframes move { 0% { top: 280px; left: 0; transform: skewX(0deg); width: 40px; height: 40px; } 20% { top: 0; left: 0; transform: skewX(50deg); width: 60px; height: 20px; background: red; } 22% { top: 0; left: 0; transform: skewX(0deg); width: 40px; height: 40px; } 30% { top: 0; left: 0; transform: skewX(0deg); width: 320px; height: 40px; } 40% { top: 0; left: 280px; transform: skewX(0deg); width: 40px; height: 40px; background: green; } 50% { top: 0; left: 280px; transform: skewX(0deg); width: 20px; height: 320px; } 55% { top: 280px; left: 280px; transform: skewX(0deg); width: 40px; height: 40px; background: blue; } 60% { transform: rotate(-90deg); transform-origin: left bottom; } 65% { transform: rotate(-180deg); transform-origin: left top; } } </style> </head> <body> <div id="warp"> <div id="box">WEB</div> </div> </body></html>
好了,今天分享就到这里,以后还有更多哟,请大家一起来交流下,有兴趣的朋友给我的文章评论下嘛

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment