Để tạo điểm nhấn trong thiết kế website thì việc sử dụng hiệu ứng phóng to thu nhỏ bằng CSS là quan trọng
Trong nhiều dự án thiết kế web của mình, chắc chắn các bạn sẽ gặp phải dùng hiệu ứng phóng to hoặc thu nhỏ một phần tử HTML nào đó, bài viết này sẽ hướng dẫn các bạn cách sử dụng thuộc tính transform: scale[giá trị] trong CSS để thực hiện điều đó.
Phóng to, thu nhỏ sử dụng thuộc tính transform: scale[giá trị] trong CSS
Mình sẽ hướng dẫn các bạn cách tạo hiệu ứng phóng to và thu nhỏ cho một khối phần tử DIV. Để tạo hiệu ứng phóng to thu nhỏ chúng ta cần tập trung vào hai yếu tố “chiều rộng” và “chiều cao”. Đầu tiên xác định chiều rộng chiều cao thích hợp của một phần tử.chiều rộng” và “chiều cao”. Đầu tiên xác định chiều rộng chiều cao thích hợp của một phần tử.
Thêm Css vào phần tử HTMLChúng ta cần sử dụng @keyframes để xác định animation
Chúng ta cần sử dụng @keyframes để xác định animation
HTML
Sau đây là code css để tạo hiệu ứng:
CSS
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là:
giá trị < 1: Thu nhỏ đối tượng.giá trị = 1: Giá trị mặc định của đối tượng.giá trị > 1: Phóng to đối tượng.
giá trị = 1: Giá trị mặc định của đối tượng.
giá trị > 1: Phóng to đối tượng.
Bây giờ chúng ta sẽ đi vào cách tạo hiệu ứng hover zoom effect bằng một ví dụ khác nhé
HTML
Hiệu ứng Zoom
Bạn hãy hover vào thẻ div bên dưới
Sau đây là code css để tạo hiệu ứng:
CSS
.zoom {
padding: 50px;
background-color: DodgerBlue;
transition: transform .2s;
width: 200px;
height: 200px;
margin: 0 auto;
}
/*Hiệu ứng zoom*/
.zoom:hover {
transform: scale[1.5];
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là:
giá trị < 1: Thu nhỏ đối tượng.giá trị = 1: Giá trị mặc định của đối tượng.giá trị > 1: Phóng to đối tượng.
HTML
Hiệu Ứng Zoom In
Bạn hãy hover vào ảnh bên dưới nhé
CSS
.noidung {
height: 200px;
width: 200px;
overflow: hidden;
}
.zoomin img {
height: 200px;
width: 200px;
-webkit-transition: all 2s ease;
-moz-transition: all 2s ease;
-ms-transition: all 2s ease;
transition: all 2s ease;
}
.zoomin img:hover {
transform: scale[1.5];
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là:
giá trị < 1: Thu nhỏ đối tượng.giá trị = 1: Giá trị mặc định của đối tượng.giá trị > 1: Phóng to đối tượng.
HTML
Hiệu Ứng Zoom Out
Bạn hãy hover vào ảnh bên dưới nhé
CSS
.noidung {
height: 200px;
width: 200px;
overflow: hidden;
}
.zoomout img {
height: 300px;
width: 300px;
-webkit-transition: all 2s ease;
-moz-transition: all 2s ease;
-ms-transition: all 2s ease;
transition: all 2s ease;
}
.zoomout img:hover {
height: 200px;
width: 200px;
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là:
giá trị < 1: Thu nhỏ đối tượng.giá trị = 1: Giá trị mặc định của đối tượng.giá trị > 1: Phóng to đối tượng.
HTML
Hiệu Ứng Zoom Rotate
Bạn hãy hover vào ảnh bên dưới nhé
CSS
.noidung {
height: 200px;
width: 200px;
overflow: hidden;
}
.zoom_rotate img {
height: 300px;
width: 300px;
transition: transform .5s ease-in-out;
}
.zoom_rotate img:hover {
transform: scale[1.8] rotate[-30deg];
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là:
HTML
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
0Sau đây là code css để tạo hiệu ứng:
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
1CSS
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
HTML
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
2CSS
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
3.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
.zoom-in-zoom-out {
margin: 24px;
width: 50px;
height: 50px;
background: green;
animation: zoom-in-zoom-out 2s ease-out infinite;
}
@keyframes zoom-in-zoom-out {
0% {
transform: scale[1, 1];
}
50% {
transform: scale[1.5, 1.5];
}
100% {
transform: scale[1, 1];
}
}
Trong khi sử dụng thuộc tính transform: scale[giá trị] trong CSS thì với giá trị là: