Vue實(shí)現(xiàn)液態(tài)玻璃登錄卡片的效果示例
效果介紹
液態(tài)玻璃(Liquid Glass)是一種極具現(xiàn)代感的UI視覺風(fēng)格,常見于高端網(wǎng)站和操作系統(tǒng)界面。它通過(guò)多層疊加、模糊、光澤、濾鏡等技術(shù),模擬出玻璃的通透、折射和高光質(zhì)感。蘋果的這次系統(tǒng)設(shè)計(jì)更新,帶火了這一設(shè)計(jì)效果,本教程將帶你一步步實(shí)現(xiàn)一個(gè)帶有3D靈動(dòng)傾斜交互的液態(tài)玻璃登錄卡片。
實(shí)際效果:
技術(shù)原理解析
1. 多層疊加
液態(tài)玻璃效果的核心是多層視覺疊加:
- 模糊層(blur):讓背景內(nèi)容變得虛化,產(chǎn)生玻璃的通透感。
- 色調(diào)層(tint):為玻璃加上一層淡淡的色彩,提升質(zhì)感。
- 高光層(shine):模擬玻璃邊緣的高光和內(nèi)陰影,增強(qiáng)立體感。
- SVG濾鏡:通過(guò) SVG 的
feTurbulence
和feDisplacementMap
,讓玻璃表面產(chǎn)生微妙的扭曲和流動(dòng)感。
2. 3D靈動(dòng)傾斜
通過(guò)監(jiān)聽鼠標(biāo)在卡片上的移動(dòng),動(dòng)態(tài)計(jì)算并設(shè)置 transform: perspective(...) rotateX(...) rotateY(...)
,讓卡片隨鼠標(biāo)靈動(dòng)傾斜,增強(qiáng)交互體驗(yàn)。
3. 背景與環(huán)境
背景可以是漸變色,也可以是圖片。玻璃卡片通過(guò) backdrop-filter
與背景內(nèi)容產(chǎn)生交互,形成真實(shí)的玻璃質(zhì)感。
實(shí)現(xiàn)步驟詳解
1. 結(jié)構(gòu)搭建
<template> <div class="login-container animated-background"> <!-- SVG濾鏡庫(kù) --> <svg style="display: none">...</svg> <!-- 登錄卡片 --> <div class="glass-component login-card" ref="tiltCard" @mousemove="handleMouseMove" @mouseleave="handleMouseLeave" > <div class="glass-effect"></div> <div class="glass-tint"></div> <div class="glass-shine"></div> <div class="glass-content"> <!-- 登錄表單內(nèi)容 --> </div> </div> </div> </template>
2. SVG濾鏡實(shí)現(xiàn)液態(tài)扭曲
<svg style="display: none"> <filter id="glass-distortion" x="0%" y="0%" width="100%" height="100%" filterUnits="objectBoundingBox"> <feTurbulence type="fractalNoise" baseFrequency="0.001 0.005" numOctaves="1" seed="17" result="turbulence" /> <feComponentTransfer in="turbulence" result="mapped"> <feFuncR type="gamma" amplitude="1" exponent="10" offset="0.5" /> <feFuncG type="gamma" amplitude="0" exponent="1" offset="0" /> <feFuncB type="gamma" amplitude="0" exponent="1" offset="0.5" /> </feComponentTransfer> <feGaussianBlur in="turbulence" stdDeviation="3" result="softMap" /> <feSpecularLighting in="softMap" aceScale="5" specularConstant="1" specularExponent="100" lighting-color="white" result="specLight"> <fePointLight x="-200" y="-200" z="300" /> </feSpecularLighting> <feComposite in="specLight" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="litImage" /> <feDisplacementMap in="SourceGraphic" in2="softMap" scale="200" xChannelSelector="R" yChannelSelector="G" /> </filter> </svg>
- 這段 SVG 代碼必須放在頁(yè)面結(jié)構(gòu)內(nèi),供 CSS filter 調(diào)用。
3. 背景設(shè)置
.animated-background { width: 100vw; height: 100vh; background-image: url('你的背景圖片路徑'); background-size: cover; background-position: center; background-repeat: no-repeat; position: fixed; top: 0; left: 0; z-index: -1; }
- 建議用高質(zhì)量漸變或壁紙,能更好襯托玻璃質(zhì)感。
4. 卡片多層玻璃結(jié)構(gòu)
.login-card { width: 400px; border-radius: 24px; overflow: hidden; box-shadow: 0 4px 24px 0 rgba(0,0,0,0.10), 0 1.5px 6px 0 rgba(0,0,0,0.08); background: transparent; position: relative; } .glass-effect { position: absolute; inset: 0; z-index: 0; backdrop-filter: blur(5px); filter: url(#glass-distortion); isolation: isolate; border-radius: 24px; } .glass-tint { position: absolute; inset: 0; z-index: 1; background: rgba(0, 0, 0, 0.15); border-radius: 24px; } .glass-shine { position: absolute; inset: 0; z-index: 2; border: 1px solid rgba(255, 255, 255, 0.13); border-radius: 24px; box-shadow: inset 1px 1px 8px 0 rgba(255, 255, 255, 0.18), inset -1px -1px 8px 0 rgba(255, 255, 255, 0.08); pointer-events: none; } .glass-content { position: relative; z-index: 3; padding: 2rem; color: white; }
- 每一層都要有一致的 border-radius,才能保證圓角處無(wú)割裂。
5. 3D靈動(dòng)傾斜交互
methods: { handleMouseMove (e) { const card = this.$refs.tiltCard const rect = card.getBoundingClientRect() const x = e.clientX - rect.left const y = e.clientY - rect.top const centerX = rect.width / 2 const centerY = rect.height / 2 const maxTilt = 18 const rotateY = ((x - centerX) / centerX) * maxTilt const rotateX = -((y - centerY) / centerY) * maxTilt card.style.transform = `perspective(600px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale(1.03)` }, handleMouseLeave () { const card = this.$refs.tiltCard card.style.transform = 'perspective(600px) rotateX(0deg) rotateY(0deg) scale(1)' } }
- 鼠標(biāo)移動(dòng)時(shí),卡片會(huì)根據(jù)指針位置靈動(dòng)傾斜。
- 鼠標(biāo)移出時(shí),卡片平滑恢復(fù)。
6. 細(xì)節(jié)優(yōu)化
- 陰影柔和:避免黑色邊緣過(guò)重,提升高級(jí)感。
- 高光線條:用低透明度白色邊框和內(nèi)陰影,模擬玻璃高光。
- 所有層的圓角一致:防止割裂。
- 表單輸入框:用半透明背景和模糊,保持整體風(fēng)格統(tǒng)一。
7.完整代碼
<template> <div class="login-container animated-background"> <!-- SVG濾鏡庫(kù) --> <svg style="display: none"> <filter id="glass-distortion" x="0%" y="0%" width="100%" height="100%" filterUnits="objectBoundingBox"> <feTurbulence type="fractalNoise" baseFrequency="0.001 0.005" numOctaves="1" seed="17" result="turbulence" /> <feComponentTransfer in="turbulence" result="mapped"> <feFuncR type="gamma" amplitude="1" exponent="10" offset="0.5" /> <feFuncG type="gamma" amplitude="0" exponent="1" offset="0" /> <feFuncB type="gamma" amplitude="0" exponent="1" offset="0.5" /> </feComponentTransfer> <feGaussianBlur in="turbulence" stdDeviation="3" result="softMap" /> <feSpecularLighting in="softMap" aceScale="5" specularConstant="1" specularExponent="100" lighting-color="white" result="specLight"> <fePointLight x="-200" y="-200" z="300" /> </feSpecularLighting> <feComposite in="specLight" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="litImage" /> <feDisplacementMap in="SourceGraphic" in2="softMap" scale="200" xChannelSelector="R" yChannelSelector="G" /> </filter> </svg> <!-- 登錄卡片 --> <div class="glass-component login-card" ref="tiltCard" @mousemove="handleMouseMove" @mouseleave="handleMouseLeave" > <div class="glass-effect"></div> <div class="glass-tint"></div> <div class="glass-shine"></div> <div class="glass-content"> <h2 class="login-title">歡迎登錄</h2> <form class="login-form"> <div class="form-group"> <input type="text" placeholder="用戶名" class="glass-input"> </div> <div class="form-group"> <input type="password" placeholder="密碼" class="glass-input"> </div> <button type="submit" class="glass-button">登錄</button> </form> </div> </div> </div> </template> <script> export default { name: 'LiquidGlass', data () { return { // 可以添加需要的數(shù)據(jù) } }, methods: { handleMouseMove (e) { const card = this.$refs.tiltCard const rect = card.getBoundingClientRect() const x = e.clientX - rect.left const y = e.clientY - rect.top const centerX = rect.width / 2 const centerY = rect.height / 2 // 最大旋轉(zhuǎn)角度 const maxTilt = 18 const rotateY = ((x - centerX) / centerX) * maxTilt const rotateX = -((y - centerY) / centerY) * maxTilt card.style.transform = `perspective(600px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale(1.03)` }, handleMouseLeave () { const card = this.$refs.tiltCard card.style.transform = 'perspective(600px) rotateX(0deg) rotateY(0deg) scale(1)' } } } </script> <style lang="scss" scoped> .login-container { min-height: 100vh; display: flex; align-items: center; justify-content: center; position: relative; overflow: hidden; } .animated-background { width: 100%; height: 100%; background-image: url('../../assets/macwallpaper.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat; } .login-card { width: 400px; position: relative; border-radius: 24px; overflow: hidden; box-shadow: 0 4px 24px 0 rgba(0,0,0,0.10), 0 1.5px 6px 0 rgba(0,0,0,0.08); transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.6); cursor: pointer; background: transparent; } .glass-effect { position: absolute; inset: 0; z-index: 0; backdrop-filter: blur(5px); filter: url(#glass-distortion); isolation: isolate; border-radius: 24px; } .glass-tint { position: absolute; inset: 0; z-index: 1; background: rgba(0, 0, 0, 0.15); border-radius: 24px; } .glass-shine { position: absolute; inset: 0; z-index: 2; border: 1px solid rgba(255, 255, 255, 0.13); border-radius: 24px; box-shadow: inset 1px 1px 8px 0 rgba(255, 255, 255, 0.18), inset -1px -1px 8px 0 rgba(255, 255, 255, 0.08); pointer-events: none; } .glass-content { position: relative; z-index: 3; padding: 2rem; color: white; } .login-title { text-align: center; color: #fff; margin-bottom: 2rem; font-size: 2rem; font-weight: 600; text-shadow: 0 1px 3px rgba(0,0,0,0.2); } .form-group { margin-bottom: 1.5rem; } .glass-input { width: 90%; padding: 12px 20px; border: none; border-radius: 10px; background: rgba(255, 255, 255, 0.1); color: #fff; font-size: 1rem; backdrop-filter: blur(5px); transition: all 0.3s ease; &::placeholder { color: rgba(255, 255, 255, 0.7); } &:focus { outline: none; background: rgba(255, 255, 255, 0.2); box-shadow: 0 0 15px rgba(255, 255, 255, 0.1); } } .glass-button { width: 100%; padding: 12px; border: none; border-radius: 10px; background: rgba(255, 255, 255, 0.2); color: #fff; font-size: 1rem; font-weight: 600; cursor: pointer; transition: all 0.3s ease; backdrop-filter: blur(5px); position: relative; overflow: hidden; &:hover { background: rgba(255, 255, 255, 0.3); transform: translateY(-2px); box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2); } &:active { transform: translateY(0); } } // 添加點(diǎn)擊波紋效果 .click-gradient { position: absolute; border-radius: 50%; background: radial-gradient(circle, rgba(255,255,255,0.4) 0%, rgba(180,180,255,0.2) 40%, rgba(100,100,255,0.1) 70%, rgba(50,50,255,0) 100%); transform: translate(-50%, -50%) scale(0); opacity: 0; pointer-events: none; z-index: 4; } .glass-component.clicked .click-gradient { animation: gradient-ripple 0.6s ease-out; } @keyframes gradient-ripple { 0% { transform: translate(-50%, -50%) scale(0); opacity: 1; } 100% { transform: translate(-50%, -50%) scale(3); opacity: 0; } } .glass-component { transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1); will-change: transform; } </style>
常見問題與優(yōu)化建議
- 陰影過(guò)重/黑邊:減小 box-shadow 的透明度和模糊半徑。
- 圓角割裂:所有玻璃層都要加 border-radius。
- 背景不通透:確保 glass-effect 層有 blur 和 SVG filter。
- 性能問題:backdrop-filter 在低端設(shè)備上可能有性能損耗,建議只在必要區(qū)域使用。
- 瀏覽器兼容性:backdrop-filter 需現(xiàn)代瀏覽器支持,IE/部分安卓瀏覽器不兼容。
技術(shù)要點(diǎn)總結(jié)
- SVG濾鏡:讓玻璃表面有微妙的流動(dòng)和扭曲感。
- backdrop-filter: blur:實(shí)現(xiàn)背景虛化。
- 多層疊加:色調(diào)、高光、陰影共同營(yíng)造真實(shí)玻璃質(zhì)感。
- 3D transform:提升交互體驗(yàn)。
- 細(xì)節(jié)打磨:陰影、邊框、圓角、色彩都要精細(xì)調(diào)整。
結(jié)語(yǔ)
液態(tài)玻璃效果是現(xiàn)代前端視覺的代表之一。只要理解其原理,分層實(shí)現(xiàn)、細(xì)致調(diào)優(yōu),任何人都能做出媲美 macOS、Win11 的高端玻璃UI。希望本教程能幫助你掌握這項(xiàng)技術(shù),做出屬于自己的酷炫界面!
到此這篇關(guān)于Vue實(shí)現(xiàn)一個(gè)“液態(tài)玻璃”效果登錄卡片的文章就介紹到這了,更多相關(guān)Vue實(shí)現(xiàn)液態(tài)玻璃登錄卡片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue+element 鍵盤回車事件導(dǎo)致頁(yè)面刷新的問題
今天小編就為大家分享一篇解決vue+element 鍵盤回車事件導(dǎo)致頁(yè)面刷新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue實(shí)現(xiàn)修改標(biāo)簽中的內(nèi)容:id class style
這篇文章主要介紹了vue實(shí)現(xiàn)修改標(biāo)簽中的內(nèi)容:id class style,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue實(shí)現(xiàn)web前端登錄頁(yè)數(shù)字驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)web前端登錄頁(yè)數(shù)字驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06vue獲取當(dāng)前點(diǎn)擊的元素并傳值的實(shí)例
下面小編就為大家分享一篇vue獲取當(dāng)前點(diǎn)擊的元素并傳值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03使用Vue Router進(jìn)行路由組件傳參的實(shí)現(xiàn)方式
Vue Router 為 Vue.js 應(yīng)用提供了完整的路由解決方案,其中包括了組件間的數(shù)據(jù)傳遞功能,通過(guò)路由組件傳參,我們可以輕松地在導(dǎo)航到新頁(yè)面時(shí)傳遞必要的數(shù)據(jù),本文將深入探討如何使用 Vue Router 進(jìn)行路由組件間的傳參,并通過(guò)多個(gè)示例來(lái)展示其實(shí)現(xiàn)方式2024-09-09vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)
這篇文章主要介紹了vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05