怎么用Vue實(shí)現(xiàn)material design的漣漪動效

第一次看到material design lite 的時候只能用驚艷來表達(dá),原來按鈕、復(fù)選框、輸入框還可以這樣玩。
material design lite 官網(wǎng) http://www.getmdl.io/components/index.html 需要翻墻

先上最終效果圖


4.gif

去年的時候我把mdl搬到了vue里,寫了一個簡單的博客。這里的動效全部是在vue渲染出dom之后調(diào)用mdl的方法來實(shí)現(xiàn)的。
比如這段代碼就是首頁異步獲取的文章列表數(shù)據(jù)之后調(diào)用componentHandler.upgradeAllRegistered()

  asyncData: function(resolve, reject) {
      this.loadPost(0,  (tmp)  => {
          store.actions.hideLoading();
          resolve({
              posts: tmp
          })
          this.site.skip = 10;
          this.$nextTick(function() {
              componentHandler.upgradeAllRegistered();
          })
      })
  }
2.gif

上面提到的是一個簡單輕松用法,但是總感覺不優(yōu)雅,你總得去調(diào)用那個黑盒搬的方法。
然而,我一直喜歡折騰,還是自己來實(shí)現(xiàn)吧。

material design里特別吸引我的一個效果就是按鈕點(diǎn)擊之后會有水的漣漪從點(diǎn)擊的地方為中心擴(kuò)散。


1.gif

于是馬上打開chrome開發(fā)者工具去看看它的dom結(jié)構(gòu)(為了顯示效果刪掉button的了一部分class)

<button class="mdl-button">
    <i class="material-icons">add</i>
    <span class="mdl-button__ripple-container">
        <span class="mdl-ripple is-animating" style="width: 160.392px; height: 160.392px; transform: translate(-50%, -50%) translate(30px, 48px);">            
     </span>
   </span>
</button>

<span class="mdl-ripple is-animating" style="width: 160.392px; height: 160.392px; transform: translate(-50%, -50%) translate(30px, 48px);">
看來它就是那個動態(tài)的水波了

實(shí)現(xiàn)原理就是通過動態(tài)改變它的中心位置,然后是從小到大擴(kuò)散的動畫。

先寫出來我們要做的button的dom

  <button  class="__cov-button-ripple">
    BUTTON
    <span class="__cov-ripple"></span>
  </button>

ripple 的css3動畫,我們在button被點(diǎn)擊之后就給<span class="__cov-ripple"></span> class中加上 .animate, 它就會開始擴(kuò)散了

.animate {
  animation: ripple 0.65s linear;
}
@keyframes ripple {
  100% {opacity: 0; transform: scale(2.5);}
}
reppleClick (e) {
      this.repple_button.animate = true
      let button = e.target
      let ripple = button.querySelector('.__cov-ripple')
      if (ripple) {
        //水波得是一個直徑大于button最長邊的正圓
        let d = Math.max(button.offsetHeight, button.offsetWidth)
        //設(shè)置它中心在鼠標(biāo)點(diǎn)擊的位置
        let x = e.layerX - ripple.offsetWidth / 2
        let y = e.layerY - ripple.offsetHeight / 2
        ripple.setAttribute('style', 'height: ' + d + 'px; width: ' + d + 'px; top: ' + y + 'px; left: ' + x + 'px;')
      }
      this.$nextTick(() => {
        //在動畫結(jié)束后移除animate
        setTimeout(() => {
          this.repple_button.animate = false
        }, 660)
      })
    }

完成就是這個效果啦


4.gif

完整代碼

button.vue

<template>
  <button @click="reppleClick"  class="__cov-button-ripple" :class="{active: repple_button.toggle}">
    <slot></slot>
    <span class="__cov-ripple" :class="{'animate': repple_button.animate}"></span>
  </button>
</template>

<script>
export default {
  data () {
    return {
      repple_button: {
        animate: false,
        toggle: false
      }
    }
  },
  methods: {
    reppleClick (e) {
      this.repple_button.animate = true
      let button = e.target
      let ripple = button.querySelector('.__cov-ripple')
      if (ripple) {
        let d = Math.max(button.offsetHeight, button.offsetWidth)
        let x = e.layerX - ripple.offsetWidth / 2
        let y = e.layerY - ripple.offsetHeight / 2
        ripple.setAttribute('style', 'height: ' + d + 'px; width: ' + d + 'px; top: ' + y + 'px; left: ' + x + 'px;')
      }
      this.$nextTick(() => {
        setTimeout(() => {
          this.repple_button.animate = false
        }, 660)
      })
    }
  }
}
</script>

<style>
.__cov-button-ripple {
  background: transparent;
  border: none;
  border-radius: 2px;
  color: #000;
  position: relative;
  height: 36px;
  min-width: 64px;
  padding: 0 16px;
  display: inline-block;
  font-family: Roboto,Helvetica,Arial,sans-serif;
  font-size: 14px;
  font-weight: 500;
  text-transform: uppercase;
  line-height: 1;
  letter-spacing: 0;
  overflow: hidden;
  will-change: box-shadow,transform;
  -webkit-transition: box-shadow .2s cubic-bezier(.4,0,1,1),background-color .2s cubic-bezier(.4,0,.2,1),color .2s cubic-bezier(.4,0,.2,1);
  transition: box-shadow .2s cubic-bezier(.4,0,1,1),background-color .2s cubic-bezier(.4,0,.2,1),color .2s cubic-bezier(.4,0,.2,1);
  outline: none;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  line-height: 36px;
  vertical-align: middle;
  min-width: 96px;
}
.__cov-button-ripple:hover {
  background-color: hsla(0,0%,62%,.2);
}
.__cov-ripple {
  display: block; 
  position: absolute;
  background: hsla(0, 0%, 65%, 0.66);
  border-radius: 100%;
  transform: scale(0);
}
.__cov-ripple.animate {
  animation: ripple 0.65s linear;
}

@keyframes ripple {
  100% {opacity: 0; transform: scale(2.5);}
}
</style>

用法

<template>
  <div class="__cov-modal">
    <div class="__cov-modal-title">資料設(shè)定</div>
    <div class="__cov-modal-text">
      某些文字
    </div>
    <div class="__cov-modal-actions">
      <!-- covbutton 在這里-->
      <cov-button class="__cov-modal-btn" @click="close">OK</cov-button>
    </div>
  </div>
</template>

<script>
import covButton from './button.vue'
export default {
  components: {
    covButton
  }
}
</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • UI組件element ★11612 - 餓了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于...
    董董董董董董董董董大笨蛋閱讀 8,806評論 6 123
  • 今天有朋友開車回老家,提前通知了我出發(fā)的時間。我在這邊辭職了,有很多行李需要帶回去。之前就約定好的,可是因?yàn)槲业南?..
    扈琴閱讀 209評論 0 0
  • 初秋,天微涼,去朋友家小坐。她無意間提起自已家的一個遠(yuǎn)房親戚,瞬間愣住,轉(zhuǎn)而換了一個話題。 在她那恍惚的神情里有一...
    雨晴天空閱讀 435評論 8 15
  • 首先先引用陽神Sunny博客中的幾道面試題: GCD開發(fā)中用的十分廣泛,所以有必要進(jìn)行深入的了解。下面就一步一步的...
    longjianjiang閱讀 571評論 0 5

友情鏈接更多精彩內(nèi)容