成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

彈彈彈,彈走魚尾紋的彈出菜單(vue)

zilu / 3483人閱讀

摘要:前言上一篇面試的總結(jié),大家看的還行,因為量很大,錯誤在所難免,希望大家發(fā)現(xiàn)錯誤了可以告訴我一聲,我的郵箱是,一個小前端的希望。

前言

上一篇面試的總結(jié),大家看的還行,因為量很大,錯誤在所難免,希望大家發(fā)現(xiàn)錯誤了可以告訴我一聲,我的郵箱是[email protected],一個小前端的希望。

言歸正傳

我們老樣子直接先上效果圖再開始今天的分享
這個項目的github可以看一看

組件分析

界面組成

邏輯分析

最終實現(xiàn)

界面組成

從上圖中,我們可以看出界面主要分為menu和item2塊,其中menu的動畫是自傳,item的動畫是位移,然后這里我們通過絕對布局的方式將整個控件定位在四個角落

.menu_container {
    position: absolute;
    z-index: 100;
    border-radius: 50%;
    transition-duration: 400ms;
    text-align: center;
    border: #efefef 3px solid;
    box-shadow: aliceblue 1px 1px 1px;
  }

  .menu_item {
    position: absolute;
    border-radius: 50%;
    z-index: 99;
    border: #efefef 3px solid;
    text-align: center;
    box-shadow: aliceblue 1px 1px 1px;
  }
邏輯分析

這里我將這個控件幾個屬性獨立出來,方便下次開發(fā),其中包含,menu的背景,整個控件在屏幕的哪個角落,menu的寬高,item距離menu位移的距離,menu的背景色,及item的背景色,item的相關(guān)內(nèi)容則由數(shù)據(jù)來控制,具體的我們直接在下方的實現(xiàn)里來講解。

最終實現(xiàn)

這里我用代碼加注釋的方式,幫助大家理解,template我簡單的帶過一下

核心實現(xiàn)
通過分析可以得出,每個item的偏移量應(yīng)該為
橫向x:基礎(chǔ)值 * sin(角度值)
縱向y:基礎(chǔ)值 * cos(角度值)
角度值:(數(shù)組的長度-1-當(dāng)前的下標(biāo)) 每一塊所占的角度 弧度表示
弧度表示:2 * Math.PI / 360

export default {
    ...
    props: {//開放的屬性,方便自定義
      menuSrc: {
        default: require("../assets/menu.png")
      },
      position: {
        default: "LT"http://可選擇LT、LB、RT、RB4個角落
      },
      width: {
        default: 50,
      },
      baseDistance: {
        default: 150,
      },
      menuBg: {
        default: "white"
      },
      itemBg: {
        default: "white"
      },
      menuItems: {
        type: Array,
      }
    },
    data() {
      return {
        openFlag: false,//展開合并標(biāo)志
        operators: ["+", "+"],//用于記錄展開時動畫XY方向
      }
    },
    mounted() {
      //根據(jù)props初始化各內(nèi)容的各種style
      this.$refs.menuHome.style.width = this.width + "px";
      this.$refs.menuHome.style.height = this.width + "px";
      this.$refs.menuHome.style.lineHeight = this.width + "px";
      this.$refs.menuHome.style.background = this.menuBg;
      this.menuItems.forEach((item) => {
        let el = document.getElementById(item.name);
        el.style.width = `${this.width * 0.8}px`;
        el.style.height = `${this.width * 0.8}px`;
        el.style.lineHeight = `${this.width * 0.8}px`;
        el.style.background = this.itemBg;
      });
      //根據(jù)position,選擇不同的定位
      switch (this.position) {
        case "LT":
          this.$refs.menuHome.style.left = "20px";
          this.$refs.menuHome.style.top = "20px";
          this.menuItems.forEach((item) => {
            let el = document.getElementById(item.name);
            el.style.left = "26px";
            el.style.top = "26px";

          });
          this.operators = ["+", "+"];
          break;
        ...
      }
    },
    methods: {
      toggleMenu() {
        if (!this.openFlag) {//合并時,點擊展開操作
          this.menuItems.forEach((item, index) => {
            this.toggleMenuTransition(item.name, index, false)
          });
          //menu本身轉(zhuǎn)一周
          this.$refs.menuHome.style.transform = "rotate(360deg)";
        } else {
          this.menuItems.forEach((item, index) => {
            this.toggleMenuTransition(item.name, index, true)
          });
          //menu恢復(fù)
          this.$refs.menuHome.style.transform = "rotate(0)";
        }
        this.openFlag = !this.openFlag;
      },
      toggleMenuTransition(name, index, revert) {
        let oneArea = 90 / (this.menuItems.length - 1);//每一塊所占的角度
        let axisX = Math.sin((this.menuItems.length - 1 - index) * oneArea * 2 * Math.PI / 360);//橫坐標(biāo)所偏移的比例
        let axisY = Math.cos((this.menuItems.length - 1 - index) * oneArea * 2 * Math.PI / 360);//縱坐標(biāo)所便宜的比例
        let el = document.getElementById(name);//若所傳的name一直,會報錯。
        let that = this;
        if (!revert) {
          setTimeout(function () {
            el.style.transitionDuration = "200ms";
            el.style.transform = `translate(${that.operators[0]}${that.baseDistance * axisX}px,${that.operators[1]}${that.baseDistance * axisY }px)`;//進行動畫
          }, index * 100)//通過定時器的方式,達(dá)到一個一個彈出來的效果
        } else {
          //item恢復(fù)
          el.style.transitionDuration = "200ms";
          el.style.transform = `translate(0,0)`;
        }
      },
      clickMenu(item, index) {
        //暴露方法給父組件,進行點擊事件的操作
        this.$emit("clickMenu", item, index)
      }
    }
  }

