55 phút
CSS Animations & Transitions
CSS Transitions
transition property
.element {
transition: property duration timing-function delay;
}
.button {
transition: all 0.3s ease-in-out;
/* Shorthand for:
transition-property: all;
transition-duration: 0.3s;
transition-timing-function: ease-in-out;
transition-delay: 0s; */
}
transition properties
.box {
width: 100px;
height: 100px;
background: blue;
transition: width 0.5s ease, height 0.5s ease, background 0.3s ease;
}
.box:hover {
width: 200px;
height: 200px;
background: red;
}
timing functions
.element {
transition-timing-function: ease; /* default */
transition-timing-function: ease-in;
transition-timing-function: ease-out;
transition-timing-function: ease-in-out;
transition-timing-function: linear;
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1);
}
CSS Animations
@keyframes
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.element {
animation: slideIn 0.5s ease-out;
}
animation properties
.element {
animation-name: slideIn;
animation-duration: 1s;
animation-timing-function: ease-in-out;
animation-delay: 0.5s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both;
animation-play-state: running;
}
animation shorthand
.element {
animation: slideIn 1s ease-in-out 0.5s infinite alternate both;
}
Advanced Animations
Multiple animations
.element {
animation:
slideIn 0.5s ease-out,
fadeIn 0.8s ease-in 0.2s both;
}
Step animations
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
.typing-animation {
animation: typing 3s steps(40, end);
}
3D transforms
.card {
transform: perspective(1000px) rotateY(0deg);
transition: transform 0.6s ease;
}
.card:hover {
transform: perspective(1000px) rotateY(180deg);
}
Performance Considerations
Hardware acceleration
.animate {
/* Sử dụng transform và opacity cho hiệu suất tốt nhất */
transform: translateZ(0);
will-change: transform;
}
Properties to animate
/* Good for performance */
transform: translateX(100px);
transform: scale(1.2);
opacity: 0.5;
/* Bad for performance */
width: 200px;
height: 200px;
margin-left: 100px;
Practical Examples
Loading animation
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
.loading-dot {
animation: bounce 1s infinite ease-in-out;
}
.loading-dot:nth-child(2) {
animation-delay: 0.1s;
}
.loading-dot:nth-child(3) {
animation-delay: 0.2s;
}
Hover effects
.button {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.button:hover {
transform: translateY(-2px) scale(1.05);
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}