微信小程序自定義sheet-action,實(shí)現(xiàn)底部彈出動(dòng)畫

效果

小程序自定義sheet-action.gif

小程序自定義底部彈出modal框組件序矩,仿照小程序sheet-action的效果,封裝成組件直接在其他業(yè)務(wù)頁面公用跋破。

  1. 底部彈出動(dòng)畫使用的是小程序的animation簸淀,彈出動(dòng)畫可以根據(jù)自行需求去替換。
  2. modal頂部有一條短的橫線毒返,向下滑動(dòng)可以觸發(fā)隱藏modal
  3. 點(diǎn)擊陰影部分可以觸發(fā)隱藏modal

使用方法

  1. 在業(yè)務(wù)頁面引入組件
// pages/index/index.json
{
  "usingComponents": {
    "product-cart": "../../components/product-cart/index"
  }
}
// pages/index/index.js
Page({
  data: {
    show: false // true:顯示  false:隱藏
  },
showModal(){
  this.setData({
       show:true
    })
  },
)}
  1. show參數(shù)是boolean值
// pages/index/wswl
<view>
  <button bindtap="showModal">點(diǎn)擊</button>
 <product-cart show="{{show}}"></product-cart>
</view>

組件源碼

// pages/index/components/buy/index.js
let pageY = 0;
Component({
  options: {
    styleIsolation: 'isolated'
  },
  /**
   * 組件的屬性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 組件的初始數(shù)據(jù)
   */
  data: {
    list: [{
        name: '快遞',
        selected: 1,
      },
      {
        name: '自提',
        selected: 0
      }
    ],
    animate: {},
    hideModal: false, //模態(tài)框的狀態(tài)  false-隱藏  true-顯示
  },
  /**
   * 數(shù)據(jù)監(jiān)聽
   */
  observers: {
    'show': function(val) {
      if (val) {
        this.showModal()
      } else {
        this.hideModal()
      }
    }
  },
  /**
   * 組件的方法列表
   */
  methods: {
    // 顯示遮罩層
    showModal() {
      this.setData({
        hideModal: true
      })
      const animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })
      // 先顯示背景再執(zhí)行動(dòng)畫租幕,translateY(0)偏移量為0代表顯示默認(rèn)高度
      setTimeout(() => {
        animation.translateY(0).step()
        this.setData({
          animate: animation.export()
        })
      }, 50)
    },
    // 隱藏遮罩層
    hideModal() {
      const animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })
      // 設(shè)置為100vh可以確保滾動(dòng)到底部,可以按照自己的內(nèi)容高度設(shè)置拧簸,能夠滑到底部即可
      animation.translateY('100vh').step()
      this.setData({
        animate: animation.export(),
      })
      // 先執(zhí)行動(dòng)畫劲绪,再隱藏組件
      setTimeout(() => {
        this.setData({
          hideModal: false
        })
      }, 300)
    },
    // 移動(dòng)
    touchMove(e) {
      const clientY = e.changedTouches[0].clientY
      if (clientY - pageY > 0 && clientY - pageY > 50) {
        this.hideModal()
      }
    },
    // 觸摸開始
    touchStart(e) {
      pageY = e.changedTouches[0].clientY;
    },
    // 選擇類型
    changeItem(e) {
      const {
        index
      } = e.currentTarget.dataset
      this.data.list.forEach((e, i) => {
        if (i == index) {
          e.selected = 1
        } else {
          e.selected = 0
        }
      })
      this.setData({
        list: this.data.list
      })
    },
    // 確認(rèn)
    confirm() {
      this.hideModal()
    },
  }
})
<!--pages/index/components/buy/index.wxml-->
<view class="box" hidden="{{!hideModal}}">
  <view class="empty-box" bindtap="hideModal" id="empty-box"></view>
  <scroll-view scroll-y style="max-height:80vh;">
    <view class="content" style="transform:translateY({{translateY}}px);" animation="{{animate}}">
      <!-- boll -->
      <view class="header" bindtouchstart="touchStart" bindtouchmove="touchMove">
        <view></view>
      </view>

      <!-- 快遞類型 -->
      <view>
        <view class="item" wx:for="{{list}}" wx:key="index" bindtap="changeItem" data-index="{{index}}">
          <view class="item-name">{{item.name}}</view>
          <view>
            <view class="item-no-selected" wx:if="{{item.selected==0}}"></view>
            <image class="item-selected" wx:if="{{item.selected==1}}" src="/assets/images/choose.png"></image>
          </view>
        </view>
      </view>

      <!-- 按鈕 -->
      <view class="button" bindtap="confirm">
        <view>確認(rèn)</view>
      </view>

    </view>
  </scroll-view>
