vue.js如何在网页中实现一个金属抛光质感的按钮
作者:饺子大魔王的男人 时间:2024-04-28 09:21:26
前言
诶?这有一个按钮(~ ̄▽ ̄)~,这是一个在html中实现的具有金属质感并且能镜面反射的按钮~
效果
电脑效果
手机效果
说明
主要思路是使用 navigator.mediaDevices.getUserMedia()
调用设备摄像头来实现镜面反射,css部分用到了filter
属性,后期可以根据需要调整它的属性值,做出更多反射效果。有了思路,实现起来还是比较简单的。
需要注意的是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>
来源:https://blog.csdn.net/u010726809/article/details/129584419
标签:vue.js,金属抛光质感,按钮
0
投稿
猜你喜欢
超链“确认”对话框confirm
2008-05-16 11:42:00
sqlserver bcp(数据导入导出工具)一般用法与命令详解
2024-01-27 03:12:08
Python设计模式之建造者模式实例详解
2021-07-13 17:37:03
Python实现EXCEL表格的排序功能示例
2021-05-17 20:50:11
YUI3.3.0 中 transition 事件的变化
2011-06-16 20:51:45
Vue父子组建的简单通信之控制开关Switch的实现
2024-06-05 09:16:25
使用vux实现上拉刷新功能遇到的坑
2024-04-28 09:25:18
Python实现随机取一个矩阵数组的某几行
2021-10-04 16:45:52
给页面加上Loading效果最简单实用的办法
2008-11-20 11:58:00
python编程开发之类型转换convert实例分析
2023-03-24 05:48:06
用Python解决x的n次方问题
2022-02-02 06:30:10
Python通过类的组合模拟街道红绿灯
2021-04-13 02:00:24
django的ORM模型的实现原理
2022-08-26 00:57:33
python多线程高级锁condition简单用法示例
2023-10-31 16:11:23
python中的十大%占位符对应的格式化的使用方法
2022-04-28 08:54:21
Python常用正则表达式符号浅析
2022-03-25 08:08:36
python中正则的使用指南
2023-09-10 18:24:51
再谈Python中的字符串与字符编码(推荐)
2023-06-15 23:25:08
python将txt等文件中的数据读为numpy数组的方法
2022-04-20 14:08:10
javaScript让文本框内的最后一个文字的后面获得焦点实现代码
2024-04-16 08:57:55