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

vue實(shí)現(xiàn)點(diǎn)擊出現(xiàn)按鈕菜單問題

 更新時(shí)間:2026年03月06日 16:21:45   作者:心明自知  
文章主要介紹了如何在Vue中實(shí)現(xiàn)點(diǎn)擊按鈕顯示菜單的功能,父組件負(fù)責(zé)觸發(fā)事件,子組件負(fù)責(zé)渲染菜單,通過事件冒泡和狀態(tài)管理,實(shí)現(xiàn)了菜單的顯示和隱藏,代碼示例簡潔明了,適合初學(xué)者參考

vue點(diǎn)擊出現(xiàn)按鈕菜單

父組件代碼

<template>
  <div class="app-container">
	  <div @click="popup($event, item)">
	  		點(diǎn)擊
	  </div>
  	  <!-- 單擊菜單 -->
      <popup-menu ref="popup" :menuData="menuData" @menuClick="menuClick(arguments)">
      </popup-menu>
  </div>
 <script>
  import PopupMenu from '@/views/popupMenu';

  export default {
  		data(){
  			return{
  				menuData: [{
		          id: 'edit',
		          name: '編輯'
		        }, {
		          id: 'delete',
		          name: '刪除'
		        }], //菜單數(shù)據(jù)
  			}
  		},
		components: {
      		PopupMenu,
    	},
    	methods: {
    		popup(event, item) {
        		this.$refs.popup.show(event); //調(diào)用子組件的show方法,用于顯示彈出式菜單,引用組件時(shí),需要 ref="child"
      		},
      		menuClick(args) {
        		// 子組件的方法 @menuClick="menuClick(arguments)" 注意menuClick()里面一定要填寫arguments,用于接受子組件的傳參
        		let type = args[0];
		        if (type == 'edit') {
		        	console.log('編輯');
		        } else if (type == 'delete') {
		        	console.log('刪除');
		        }
      		},
    	}
  }
   </script>
</template>

菜單組件代碼

<template>
  <div v-if="showMenu" class="position-fixed popup-menu flex flex-col" :style="{top:clientY, left:clientX}">
    <div v-for="item in menuData" class="popup-menu-item pointer flex flex-center-cz padding-left-m" :data-id="item.id"
      @click="itemClick">{{item.name}}</div>
  </div>
</template>

<script>
  export default {
    name: 'PopupMenu',
    props: {
      menuData: {},
    },

    data() {
      return {
        showMenu: false,
        clientX: '',
        clientY: '',
      }
    },

    methods: {

      show(event) {
        // console.log("父組件傳過來的event", event);
        let x = event.x; //鼠標(biāo)點(diǎn)擊的x坐標(biāo)
        let y = event.y; //鼠標(biāo)點(diǎn)擊的y坐標(biāo)
        let menuWidth = 150; //菜單寬度,這個(gè)與.popup-menu樣式對應(yīng)的寬度一致
        let menuHeight = this.menuData.length * 30 + 20; //菜單高度,根據(jù)菜單項(xiàng)自動(dòng)計(jì)算的高度

        this.clientX = (parseFloat(x) - 10) + "px";
        this.clientY = (parseFloat(y) + 10) + "px";

        let windowWidth = document.documentElement.clientWidth; // 實(shí)時(shí)屏幕寬度
        let windowHeight = document.documentElement.clientHeight; // 實(shí)時(shí)屏幕高度

        if (parseFloat(x) + menuWidth > windowWidth) {
          this.clientX = (parseFloat(windowWidth) - menuWidth - 50) + "px";
        }
        if (parseFloat(y) + menuHeight > windowHeight) {
          this.clientY = (parseFloat(windowHeight) - menuHeight - 50) + "px";
        }
        this.showMenu = true;
        event.stopPropagation(); //阻止事件冒泡
        document.addEventListener("click", this.closeMenu, false); // 添加關(guān)閉事件
      },
      closeMenu() {
        // console.log("銷毀監(jiān)聽事件。");
        document.removeEventListener("click", this.closeMenu, false); //銷毀關(guān)閉事件
        this.showMenu = false;
      },
      itemClick(event) {
        let id = event.target.getAttribute("data-id"); //獲取菜單項(xiàng)id
        this.$emit('menuClick', id); //傳參調(diào)用父組件事件,讓父組件知道是點(diǎn)擊到哪個(gè)菜單項(xiàng)
      }

    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .position-fixed {
    position: fixed;
  }

  .popup-menu {

    width: 80px;
    padding: 10px;
    border-radius: 10px;
    background-color: #fff;
    border-style: solid;
    border-width: 1px;
    border-color: rgba(0, 0, 0, .2);
    z-index: 999;
  }

  .popup-menu-item {
    height: 20px;
  }

  .popup-menu-item:hover {
    border-radius: 5px;
    background: rgba(0, 0, 0, .1);
  }
</style>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

长白| 晋中市| 万宁市| 平潭县| 牡丹江市| 淳化县| 志丹县| 即墨市| 金阳县| 新泰市| 黔南| 虞城县| 临潭县| 赣榆县| 西华县| 连山| 邳州市| 长葛市| 渑池县| 许昌市| 万宁市| 田阳县| 自贡市| 茂名市| 东安县| 汝城县| 石台县| 清苑县| 太白县| 青神县| 汝阳县| 中方县| 额济纳旗| 元阳县| 勃利县| 嘉荫县| 剑川县| 白城市| 申扎县| 洪泽县| 昭觉县|