</view>
/* pages/index/components/buy/index.wxss */

.flex {
  display: flex;
  align-items: center;
}

.box {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

.empty-box {
  flex: 1;
  background-color: transparent;
}

/* 內(nèi)容視圖 */

.content {
  width: 100vw;
  background: rgba(255, 255, 255, 1);
  opacity: 1;
  border-radius: 20px 20px 0px 0px;
  z-index: 1001;
}

/* 頭部 */

.header {
  position: relative;
  height: 80rpx;
  width: 100vw;
}

.header > view {
  position: absolute;
  top: 26rpx;
  left: calc(50vw - 30rpx);
  width: 60rpx;
  height: 10rpx;
  background: rgba(161, 166, 175, 1);
  opacity: 0.6;
  border-radius: 6rpx;
}

/* 快遞 */

.item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: calc(100vw - 80rpx);
  padding: 0 40rpx;
  height: 100rpx;
  background: rgba(255, 255, 255, 1);
  opacity: 1;
}

.item-no-selected {
  width: 36rpx;
  height: 36rpx;
  background: rgba(255, 255, 255, 1);
  border: 2rpx solid rgba(112, 112, 112, 1);
  border-radius: 50%;
  opacity: 0.5;
}

.item-selected {
  width: 40rpx;
  height: 40rpx;
}

/* 按鈕 */

.button {
  width: 100vw;
  padding: 80rpx 40rpx 20rpx 40rpx;
}

.button >view {
  width: calc(100% - 80rpx);
  height: 98rpx;
  border-radius: 50rpx;
  line-height: 98rpx;
  text-align: center;
  font-size: 32rpx;
  font-family: PingFang SC;
  font-weight: bold;
  color: rgba(255, 255, 255, 1);
  background: rgba(237, 58, 74, 1);
  opacity: 1;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市盆赤,隨后出現(xiàn)的幾起案子贾富,更是在濱河造成了極大的恐慌,老刑警劉巖牺六,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颤枪,死亡現(xiàn)場離奇詭異,居然都是意外死亡淑际,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門盗胀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人票灰,你說我怎么就攤上這事。” “怎么了屑迂?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵浸策,是天一觀的道長屈糊。 經(jīng)常有香客問我,道長逻锐,這世上最難降的妖魔是什么夫晌? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮昧诱,結(jié)果婚禮上晓淀,老公的妹妹穿的比我還像新娘盏档。我一直安慰自己凶掰,他們只是感情好蜈亩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著畅涂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪午衰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天臊岸,我揣著相機(jī)與錄音尊流,去河邊找鬼。 笑死奠旺,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的响疚。 我是一名探鬼主播瞪醋,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼践盼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起咕幻,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎锣吼,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玄叠,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拓提,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了寺惫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡西雀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出必尼,到底是詐尸還是另有隱情,我是刑警寧澤判莉,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站帮哈,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏娘侍。R本人自食惡果不足惜泳炉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望花鹅。 院中可真熱鬧,春花似錦、人聲如沸箩帚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽桅打。三九已至,卻和暖如春油额,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潦嘶。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國打工掂僵, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人锰蓬。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像芹扭,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子舱卡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容