# 按钮悬停效果
# 快速导航
# 示例效果
# 背景变色
<div class="button"><span>hover me to change</span></div>
1
.button {
margin:40px auto;
width:200px;
height:60px;
padding:0 30px;
line-height: 60px;
text-align: center;
position: relative;
appearance: none;
background: #f72359;
border: none;
color: white;
font-size: 1.2em;
cursor: pointer;
outline: none;
overflow: hidden;
border-radius: 100px;
}
.button span {
position: relative;
}
.button::before {
--size: 0;
content: '';
position: absolute;
left: var(--x);
top: var(--y);
width: var(--size);
height: var(--size);
background: radial-gradient(circle closest-side, #4405f7, transparent);
transform: translate(-50%, -50%);
transition: width .2s ease, height .2s ease;
}
.button:hover::before {
--size: 400px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
document.querySelector('.button').addEventListener('mousemove', function (e) {
const x = e.pageX - this.offsetLeft
const y = e.pageY - this.offsetTop
this.style.setProperty('--x', `${ x }px`)
this.style.setProperty('--y', `${ y }px`)
})
1
2
3
4
5
6
7
2
3
4
5
6
7
hoverBg
<style scoped lang="scss">
.button {
margin: 40px auto;
width: 200px;
height: 60px;
padding: 0 30px;
line-height: 60px;
text-align: center;
position: relative;
appearance: none;
background: #f72359;
border: none;
color: white;
font-size: 1.2em;
cursor: pointer;
outline: none;
overflow: hidden;
border-radius: 100px;
}
.button span {
position: relative;
}
.button::before {
--size: 0;
content: '';
position: absolute;
left: var(--x);
top: var(--y);
width: var(--size);
height: var(--size);
background: radial-gradient(circle closest-side, #4405f7, transparent);
transform: translate(-50%, -50%);
transition: width .2s ease, height .2s ease;
}
.button:hover::before {
--size: 400px;
}
</style>
<template>
<div>
<div class="button"><span>hover背景渐变</span>
</div>
</div>
</template>
<script>
export default {
name: 'hoverBg',
data() {
return {}
},
mounted() {
document.querySelector('.button').addEventListener('mousemove', function (e) {
const x = e.pageX - this.offsetLeft
const y = e.pageY - this.offsetTop
this.style.setProperty('--x', `${ x }px`)
this.style.setProperty('--y', `${ y }px`)
})
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 边框动画
<div class="button">
<div class="button__content">边框动画</div>
</div>
1
2
3
2
3
.btn-container{
padding:10px;
.button{
width:200px;
height:60px;
position: relative;
background: #fff;
margin:30px auto;
box-sizing: border-box;
cursor: pointer;
text-align: center;
line-height: 60px;
&::before{
content:'';
width:0;
height:0;
background: #00adb5;
position: absolute;
top:-1px;right:-1px;
z-index: 0;
transition: width .5s,height .5s;
}
&::after{
content:'';
width:0;
height:0;
background: #00adb5;
position: absolute;
bottom:-1px;left:-1px;
z-index: 0;
transition: width .5s,height .5s;
}
&:hover::before{
width:calc(100% + 2px);
height:calc(100% + 2px);
}
&:hover::after{
width:calc(100% + 2px);
height:calc(100% + 2px);
}
.button__content{
height:100%;
width:100%;
position: absolute;
left:0;top:0;
z-index: 1;
background: #fff;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
边框动画
<style scoped lang="scss">
.btn-container{
padding:10px;
.button{
width:200px;
height:60px;
position: relative;
background: #fff;
margin:30px auto;
box-sizing: border-box;
cursor: pointer;
text-align: center;
line-height: 60px;
&::before{
content:'';
width:0;
height:0;
background: #00adb5;
position: absolute;
top:-1px;right:-1px;
z-index: 0;
transition: width .5s,height .5s;
}
&::after{
content:'';
width:0;
height:0;
background: #00adb5;
position: absolute;
bottom:-1px;left:-1px;
z-index: 0;
transition: width .5s,height .5s;
}
&:hover::before{
width:calc(100% + 2px);
height:calc(100% + 2px);
}
&:hover::after{
width:calc(100% + 2px);
height:calc(100% + 2px);
}
.button__content{
height:100%;
width:100%;
position: absolute;
left:0;top:0;
z-index: 1;
background: #fff;
}
}
}
</style>
<template>
<div class="btn-container">
<div class="button">
<div class="button__content">边框动画</div>
</div>
</div>
</template>
<script>
export default {
name: 'hoverBorder',
data() {
return {}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71