Vue3 根據點擊位置,實現一個用戶頭像彈框定位
**開篇:**
在Web開發中,響應式UI設計一直是提升用戶體驗的關鍵所在。Vue3以其優秀的響應式機制和簡潔的API深受開發者喜愛。本文將詳細介紹如何利用Vue3的功能特性,結合DOM事件處理和CSS定位,實現一個可以根據點擊位置動態顯示用戶頭像彈框的功能。為了確保文章具有實踐指導意義,我們將按照實際操作流程,逐步解析并提供核心代碼示例。
---
### **一、搭建Vue3項目與基礎布局**
**標題:** 初始化項目與頁面結構設定
**內容:**
首先,使用Vue CLI創建一個新的Vue3項目,并在主頁面上設置基礎布局,包括一個用于觸發頭像彈框顯示的用戶頭像區域和一個隱藏的彈框組件。
```html
<template>
<div id="app">
<!-- 用戶列表或其他包含頭像的區域 -->
<div @click="showAvatarPopup($event)">
<img :src="user.avatarUrl" alt="User Avatar" class="avatar" />
</div>
<!-- 頭像彈框組件,初始狀態為隱藏 -->
<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: { // 示例用戶數據
avatarUrl: 'path/to/avatar.jpg',
// 其他用戶信息...
},
popupPosition: { x: 0, y: 0 }, // 彈框初始位置
};
},
methods: {
showAvatarPopup(event) {
this.popupPosition = { x: event.clientX, y: event.clientY }; // 獲取點擊位置
this.isPopupVisible = true; // 顯示彈框
},
hideAvatarPopup() {
this.isPopupVisible = false; // 隱藏彈框
},
},
};
</script>
```
### **二、創建并樣式化頭像彈框組件**
**標題:** 設計并實現自定義的`AvatarPopup`組件
**內容:**
在`AvatarPopup.vue`組件中,我們需要接收傳遞過來的位置坐標,并使用CSS絕對定位來使彈框跟隨鼠標點擊位置展示。
```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">關閉</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>
```
### **三、優化彈框顯示邏輯**
**標題:** 考慮邊界情況,確保彈框始終在可視區域內
**內容:**
為了防止彈框超出瀏覽器窗口范圍,我們需要對計算出的彈框位置進行適當的調整:
```javascript
// 在App.vue中的methods內
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;
}
```
### **四、添加過渡動畫效果**
**標題:** 使用Vue Transition實現彈框顯示/隱藏動畫
**內容:**
為了讓彈框的出現和消失更加流暢自然,我們可以使用Vue的Transition組件包裹AvatarPopup,為其添加CSS過渡動畫。
```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>
```
---
**總結:**
通過以上步驟,我們成功地在Vue3項目中實現了根據點擊位置動態定位用戶頭像彈框的功能。這一功能在社交網絡、評論區以及其他需要展現用戶詳細信息的場景中非常實用,既提升了用戶體驗,也展現了Vue3強大而靈活的應用能力。隨著進一步實踐,你可以嘗試增加更多高級功能,如自動調整彈框方向以適應屏幕邊界,或是集成拖拽移動等功能,從而使得彈框組件更為完善和人性化。
在現代網頁設計中,個人主頁是一個展示個人信息、技能、事件等的重要載體。為了吸引訪客的注意力并提供良好的用戶體驗,設計師通常會運用各種技巧和效果來增加頁面的吸引力。本文將介紹如何使用CSS創建一個驚嘆的個人主頁介紹卡片,展示獨特魅力;
首先,需要定義基本的HTML結構來容納個人主頁介紹卡片;
這里外層使用一個div包裹,里面使用三個<div>元素作為包裹容器布局,并在其中添加所需的圖像、內容和按鈕等:
<div class="card">
<div class="box">
<div class="img_box">
<video
src="./assets/video.mp4"
muted
autoplay
loop
/>
</div>
</div>
<div class="box">
<div class="content">
<h2>
Alexa
<br>
<span>
Professional Artist
</span>
</h2>
<ul>
<li>
Posts
<span>22</span>
</li>
<li>
Followers
<span>999+</span>
</li>
<li>
Following
<span>7</span>
</li>
</ul>
<button>Follow</button>
</div>
</div>
<div class="circle">
<div class="img_box">
<img src="./assets/user.jpg" alt="">
</div>
</div>
</div>
外層是card容器,視頻和文本內容區域是上下布局的,分別使用box容器包裹,最后是circle容器包裹頭像在定位在中間左邊超出;
注:
video設置屬性:靜音(muted)可實現自動播放(autoplay),接著設置循環播放(loop);
img>和video>的父容器是一個類名img_box;
接下來,我們將使用CSS來為個人主頁介紹卡片添加樣式。以下是一些關鍵的樣式屬性和技巧,可以使卡片看起來更加漂亮和吸引人;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--clr: #083d41
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: var(--clr);
}
.card {
background-color: var(--clr);
position: relative;
width: 320px;
height: 430px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
/* 先把容器基本樣式調整一下 */
.card .box {
background-color: tomato;
position: relative;
width: 110%;
height: 200px;
/* 文本內容區域圓角 */
border-radius: 20px;
}
/* 頭像容器則使用定位布局 */
.card .circle {
width: 180px;
height: 180px;
position: absolute;
left: -70px;
top: 50%;
transform: translateY(-50%);
border-radius: 50%;
border: 10px solid var(--clr);
}
/* 調整img和video共有的父容器樣式 */
.card .box .img_box,
.card .circle .img_box {
position: absolute;
inset: 0;
overflow: hidden;
/* img的圓角 */
border-radius: 50%;
}
.card .box .img_box {
/* video的圓角 */
border-radius: 15px;
}
/* 調整圖片和視頻的樣式 */
.card .box .img_box video,
.card .circle .img_box img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
調整card下的第一個box容器樣式,也就是包裹視頻的容器:
.card .box:nth-child(1)::before {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
z-index: 10;
top: 106px;
left: -1px;
border-bottom-left-radius: 20px;
box-shadow: -6px 6px var(--clr);
}
/* 樣式同before類似,注意定位樣式 */
.card .box:nth-child(1)::after {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
z-index: 10;
bottom: -1px;
left: 105px;
border-bottom-left-radius: 20px;
box-shadow: -6px 6px var(--clr);
}
目前添加樣式效果圖,可以在調試階段更改明顯色彩用于調整距離、位置等;
調整card下的第二個box容器樣式,也就是包含文字信息的容器:
.card .box:nth-child(2) {
background-color: #fff;
width: 100%;
height: 220px;
}
.card .box:nth-child(2)::before {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
z-index: 10;
bottom: 106px;
left: -1px;
border-top-left-radius: 20px;
box-shadow: -6px -6px var(--clr);
}
.card .box:nth-child(2)::after {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
z-index: 10;
top: -1px;
left: 109px;
border-top-left-radius: 20px;
box-shadow: -6px -6px var(--clr);
}
.card .box .content {
position: absolute;
inset: 0;
padding: 30px 10px 20px;
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
/* 姓名和Title樣式 */
.card .box .content h2 {
width: 100%;
padding-left: 120px;
text-transform: uppercase;
letter-spacing: 0.1em;
line-height: 1.1em;
font-size: 1.15em;
font-weight: 600;
color: #333;
}
.card .box .content h2 span {
letter-spacing: 0.05em;
font-size: 0.75em;
font-weight: 400;
color: tomato;
text-transform: initial;
}
/* 列表樣式 */
.card .box .content ul {
position: relative;
top: 15px;
width: 100%;
padding: 0 10px;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card .box .content ul li {
list-style: none;
display: flex;
flex-direction: column;
align-items: center;
padding: 0 10px;
font-size: 0.85em;
font-weight: 500;
color: #999;
}
.card .box .content ul li:not(:last-child)
{
border-right: 1px solid #ccc;
}
.card .box .content ul li span{
font-size: 1.65em;
color: #333;
}
/* 按鈕樣式 */
.card .box .content button {
position: relative;
top: 25px;
padding: 8px 30px;
border: none;
outline: none;
background-color: #03a9f4;
border-radius: 30px;
color: #fff;
font-size: 1em;
letter-spacing: 0.2em;
text-transform: uppercase;
font-weight: 500;
cursor: pointer;
border: 5px solid var(--clr);
box-shadow: 0 0 0 10px #fff;
transition: .5s;
}
.card .box .content button:hover {
letter-spacing: 0.5em;
background-color: #ff3d7f;
}
由于按鈕的圓角與文本內容卡片的交界處看上去顯得有些過于突兀了; 所以現在把它們的交界處優化成弧形,樣式類似box的偽元素,這里也給按鈕創建兩個偽元素,用于優化兩邊的交界處:
.card .box .content button::before {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
top: 23px;
left: -29px;
border-top-right-radius: 20px;
box-shadow: 5px -7px #fff;
}
.card .box .content button::after {
content: "";
width: 20px;
height: 20px;
background-color: transparent;
position: absolute;
top: 23px;
right: -29px;
border-top-left-radius: 20px;
box-shadow: -5px -7px #fff;
}
除了基本樣式之外,還進一步優化個人主頁介紹卡片的細節。一些可選的技巧包括:
通過運用CSS的各種樣式屬性和技巧,我們可以輕松地創建漂亮的個人主頁介紹卡片。這些卡片不僅能夠有效地展示個人信息和技能,還能夠吸引訪客的注意力并提供良好的用戶體驗。記得嘗試不同的樣式和效果來定制你自己獨特的個人主頁卡片!
CSS創作個人主頁介紹卡片,展示獨特魅力
原文鏈接:https://juejin.cn/post/7260709771870060603
者:sunshine小小倩
轉發鏈接:https://juejin.im/post/599970f4518825243a78b9d5
*請認真填寫需求信息,我們會在24小時內與您取得聯系。