最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序實現自定義動畫彈框/提示框的方法實例

 更新時間:2020年11月06日 10:20:06   作者:itclanCoder  
這篇文章主要給大家介紹了關于微信小程序實現自定義動畫彈框/提示框的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

在小程序中,用戶與界面進行交互時,有一些用戶反饋提示,例如:觸發(fā)某個按鈕,從底部彈出框,從頂部彈出等

如今,有一些現成的 UI 庫,雖然已經實現了的,但若只是為了實現一個底部彈出框或者自定義提示框,不引用第三方 UI 庫

怎么手動原生方式去實現呢,最主要的是怎么去實現動畫

css3 實現動畫

如下是wxml代碼

<view>
 <view class="click-btn" catchtap="onBottomBox">彈出底部彈出框</view>
 <view class="click-btn" bindtap="onTopBox">彈出頂部提示框</view>
 <view wx:if="{{isBottom}}" class="bottom-box">
 <div class="mask" bindtap="onHideBox"></div>
 <div class="pop">底部彈出內容</div>
 </view>
 <div wx:if="{{isTop}}" class="top-box">通知內容</div>
</view>

如下是wxss代碼

/* pages/customalertbox/customalertbox.wxss */
.click-btn {
 width: 120px;
 height: 40px;
 line-height: 40px;
 text-align: center;
 margin: 20px auto;
 border: 1px solid #ccc;
 border-radius: 5px;
}

.top-box {
 width: 100%;
 height: 30px;
 background: #f56c6c;
 border-radius: 0 0 8px 8px;
 color: #fff;
 text-align: center;
 line-height: 30px;
 font-size: 28rpx;
 position: absolute;
 top: 0px;
 left: 0;
 animation-duration: 0.5s;
 animation-name: slidetop;
}

.mask {
 width: 100%;
 height: 100%;
 position: fixed;
 top: 0;
 left: 0;
 background: rgba(0, 0, 0, 0.5);
}

.pop {
 position: absolute;
 width: 100%;
 height: 180px;
 background: #42b983;
 border-radius: 8px 8px 0 0;
 position: absolute;
 bottom: 0px;
 animation-duration: 0.5s;
 animation-name: slidein;
}

@keyframes slidein {
 from {
 transform: translateY(70%);
 }
 to {
 transform: translateY(0);
 }
}

@keyframes slidetop {
 from {
 transform: translateY(-30px);
 }
 to {
 transform: translateY(0px);
 }
}

如下是邏輯代碼

// pages/customalertbox/customalertbox.js
Page({
 /**
 * 頁面的初始數據
 */
 data: {
 isBottom: false,
 isTop: false,
 },

 /**
 * 生命周期函數--監(jiān)聽頁面加載
 */
 onLoad: function(options) {},

 onBottomBox() {
 this.setData({
 isBottom: true,
 });
 },

 onHideBox() {
 this.setData({
 isBottom: false,
 });
 },

 onTopBox() {
 this.setData({
 isTop: true,
 });

 setTimeout(() => {
 this.setData({
 isTop: false,
 });
 }, 2000);
 },
});

在小程序中實現動畫,如上實現的動畫,是通過css3中的@keyframes實現的,如下所示

.pop {
 /* ... */
 animation-duration: 0.5s;
 animation-name: slidein; // 動畫的名稱
}

@keyframes slidein {
 // 定義動畫的名稱
 from {
 transform: translateY(70%); // 平移,垂直方向上
 }
 to {
 transform: translateY(0);
 }
}

.top-box {
 /* ... */
 animation-duration: 0.5s;
 animation-name: slidetop;
}

@keyframes slidetop {
 from {
 transform: translateY(-30px);
 }
 to {
 transform: translateY(0px);
 }
}

通過 css3 中的@keyframes以及變換transform,垂直方向上平移,實現動畫

示例效果如下所示

以上是通過 css3 的動畫animation結合@keyframes動畫幀實現的,那么在小程序當中,也可以通過官方的動畫API實現的

小程序動畫 API-實現動畫

創(chuàng)建一個動畫實例 animation,調用實例的方法來描述動畫。最后通過動畫實例的 export 方法導出動畫數據傳遞給組件的 animation 屬性

