CSS文字懸停

文字懸停

參考自 https://blog.csdn.net/qq449245884/article/details/105326186

效果

代碼

HTML

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./main.css">
</head>
<body>
<div class="container">
<div class="box">
<div class="img-box">
<img src="https://picsum.photos/400/300?random=1">
</div>
<div class="content-box">
<div class="content">
<h3>LoremLorem</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam libero.</p>
</div>
</div>
</div>
<div class="box">
<div class="img-box">
<img src="https://picsum.photos/400/300?random=2">
</div>
<div class="content-box">
<div class="content">
<h3>LoremLorem</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam libero.</p>
</div>
</div>
</div>

</div>
</body>
</html>

CSS

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
72
73
74
75
76
77
78
79
80
81
82
body {
margin: 0;
padding: 0;
height: 100vh;
font-family: "Arial", "Microsoft", "YaHei", "黑體", "宋體", "sans-serif";

display: flex;
justify-content: center;
align-items: center;
}
* {
background-origin: border-box;
}
.container {
display: flex;
justify-content: space-around;
margin: 0 auto;

min-width: 1200px;
}

.box {
position: relative;
width: 400px;
height: 300px;
}

.img-box {
position: absolute;
left: 0;
top: 0;

width: 100%;
height: 100%;
}
.img-box img {
width: 100%;
}
.content-box {
width: 100% ;
position: absolute;
top: 40px;
right: 40px;
bottom: 40px;

background-color: rgba(0, 0, 0, 0.8);
transition: transform 0.5s;
transform-origin: right;
transform: scaleX(0);

/* 變成從上到下 */
/* transform: scaleY(0);
transform-origin: bottom; */

}
.box:hover .content-box {
transform: scaleX(1);
transform-origin: left;

/* 變成從上到下 */
/* transform: scaleY(1);
transform-origin: top; */
}
.content-box .content {
padding: 20px 60px;
opacity: 0;
}

.box:hover .content-box .content{
opacity: 1;
transition: all 0.5s 0.3s;
}

.box .content h3 {
color: #FFF;
font-weight: 500;
font-size: 2em;
}
.box .content p {
color: #fff;
font-weight: 300;
}

CSS講解

示意圖

透過在box定義相對定位,並在content-box以及img-box設定絕對定位來讓它們重和

重要代碼塊

  1. 當hover到box時要讓content-box營造從左到右出現並在消失是從右邊收合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    .content-box {
    transition: transform 0.5s;
    /*將元素以開窗戶的方式顯現(如果沒有設定origin的話)*/
    transform: scaleX(0);
    /*消失是在右邊*/
    transform-origin: right;
    }
    .box:hover .content-box {
    /*將元素以開窗戶的方式顯現(如果沒有設定origin的話)*/
    transform: scaleX(1);
    /*消失是在左邊*/
    transform-origin: left;
    }
  2. 當hover到的時候再讓文字塊顯現(使用opacity)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    設置延遲時間,讓用戶產生打開才顯現的感覺.content-box .content {
    opacity: 0;
    }

    .box:hover .content-box .content{
    opacity: 1;
    /*設置延遲時間,讓用戶產生打開才顯現的感覺*/
    transition: all 0.5s 0.3s;
    }