自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS - 自定義組件之Switch開關(guān)

系統(tǒng) OpenHarmony
最近在開發(fā)FA項目時使用到switch開關(guān)組件,該switch組件是基于JS擴(kuò)展的類Web開發(fā)范式組件中的基礎(chǔ)組件 。

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

前言

最近在開發(fā)FA項目時使用到switch開關(guān)組件,該switch組件是基于JS擴(kuò)展的類Web開發(fā)范式組件中的基礎(chǔ)組件 。在使用的過程中發(fā)現(xiàn)了一些問題,比如:

1、設(shè)置寬高等樣式屬性不會改變switch組件本身的寬高,而是會修改switch組件的內(nèi)邊距。

2、switch組件存在一定比例的縮放。

使用switch組件在頁面布局時存在上述等問題,但可通過一些技術(shù)手段(transform屬性的scale)或一些替代方案,達(dá)到預(yù)期效果?;诖耍ㄟ^現(xiàn)在已掌握的FA相關(guān)知識實現(xiàn)一個簡單的自定義switch開關(guān)組件。

效果展示

#夏日挑戰(zhàn)賽# HarmonyOS - 自定義組件之switch開關(guān)-開源基礎(chǔ)軟件社區(qū)#夏日挑戰(zhàn)賽# HarmonyOS - 自定義組件之switch開關(guān)-開源基礎(chǔ)軟件社區(qū)

實現(xiàn)原理

基于css關(guān)鍵幀動畫animation和@keyframes,通過對switch的滑塊設(shè)置css屬性position: absolute,并添加相應(yīng)的動畫class,即可達(dá)到switch開關(guān)的過渡效果。給switch設(shè)置固定的寬度和高度,保證switch組件不被縮放,在switch組件的外層包裹div組件,來實現(xiàn)布局的合理性。

css動畫樣式官方指導(dǎo)API:

屬性名稱

數(shù)據(jù)類型

默認(rèn)值

描述

transform

string

-

支持同時設(shè)置平移/旋轉(zhuǎn)/縮放等屬性。

animation6+

string

0s ease 0s 1 normal none running none

格式:duration | timing-function | delay | iteration-count | direction | fill-mode| play-state | name,每個字段不區(qū)分先后,但是 duration / delay 按照出現(xiàn)的先后順序解析。

animation-name

string

-

指定@keyframes的名稱。

animation-fill-mode

string

none

指定動畫開始和結(jié)束的狀態(tài):1、none:在動畫執(zhí)行之前和執(zhí)行之后都不會應(yīng)用任何樣式到目標(biāo)上;2、forwards:在動畫結(jié)束后,目標(biāo)將保留動畫結(jié)束時的狀態(tài)(在最后一個關(guān)鍵幀中定義);3、backwards6+:動畫將在animation-delay期間應(yīng)用第一個關(guān)鍵幀中定義的值。當(dāng)animation-direction為"normal"或"alternate"時應(yīng)用from關(guān)鍵幀中的值,當(dāng)animation-direction為"reverse"或"alternate-reverse"時應(yīng)用to關(guān)鍵幀中的值。4、both6+:動畫將遵循forwards和backwards的規(guī)則,從而在兩個方向上擴(kuò)展動畫屬性。

animation-duration

<time>

0

定義一個動畫周期。支持的單位為[s(秒) | ms(毫秒) ],默認(rèn)單位為ms,格式為:1000ms或1s。(注意:animation-duration 樣式必須設(shè)置,否則時長為 0,則不會播放動畫。)

animation-delay

<time>

0

定義動畫播放的延遲時間。支持的單位為[s(秒) | ms(毫秒) ],默認(rèn)單位為ms,格式為:1000ms或1s。

translateX

<length> | <percent>

-

X軸方向平移動畫屬性。

實現(xiàn)過程

switch組件 hml 部分:

<div class="define-switch">
<!-- switch 滑塊背景包裹節(jié)點 -->
<div class="switch-wrapper {{ isChecked ? 'bg-blue' : 'bg-grey'}}" onclick="handleClick"></div>
<!-- switch 滑塊 -->
<div class="switch-block {{ isChecked ? 'closed' : 'opened'}}"></div>
</div>

