vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個金屬拋光質(zhì)感的按鈕
前言
誒?這有一個按鈕(~ ̄▽ ̄)~,這是一個在html中實(shí)現(xiàn)的具有金屬質(zhì)感并且能鏡面反射的按鈕~
效果
電腦效果
手機(jī)效果
說明
主要思路是使用 navigator.mediaDevices.getUserMedia()
調(diào)用設(shè)備攝像頭來實(shí)現(xiàn)鏡面反射,css部分用到了filter
屬性,后期可以根據(jù)需要調(diào)整它的屬性值,做出更多反射效果。有了思路,實(shí)現(xiàn)起來還是比較簡單的。
需要注意的是window.navigator.mediaDevices.getUserMedia()
只能在localhost
或https
下才能使用,否則會報錯哦。
完整代碼
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <style> body { background-color: rgb(86, 86, 86); } :root { --transition: 0.1s; --border-radius: 56px; } .button-wrap { margin: calc(30vh - 50px) auto 0; width: 300px; height: 100px; position: relative; transition: transform var(--transition), box-shadow var(--transition); } .button-wrap.pressed { transform: translateZ(0) scale(0.98); } .button { width: 100%; height: 100%; overflow: hidden; border-radius: var(--border-radius); box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.6), 0px 0px 20px rgba(0, 0, 0, 0.4), 0px 0px 40px rgba(0, 0, 0, 0.2), inset 0 2px 2px rgba(255, 255, 255, 0.8), inset 0 -2px 2px rgba(255, 255, 255, 0.8); transform: translateZ(0); cursor: pointer; } .button.pressed { box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.6), 0px 0px 10px rgba(0, 0, 0, 0.4), 0px 0px 20px rgba(0, 0, 0, 0.2), inset 0 2px 2px rgba(255, 255, 255, 0.8), inset 0 -2px 2px rgba(255, 255, 255, 0.8); } .text { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); pointer-events: none; color: rgba(0, 0, 0, 0.9); font-size: 48px; font-weight: bold; text-shadow: 0px 2px 1px rgba(255, 255, 255, 0.3), 0px -2px 1px rgba(255, 255, 255, 0.3); } .text::selection { background-color: transparent; } .button .button-reflection { width: 100%; height: 100%; transform: scaleX(-1); object-fit: cover; opacity: 0.7; filter: blur(2px) saturate(0.6) brightness(1.1); object-position: 0 -100px; } </style> </head> <body> <div id="app"> <div v-show="cameraOpened" :class="`button-wrap ${buttonPressed ? 'pressed' : ''}`"> <div :class="`button ${buttonPressed ? 'pressed' : ''}`" @mousedown="setButtonPressed(true)" @mouseup="setButtonPressed(false)" @touchstart="setButtonPressed(true)" @touchend="setButtonPressed(false)"> <video class="button-reflection" ref="reflectionRef" /> </div> <div class="text">Button</div> </div> </div> <script> new Vue({ el: '#app', data: { cameraOpened: false, buttonPressed: false }, mounted() { const _this = this navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then((stream) => { const video = this.$refs.reflectionRef video.srcObject = stream; video.onloadedmetadata = () => { _this.cameraOpened = true video.play() } }) }, methods: { setButtonPressed(val) { this.buttonPressed = val } } }) </script> </body> </html>
總結(jié)
到此這篇關(guān)于vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個金屬拋光質(zhì)感按鈕的文章就介紹到這了,更多相關(guān)vue.js實(shí)現(xiàn)金屬拋光質(zhì)感按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于下拉類型多選組件Vue-Treeselect(鍵名轉(zhuǎn)換)
這篇文章主要介紹了關(guān)于下拉類型多選組件Vue-Treeselect(鍵名轉(zhuǎn)換),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07animate.css在vue項(xiàng)目中的使用教程
在vue項(xiàng)目中使用動畫其實(shí)有多種方式,可以使用vue中的過渡transition,可以使用animate動畫與transition配合使用,也可以單獨(dú)使用animate動畫庫,下面我們開始介紹在vue中單獨(dú)使用animate動畫,感興趣的朋友一起看看吧2018-08-08一步步從Vue3.x源碼上理解ref和reactive的區(qū)別
vue3的數(shù)據(jù)雙向綁定,大家都明白是proxy數(shù)據(jù)代理,但是在定義響應(yīng)式數(shù)據(jù)的時候,有ref和reactive兩種方式,如果判斷該使用什么方式,是大家一直不很清楚地問題,下面這篇文章主要給大家介紹了關(guān)于從Vue3.x源碼上理解ref和reactive的區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-02-02