变形 transform
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
</script>
</html>
过渡 transition
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
</script>
</html>
动画 animation
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
</script>
</html>
示例
大风车
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
margin: 100px auto;
position: relative;
border-radius: 50%;
background-color: pink;
animation: moveCircle 1s linear infinite paused;
}
@keyframes moveCircle {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#box div {
width: 150px;
height: 50px;
position: absolute;
top: 100px;
border-top-left-radius: 100%;
transform-origin: bottom right;
}
#box div:nth-of-type(1) {
background-color: red;
}
#box div:nth-of-type(2) {
background-color: green;
transform: rotate(90deg);
}
#box div:nth-of-type(3) {
background-color: deepskyblue;
transform: rotate(180deg);
}
#box div:nth-of-type(4) {
background-color: yellow;
transform: rotate(270deg);
}
input {
width: 200px;
height: 50px;
display: block;
margin: 50px auto;
font-size: 30px;
background-color: orange;
}
</style>
</head>
<body>
<div id="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<input type="button" value="开始" />
<script type="text/javascript">
var btn = document.querySelector("input");
var box = document.querySelector("#box");
btn.onclick = function() {
if (this.value == '开始') {
this.value = '结束';
box.style.animationPlayState = 'running';
} else {
this.value = '开始';
box.style.animationPlayState = 'paused';
}
}
</script>
</body>
</html>
立方体
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
margin: 100px auto;
position: relative;
transform-style: preserve-3d;
animation: moveRect 2s linear infinite paused;
}
@keyframes moveRect {
from {
transform: perspective(1200px) rotateX(0) rotateY(0);
}
to {
transform: perspective(1200px) rotateX(360deg) rotateY(360deg);
}
}
#box div {
width: 300px;
height: 300px;
position: absolute;
font-size: 50px;
font-weight: bold;
text-align: center;
line-height: 300px;
background-color: orange;
}
#box div:nth-of-type(1) {
background-color: red;
transform: translateZ(150px);
}
#box div:nth-of-type(2) {
background-color: green;
transform: translateZ(-150px);
}
#box div:nth-of-type(3) {
background-color: deepskyblue;
transform: rotateX(90deg) translateZ(150px);
}
#box div:nth-of-type(4) {
background-color: deeppink;
transform: rotateX(-90deg) translateZ(150px);
}
#box div:nth-of-type(5) {
background-color: yellow;
transform: rotateY(90deg) translateZ(150px);
}
#box div:nth-of-type(6) {
background-color: yellow;
transform: rotateY(-90deg) translateZ(150px);
}
input {
width: 200px;
height: 50px;
display: block;
margin: 50px auto;
font-size: 30px;
background-color: orange;
}
</style>
</head>
<body>
<div id="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
<input type="button" value="开始" />
<script type="text/javascript">
var btn = document.querySelector("input");
var box = document.querySelector("#box");
btn.onclick = function() {
if (this.value == '开始') {
this.value = '结束';
box.style.animationPlayState = 'running';
} else {
this.value = '开始';
box.style.animationPlayState = 'paused';
}
}
</script>
</body>
</html>
钟表
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
margin: 100px auto;
border: 5px solid black;
border-radius: 50%;
position: relative;
}
#box .kedu {
width: 30px;
height: 150px;
position: absolute;
top: 0;
left: 135px;
text-align: center;
transform-origin: bottom center;
}
#hour {
width: 14px;
height: 90px;
position: absolute;
background-color: black;
left: 145px;
top: 60px;
transform-origin: bottom center;
}
#minute {
width: 12px;
height: 110px;
position: absolute;
background-color: deepskyblue;
left: 145px;
top: 40px;
transform-origin: bottom center;
}
#second {
width: 10px;
height: 130px;
position: absolute;
background-color: deeppink;
left: 145px;
top: 20px;
transform-origin: bottom center;
}
</style>
</head>
<body>
<div id="box">
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
<script type="text/javascript">
var box = document.querySelector("#box");
for (var i = 0; i < 12; i++) {
var tempKeDuDiv = document.createElement('div');
tempKeDuDiv.className = 'kedu';
var tempKeDuP = document.createElement('p');
tempKeDuP.innerHTML = (i == 0) ? 12 : i;
tempKeDuDiv.appendChild(tempKeDuP);
tempKeDuDiv.style.transform = 'rotate(' + i * 30 + 'deg)';
tempKeDuP.style.transform = 'rotate(' + (-i * 30) + 'deg)';
box.appendChild(tempKeDuDiv);
}
var hourDiv = document.querySelector('#hour');
var minuteDiv = document.querySelector('#minute');
var secondDiv = document.querySelector('#second');
var date = new Date();
var hour;
var minute;
var second = date.getSeconds();
function setPointers() {
second++;
date.setSeconds(second);
hour = date.getHours() % 12;
minute = date.getMinutes();
second = date.getSeconds();
console.log(hour + " : " + minute + " : " + second);
secondDiv.style.transform = 'rotate(' + second * 6 + 'deg)';
minuteDiv.style.transform = 'rotate(' + minute * 6 + 'deg)';
hourDiv.style.transform = 'rotate(' + (hour * 30 + minute * 0.5) + 'deg)';
}
setPointers();
setInterval(setPointers, 1000);
</script>
</body>
</html>