CSS過渡、動畫、變換,綜合例項
阿新 • • 發佈:2019-02-05
過渡是出去後不會回來,動畫是可以回來的,當然也可以不回來,動畫功能比較多
一、過渡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>過渡練習2</title>
<style type="text/css">
.class1{
width:100px;
height: 100px;
background-color : aqua;
}
.class1:hover{/*代表滑鼠觸發後的變換*/
width:200px;
height:200px;
background-color: brown;
transition-delay: 200ms;
transition-duration: 1000ms;
transition-timing-function: ease;
}
</style>
</head>
<body >
<p class="class1"></p>
</body>
</html>
二、動畫
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阿水的阿里</title>
<style type="text/css">
.class1{
width:100px;
height: 100px;
background-color : bisque;
}
.class1:hover{
animation-delay: 200ms;/*延遲時間*/
animation-duration: 1000ms;/*動畫總時間*/
animation-iteration-count: 4;/*動畫來回次數*/
animation-direction: alternate;/*有去有回*/
animation-name: ashui;
}
@keyframes ashui {/*這裡to是必須的,代表最後的狀態,其他都是中間過程*/
from{
width:100px;
height: 100px;
background-color: bisque;
}
50%{
width:150px;
height: 150px;
background-color: aquamarine;
}
75%{
width: 240px;
height: 240px;
background-color: aliceblue;
}
to{
width:300px;
height:300px;
background-color: burlywood;
}
}
</style>
</head>
<body>
<div align="center">
<p class="class1"></p>
</div>
</body>
</html>
這裡動畫注意:@keyframs 自定義名字{}裡面還有要to{},自定義名字在hover中animation-name:自定義名字,裡面定義
三、變換
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>變換練習</title>
<style type="text/css">
.class1{
width: 100px;
height: 100px;
background-color: brown;
}
.class1:hover{
width:100px;
height: 100px;
background-color: brown;
transform: rotate(45deg);/*轉動的角度*/
transform-origin: top right;/*繞哪點轉,若要繞重心轉,這句就不用寫了*/
}
</style>
</head>
<body>
<p class="class1"></p>
</body>
</html>
注意:變換使用transform, transform-rotate(45deg);代表轉的角度,順逆時針,用數的正負即可,transform-origin:top right;代表繞哪點轉,若繞重心轉,則這句不用寫了
綜合例項
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阿水的阿里</title>
<style type="text/css">
div{
text-align: center;
}
.class1{
width: 300px;
height: 300px;
border-style: solid;
border-radius: 20px/20px;
margin-top:130px ;/*與div上邊距相差畫素*/
}
html:hover .class1{
animation-delay: 200ms;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-name: ashui;
}
@keyframes ashui {
to{
width: 40px;
height: 40px;
margin-top: 0px;
}
}
p{
font-family: 微軟雅黑;
font-size: 40px;
text-shadow: 1px 1px 2px #b0b0b0;
opacity:1;
}
html:hover p{
transition-delay: 200ms;
transition-duration: 1s;
font-family: 微軟雅黑;
font-size: 10px;
text-shadow: 1px 1px 2px #b0b0b0;
opacity: 0;
}
</style>
</head>
<body>
<div>
<img src="github1.jpg" class="class1">
<p>阿水的阿里</p>
</div>
</body>
</html>
例項使用了前面的很多內容,有一點注意:html:hover .class{}和html:hover p{},代表滑鼠指標觸及網頁頁面就會變化,因為我這裡用p標籤和img很難同時實現,所以這樣省時省力。