整合營(yíng)銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          CSS蒙層+彈框

          CSS蒙層+彈框
          <!DOCTYPE html>
          <html lang="en">
          <head>
           <title></title>
           <meta charset="UTF-8">
           <meta name="viewport" content="width=device-width, initial-scale=1">
           <link href="css/style.css" rel="stylesheet">
           <style>
           * {
           margin: 0;
           }
           .mask {
           background-color: #7b7070;
           position: absolute;
           top: 0;
           left: 0;
           right: 0;
           bottom: 0;
           width: 100%;
           height: 100%;
           }
           .box {
           position: absolute;
           top: 50%;
           margin-top: -100px;
           background-color: #fff;
           height: 200px;
           width: calc(100% - 50px); //這里是水平居中的關(guān)鍵 margin-left=50/2
           margin-left: 25px;
           text-align: center;
           border-radius: 5px;
           }
           </style>
          </head>
          <body>
           <div>
           <div class="mask"></div>
           <div class="box">
           <p>title</p>
           </div>
           </div>
          </body>
          </html>
          

          Vue3 根據(jù)點(diǎn)擊位置,實(shí)現(xiàn)一個(gè)用戶頭像彈框定位

          **開篇:**

          在Web開發(fā)中,響應(yīng)式UI設(shè)計(jì)一直是提升用戶體驗(yàn)的關(guān)鍵所在。Vue3以其優(yōu)秀的響應(yīng)式機(jī)制和簡(jiǎn)潔的API深受開發(fā)者喜愛。本文將詳細(xì)介紹如何利用Vue3的功能特性,結(jié)合DOM事件處理和CSS定位,實(shí)現(xiàn)一個(gè)可以根據(jù)點(diǎn)擊位置動(dòng)態(tài)顯示用戶頭像彈框的功能。為了確保文章具有實(shí)踐指導(dǎo)意義,我們將按照實(shí)際操作流程,逐步解析并提供核心代碼示例。

          ---

          ### **一、搭建Vue3項(xiàng)目與基礎(chǔ)布局**

          **標(biāo)題:** 初始化項(xiàng)目與頁(yè)面結(jié)構(gòu)設(shè)定

          **內(nèi)容:**

          首先,使用Vue CLI創(chuàng)建一個(gè)新的Vue3項(xiàng)目,并在主頁(yè)面上設(shè)置基礎(chǔ)布局,包括一個(gè)用于觸發(fā)頭像彈框顯示的用戶頭像區(qū)域和一個(gè)隱藏的彈框組件。

          ```html

          <template>

          <div id="app">

          <!-- 用戶列表或其他包含頭像的區(qū)域 -->

          <div @click="showAvatarPopup($event)">

          <img :src="user.avatarUrl" alt="User Avatar" class="avatar" />

          </div>

          <!-- 頭像彈框組件,初始狀態(tài)為隱藏 -->

          <AvatarPopup v-if="isPopupVisible" :position="popupPosition" :user="user" @close="hideAvatarPopup" />

          </div>

          </template>

          <script>

          import AvatarPopup from '@/components/AvatarPopup.vue';

          export default {

          components: {

          AvatarPopup,

          },

          data() {

          return {

          isPopupVisible: false,

          user: { // 示例用戶數(shù)據(jù)

          avatarUrl: 'path/to/avatar.jpg',

          // 其他用戶信息...

          },

          popupPosition: { x: 0, y: 0 }, // 彈框初始位置

          };

          },

          methods: {

          showAvatarPopup(event) {

          this.popupPosition={ x: event.clientX, y: event.clientY }; // 獲取點(diǎn)擊位置

          this.isPopupVisible=true; // 顯示彈框

          },

          hideAvatarPopup() {

          this.isPopupVisible=false; // 隱藏彈框

          },

          },

          };

          </script>

          ```

          ### **二、創(chuàng)建并樣式化頭像彈框組件**

          **標(biāo)題:** 設(shè)計(jì)并實(shí)現(xiàn)自定義的`AvatarPopup`組件

          **內(nèi)容:**

          在`AvatarPopup.vue`組件中,我們需要接收傳遞過(guò)來(lái)的位置坐標(biāo),并使用CSS絕對(duì)定位來(lái)使彈框跟隨鼠標(biāo)點(diǎn)擊位置展示。

          ```html

          <!-- AvatarPopup.vue -->

          <template>

          <div class="avatar-popup" :style="{ top: position.y + 'px', left: position.x + 'px' }">

          <img :src="user.avatarUrl" alt="Popup Avatar" class="popup-avatar" />

          <!-- 其他用戶信息展示... -->

          <button @click="emitClose">關(guān)閉</button>

          </div>

          </template>

          <script>

          export default {

          props: {

          position: Object,

          user: Object,

          },

          emits: ['close'],

          methods: {

          emitClose() {

          this.$emit('close');

          },

          },

          };

          </script>

          <style scoped>

          .avatar-popup {

          position: absolute;

          width: fit-content;

          background-color: #fff;

          border-radius: 4px;

          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);

          padding: 1rem;

          z-index: 1000; /* 確保彈框位于頂層 */

          }

          .popup-avatar {

          width: 100px;

          height: 100px;

          object-fit: cover;

          }

          </style>

          ```

          ### **三、優(yōu)化彈框顯示邏輯**

          **標(biāo)題:** 考慮邊界情況,確保彈框始終在可視區(qū)域內(nèi)

          **內(nèi)容:**

          為了防止彈框超出瀏覽器窗口范圍,我們需要對(duì)計(jì)算出的彈框位置進(jìn)行適當(dāng)?shù)恼{(diào)整:

          ```javascript

          // 在App.vue中的methods內(nèi)

          showAvatarPopup(event) {

          const viewportWidth=document.documentElement.clientWidth;

          const viewportHeight=document.documentElement.clientHeight;

          const popupWidth=document.querySelector('.avatar-popup').offsetWidth;

          const popupHeight=document.querySelector('.avatar-popup').offsetHeight;

          const x=Math.min(Math.max(event.clientX, popupWidth / 2), viewportWidth - popupWidth / 2);

          const y=Math.min(Math.max(event.clientY, popupHeight / 2), viewportHeight - popupHeight / 2);

          this.popupPosition={ x, y };

          this.isPopupVisible=true;

          }

          ```

          ### **四、添加過(guò)渡動(dòng)畫效果**

          **標(biāo)題:** 使用Vue Transition實(shí)現(xiàn)彈框顯示/隱藏動(dòng)畫

          **內(nèi)容:**

          為了讓彈框的出現(xiàn)和消失更加流暢自然,我們可以使用Vue的Transition組件包裹AvatarPopup,為其添加CSS過(guò)渡動(dòng)畫。

          ```html

          <!-- App.vue -->

          <transition name="fade" appear>

          <AvatarPopup v-if="isPopupVisible" :position="popupPosition" :user="user" @close="hideAvatarPopup" />

          </transition>

          <style>

          .fade-enter-active,

          .fade-leave-active {

          transition: opacity .3s ease;

          }

          .fade-enter-from,

          .fade-leave-to {

          opacity: 0;

          }

          </style>

          ```

          ---

          **總結(jié):**

          通過(guò)以上步驟,我們成功地在Vue3項(xiàng)目中實(shí)現(xiàn)了根據(jù)點(diǎn)擊位置動(dòng)態(tài)定位用戶頭像彈框的功能。這一功能在社交網(wǎng)絡(luò)、評(píng)論區(qū)以及其他需要展現(xiàn)用戶詳細(xì)信息的場(chǎng)景中非常實(shí)用,既提升了用戶體驗(yàn),也展現(xiàn)了Vue3強(qiáng)大而靈活的應(yīng)用能力。隨著進(jìn)一步實(shí)踐,你可以嘗試增加更多高級(jí)功能,如自動(dòng)調(diào)整彈框方向以適應(yīng)屏幕邊界,或是集成拖拽移動(dòng)等功能,從而使得彈框組件更為完善和人性化。

          家好,今天給大家介紹一款,css實(shí)現(xiàn)的圖片懸停旋轉(zhuǎn)彈出文本框html頁(yè)面前端源碼(圖1)。送給大家哦,獲取方式在本文末尾。

          圖1

          當(dāng)鼠標(biāo)經(jīng)過(guò)圖片的時(shí)候,就會(huì)激活旋轉(zhuǎn)和文本彈出事件,非常適合用在圖片展示的功能中(圖2)

          圖2

          源碼完整,需要的朋友可以下載學(xué)習(xí)(圖3)

          圖3

          本源碼編碼:20241,需要的朋友,訪問(wèn)下面鏈接后,搜索20241,即可獲取。

          「鏈接」


          主站蜘蛛池模板: 国产精久久一区二区三区| 亚洲av无码一区二区乱子伦as| 一区二区三区在线观看视频| 亚洲av鲁丝一区二区三区| 国产一区二区在线视频播放| 亚洲夜夜欢A∨一区二区三区| 波多野结衣一区在线观看| 亚洲av成人一区二区三区观看在线| 中文字幕VA一区二区三区| 无码精品人妻一区二区三区影院| 亚洲香蕉久久一区二区三区四区| 一区二区视频在线免费观看| 国产福利酱国产一区二区| 精品国产一区AV天美传媒| 无码国产精品一区二区免费式芒果 | 人妻天天爽夜夜爽一区二区| 自慰无码一区二区三区| 精品福利一区二区三区| 国产在线一区二区杨幂| 国产一区二区在线观看| 成人精品一区二区激情| 成人精品一区二区电影| 精品一区二区三区中文| 国产成人av一区二区三区在线| 日本精品一区二区三区视频| 在线不卡一区二区三区日韩| 日本一区二区三区久久| 国产福利一区二区精品秒拍| 国产一区风间由美在线观看| 日本高清无卡码一区二区久久 | 国产激情一区二区三区在线观看| 波多野结衣一区视频在线| 无码一区二区三区爆白浆| 国精产品999一区二区三区有限| 国产一区二区三区播放心情潘金莲 | 一区二区三区视频免费| 国偷自产Av一区二区三区吞精| 2014AV天堂无码一区| 国产成人精品无码一区二区| 精品成人乱色一区二区| 国产日韩精品一区二区三区|