目录:css
一.关键帧(keyframes)css3
今天给你们介绍的是animation动画组件在鸿蒙中的应用.要实现一个 animation 动画,须要使用两个属性@keyframes 和 animation.函数
一.关键帧(keyframes) :定义动画在不一样阶段的状态.所制做的动画就根据keyframes中定义的规则运动. 它的语法结构以下两种:1.百分比来规定改变发生的时间,2.经过关键词 "from" 和 "to",等价于 0% 和 100%. 以下图:post
二.animation属性:flex
(1)animation-name:指定选用的动画的名字(自定义),须要和keyframes中的name一致动画
(2)animation-delay:定义动画是从什么时候开始播放this
(3)animation-iteration-count:定义咱们的动画播放的次数.次数能够是1次或者无限循环.默认值只播放一次url
(4)animation-fill-mode:定义动画模式,即指给定动画播放先后应用元素的样式,具体取值有none ,forwards,backwards,both .这个属性我的感受提及来比较抽象.
(5)animation-timing-function:时间函数,控制动画的执行速度.linear 匀速; ease-in 加速; ease-out 减速; ease-in-out 先加速后减速
(6)animation-play-state 动画运行状态 paused:暂停 ; running 运行
注意:在css3的animation动画中有一个animation-direction的属性,可是我在鸿蒙中应用这个时,刚开始我觉得是官方没有提示,本身写出来运行后也没有想要的效果,因此我猜鸿蒙的animation组件中没有这个属性(如有或者有替代的属性请各位大佬指出.)
视图渲染:
<div class="container"> <div class="oneview"> </div> <div class="twoview"> <image class="img1" src="common/zhou.jpg"> </image> </div> </div>
css属性设置:
.container { width: 100%; height: 1200px; display: flex; flex-direction: column; } .oneview{ width: 300px; height: 300px; border-radius: 150px; background-color: skyblue; /**动画名称**/ animation-name: one1; /**动画时间**/ animation-duration: 6s; /**动画执行次数**/ animation-iteration-count: infinite; /**动画执行速度**/ animation-timing-function: ease-in-out; /**动画模式**/ animation-fill-mode: forwards;/**保持当前**/ } /**动画执行规则**/ @keyframes one1 { from{ transform: translateX(0px); } to{ transform: translateX(300px); } } .twoview{ width: 400px; height: 400px; border-radius: 200px; display: flex; justify-content: center; align-items: center; background-color: lightgrey; } .img1{ width: 300px; height: 300px; border-radius: 150px; /**动画名称**/ animation-name: one2; /**动画时间**/ animation-duration: 8s; /**动画执行次数**/ animation-iteration-count: infinite; /**动画执行速度**/ animation-timing-function: linear; } @keyframes one2{ from{ transform: rotate(0deg) ; } to{ transform: rotate(360deg); } }
效果以下:
-------------------------------------------------------------------------------------分割线--------------------------------------------------------------------------------------
鸿蒙的定时函数和js的动画:
任务:采用js的定时函数作一个定时器,每隔一秒,数字减一,到0时定时任务结束,而后跳转页面,
定时函数setinterval:方法会不停地循环调用函数,以毫秒为单位,直到使用 clearInterval() 明确中止该函数
clearInterval() 函数的参数即 setInterval() 返回的id值,以下图:
注意事项:一在执行计时器的倒计时时,经过this.num传递给视图层时,咱们发现并无执行这个操做.缘由出在this这个关键字上.this所执行的操做跟它所处的位置有关,如今this处于function这个函数中,因此它表明的是这个函数对象,并非当前页面,所以,不会执行this.num--的操做,因此在这里咱们须要合理规避关键字,作一次关键字替换.这样this就能够引用num,执行减减的操做.
二.在定义动画的时候,经过引用页面对象时,鸿蒙给的提示框,咱们能够发现动画规则对应的是一个数组,动画选项对应的是一个键,
代码展现以下:
js业务逻辑层:
import router from '@system.router'; export default { data: { title: 'World', num:10, an1:"", an2:"" }, onInit() { let that=this; let aa=setInterval(function () { that.num--; if(that.num==0){ //settimeout清楚函数,只执行一次 setTimeout(function(){ router.push({ uri:"pages/categories/categories" }) }) clearInterval(aa); } },1000) }, onShow(){ this.loadan1(); this.loadan2(); }, loadan1(){ //动画的规则 let key=[{transform:{translate:'1000px -200px'}}, {transform:{translate:'390px 300px'}} ]; //动画的选项 let options={ duration:8000, easing:"linear", fill:"forwards", iterations:1 //执次数 } //经过id做用于topone this.an1=this.$element("topone").animate(key,options); //播放动画 this.an1.play(); }, loadan2(){ //动画的规则 let key=[{transform:{translate:'-800px 200px'}}, {transform:{translate:'-100px -200px'}} ]; //动画的选项 let options={ duration:8000, easing:"linear", fill:"forwards", iterations:1 //执次数 } //经过id做用于topone this.an2=this.$element("bottomone").animate(key,options); //播放动画 this.an2.play(); } }
视图层:
<div class="container"> <div class="tv1"> <div class="view1" id="topone"> <text class="txt2">{{"来嘛,吃涩!"}} </text> </div> </div> <div class="tv2"> <div class="yuan"> <text class="txt1"> {{num}} </text> </div> </div> <div class="tv3"> <div class="view2" id="bottomone"> <text class="txt2">{{"欢迎光顾"}} </text> </div> </div> </div>
css属性设置:
.container { width: 100%; height: 1500px; display: flex; justify-content: center; align-items: center; flex-direction: column; background-color: skyblue; } .tv1{ width: 100%; height: 20%; /**background-color: orange;**/ display: flex; justify-content: center; align-items: center; } .tv2{ width: 100%; height: 53%; /**background-color: skyblue;**/ display: flex; justify-content: center; align-items: center; } .tv3{ width: 100%; height: 27%; /**background-color: red;**/ display: flex; justify-content: center; align-items: center; } .view1{ position: absolute; top:0px; left: -250px; width: 400px; height: 200px; border: 1p solid lightgrey; background-color: black; opacity: 0.1; border-radius: 45px; display: flex; justify-content: center; align-items: center; } .view2{ position: absolute; bottom:200px; left: 250px; width: 400px; height: 200px; border: 1p solid lightgrey; background-color: black; opacity: 0.1; border-radius: 45px; display: flex; justify-content: center; align-items: center; } .txt2{ font-size: 60px; font-weight: bold; font-family: sans-serif; color: white; } .yuan{ width: 360px; height: 360px; border-radius: 180px; background-color: black; opacity: 0.1; display: flex; justify-content: center; align-items: center; } .txt1{ font-family: sans-serif; font-weight: bold; font-size: 300px; color: white; }
效果展现:
做者:noutsider
想了解更多内容,请访问: 51CTO和华为官方战略合做共建的鸿蒙技术社区https://harmonyos.51cto.com