示例效果如下所示

如下是實例代碼

<view>
 <view class="click-btn" bindtap="onBottomBox">彈出底部彈出框</view>
 <view class="click-btn" bindtap="onTopBox">彈出頂部提示框</view>
 <view
 wx:if="{{isBottom}}"
 style="position: absolute;width: 100%;height: 100%;bottom: 0px;"
 >
 <div class="mask" bindtap="onHideBox"></div>
 <div class="pop" animation="{{animationData}}">底部彈出內容</div>
 </view>
 <div wx:if="{{isTop}}" class="top-box">通知內容</div>
</view>

主要是給想要添加動畫的元素添加了一個animation屬性,現在的動畫是通過js去控制,而非css

如下代碼所示

// pages/customalertbox/customalertbox.js
Page({
 /**
 * 頁面的初始數據
 */
 data: {
 isBottom: false,
 isTop: false,
 animationData: {}, // 定義動畫對象
 },

 /**
 * 生命周期函數--監(jiān)聽頁面加載
 */
 onLoad: function(options) {},

 onBottomBox() {
 // 創(chuàng)建動畫
 var animation = wx.createAnimation({
  duration: 2000,
  timingFunction: 'ease',
 });

 this.animation = animation;
 // 先在y軸偏移180,然后用step()完成一個動畫
 animation.translateY(180).step();
 this.setData({
  animationData: animation.export(),
  isBottom: true,
 });

 // 設置setTimeout來改變y軸偏移量,實現有感覺的滑動,回到初始位置
 setTimeout(() => {
  animation.translateY(0).step();
  this.setData({
  animationData: animation.export(),
  });
 }, 200);
 },

 // 點擊遮罩層隱藏彈框
 onHideBox() {
 var animation = wx.createAnimation({
  duration: 2000,
  timingFunction: 'ease',
 });
 this.animation = animation;
 // 先在y軸偏移180,然后用step()完成一個動畫
 animation.translateY(180).step();
 this.setData({
  animationData: animation.export(),
 });
 setTimeout(() => {
  animation.translateY(0).step();
  this.setData({
  animationData: animation.export(),
  isBottom: false,
  });
 }, 200);
 },

 onTopBox() {
 this.setData({
  isTop: true,
 });

 setTimeout(() => {
  this.setData({
  isTop: false,
  });
 }, 2000);
 },
});

以上就是通過微信小程序中動畫API實現的完成的動畫,代碼要比 css3 要多一些,可以實現更加復雜的動畫效果

注意

如果是底部彈出框,拖動里面時,若遮罩層底部會跟著滾動,具體解決辦法也可以在外層添加catchtouchmove="true"即可解決

<view>
 <view class="click-btn" bindtap="onBottomBox">彈出底部彈出框</view>
 <view
 catchtouchmove="true"
 wx:if="{{isBottom}}"
 style="position: absolute;width: 100%;height: 100%;bottom: 0px;"
 >
 <div class="mask" bindtap="onHideBox"></div>
 <div class="pop" animation="{{animationData}}">底部彈出內容</div>
 </view>
 <div wx:if="{{isTop}}" class="top-box">通知內容</div>
</view>

結語

在小程序當中實現動畫可以用css3的animation結合@keyframes實現,同樣也可以通過小程序動畫的api去實現

到此這篇關于微信小程序實現自定義動畫彈框/提示框的文章就介紹到這了,更多相關微信小程序自定義動畫彈框/提示框內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文檔

小程序動畫 API

相關文章

最新評論

同仁县| 璧山县| 凤阳县| 册亨县| 和田县| 沙河市| 阜康市| 泾川县| 麦盖提县| 营口市| 北宁市| 泾源县| 奇台县| 文水县| 石景山区| 酉阳| 巴楚县| 会泽县| 古蔺县| 石首市| 阜南县| 萨嘎县| 通州市| 务川| 蒙阴县| 和田县| 津市市| 司法| 沐川县| 读书| 潢川县| 太保市| 富裕县| 平武县| 商南县| 五原县| 南阳市| 正安县| 普陀区| 会泽县| 通海县|