再父組件中引入就可以大功告成啦,先跳一會兒吧,燃燒你的卡路里

父組件調(diào)用

引入組件

import toggleMenu from "./toggleMenu"

在 components聲明

components: {
     toggleMenu
},

template中使用

menuItems: [//name和src必填,且name唯一否則會報錯
       {name: "menu1", src: require("../assets/emoji.png")},
       {name: "menu2", src: require("../assets/cart.png")},
       {name: "menu3", src: require("../assets/folder.png")},
       {name: "menu4", src: require("../assets/home.png")},
       {name: "menu5", src: require("../assets/my.png")},
]
屬性及方法一欄
屬性名 用處 默認(rèn)值 是否必須
position 四個方位(LT、LB、RT、RB) LT
menuBg 菜單背景 white
menuSrc 菜單圖片 一個菜單圖片
itemBg 按鈕背景 white
width 按鈕寬度 50px
baseDistance 位移距離,若item很多,可適當(dāng)提高 150px
menuItems 菜單數(shù)組
方法名 用處 參數(shù)
clickMenu 點擊item觸發(fā)事件 item,index

好了,差不多就分享這么多,


我的github,求戳,求star

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/97668.html

相關(guān)文章

  • 彈彈彈,彈走尾紋彈出菜單vue

    摘要:前言上一篇面試的總結(jié),大家看的還行,因為量很大,錯誤在所難免,希望大家發(fā)現(xiàn)錯誤了可以告訴我一聲,我的郵箱是,一個小前端的希望。 前言 上一篇面試的總結(jié),大家看的還行,因為量很大,錯誤在所難免,希望大家發(fā)現(xiàn)錯誤了可以告訴我一聲,我的郵箱是[email protected],一個小前端的希望。showImg(https://segmentfault.com/img/remote/146000...

    mingde 評論0 收藏0
  • 一個神奇的vue小插件,讓你更easy得彈彈

    摘要:用了,這類的框架之后,發(fā)現(xiàn)組件化的開發(fā)在大部分時間很方便,但是有些時候卻用起來很變扭。更新今天發(fā)現(xiàn)一個大失誤,原來我一直沒把包發(fā)布到。。。趕緊發(fā)布了一下,如果有任務(wù)問題,都可以在的里面提出來 用了vue,react這類的框架之后,發(fā)現(xiàn)組件化的開發(fā)在大部分時間很方便,但是有些時候卻用起來很變扭。 比如我需要自定義一個alert組件,組件寫起來很方便,但是使用的時候卻是這樣的: ...

    harryhappy 評論0 收藏0
  • HTML5,不只是看上去很美(第四彈:可交互地鐵線路圖)

    摘要:哥決定把小弟的成果納入,不只是看上去很美系列,以示鼓勵其實還挺有壓力的,后浪推前浪,新人趕舊人。連續(xù)單擊同一站點連續(xù)單擊同一站點注意不是雙擊,可以將經(jīng)過此站點的所有線路突出顯示出來。 前言 最近特別忙,承蒙大伙關(guān)照,3D機房的項目一個接著一個,領(lǐng)了一幫小弟,搞搞傳幫帶,烏飛兔走,轉(zhuǎn)眼已經(jīng)菊黃蟹肥……有個小弟很不錯,勤奮好學(xué),很快就把API都摸透了,國慶幾天自己折騰著做了個HTML5的魔...

    roland_reed 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<