switch組件 css 部分:

.define-switch {
position: relative;
padding: 2px;
}
.switch-wrapper {
position: relative;
width: 36px;
height: 20px;
background-color: rgba(0,0,0,0.05);
border-radius: 25px;
}
.switch-block {
position: absolute;
left: 2px;
top: 2px;
width: 16px;
height: 16px;
background-color: #FFFFFF;
border-radius: 8px;
}
.opened {
animation-name: open;
animation-duration: 1s;
animation-fill-mode: forwards;
}
.closed {
animation-name: close;
animation-duration: 1s;
animation-fill-mode: forwards;
}
.bg-blue {
background-color: #0A59F7;
}
.bg-grey {
background-color: rgba(0,0,0,0.05);
}
@keyframes open {
from {
transform: translateX(16px);
}
to {
transform: translateX(0px);
}
}
@keyframes close {
from {
transform: translateX(0px);
}
to {
transform: translateX(16px);
}
}

switch組件 js 部分:

export default {
name: 'DefineSwitch',
data() {
return {
isChecked: false,
}
},
handleClick(evt) {
console.log('事件對象event:' + JSON.stringify(evt) + this.isChecked);
this.isChecked = ! this.isChecked; // 修改isChecked的值
this.$emit('switchChange', {checked: this.isChecked}); // 向父組件傳遞值
}
}

父組件 hml中:

<!-- 引入組件 -->
<element name="mySwitch" src="../mySwitch/mySwitch.hml"></element>
<!-- 組件使用 -->
<div style="width: 40px; height: 24px;">
<mySwitch @switch-change="handleSwitchChange"></mySwitch>
</div>

父組件 js中:

handleSwitchChange(e) {
console.log('子組件傳來的數(shù)據(jù)' + e.detail.checked); // 父組件中獲取到switch的開關(guān)狀態(tài)
}

實現(xiàn)中遇到的問題及解決方案:

問題描述:在實現(xiàn)關(guān)鍵幀動畫時switch滑塊執(zhí)行完最后一幀動畫后會回到原來的位置,導(dǎo)致動畫執(zhí)行效果不理想。

原因分析:動畫屬性animation的animation-fill-mode屬性默認(rèn)為none( 在動畫執(zhí)行之前和之后都不會應(yīng)用任何樣式到目標(biāo)上 )。

解決方案:將animation-fill-mode屬性設(shè)置為 forwards ( 在動畫結(jié)束后,目標(biāo)將保留動畫結(jié)束時的狀態(tài) )。

總結(jié)

此自定義組件是對css關(guān)鍵幀動畫、父子組件通信的回顧。

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-11-01 10:21:36

鴻蒙HarmonyOS應(yīng)用

2022-06-30 14:02:07

鴻蒙開發(fā)消息彈窗組件

2022-07-15 16:45:35

slider滑塊組件鴻蒙

2021-12-24 15:46:23

鴻蒙HarmonyOS應(yīng)用

2022-07-12 16:56:48

自定義組件鴻蒙

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2022-05-20 14:34:20

list組件鴻蒙操作系統(tǒng)

2023-02-20 15:20:43

啟動頁組件鴻蒙

2021-11-24 10:02:53

鴻蒙HarmonyOS應(yīng)用

2022-10-25 15:12:24

自定義組件鴻蒙

2022-10-26 15:54:46

canvas組件鴻蒙

2021-09-15 10:19:15

鴻蒙HarmonyOS應(yīng)用

2022-02-21 15:16:30

HarmonyOS鴻蒙操作系統(tǒng)

2021-12-21 15:22:22

鴻蒙HarmonyOS應(yīng)用

2022-07-06 20:24:08

ArkUI計時組件

2021-12-30 16:10:52

鴻蒙HarmonyOS應(yīng)用

2021-11-22 10:00:33

鴻蒙HarmonyOS應(yīng)用

2021-11-04 09:55:50

鴻蒙HarmonyOS應(yīng)用

2022-02-16 16:09:12

鴻蒙游戲操作系統(tǒng)

2022-05-23 10:53:54

canvas柱狀圖鴻蒙
點贊
收藏

51CTO技術(shù)棧公眾號