Hàm translateY() là một hàm dựng sẵn dùng để định vị lại phần tử theo trục dọc.
Cú pháp:
translateY( t )
Tham số: Hàm này chấp nhận tham số duy nhất là t. t giữ độ dài dịch chuyển tương ứng với trục y.
Các ví dụ dưới đây minh họa hàm translateY() trong CSS.
Ví dụ 1:
html<!DOCTYPE html>
<html>
<head>
<title>
CSS translateY() function
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
.translateY_image {
transform: translateY(100px);
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>CSS translateY() function</h2>
<h4>Original Image</h4>
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png"
alt="GeeksforGeeks logo">
<br>
<h4>Translated image</h4>
<img class="translateY_image"
src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png"
alt="GeeksforGeeks logo">
</body>
</html>
Đầu ra:
Ví dụ 2:
html<!DOCTYPE html>
<html>
<head>
<title>
CSS translateY() function
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
.GFG {
font-size: 35px;
font-weight: bold;
color: green;
}
.geeks {
transform: translateY(100px);
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>CSS translateY() function</h2>
<h4>Original Element</h4>
<div class="GFG">
Welcome to GeeksforGeeks
</div>
<h4>Translated Element</h4>
<div class="GFG geeks">
Welcome to GeeksforGeeks
</div>
</body>
</html>
Đầu ra:
Trình duyệt được hỗ trợ: Các trình duyệt được hỗ trợ bởi hàm translateY() được liệt kê bên dưới:
- Google Chrome 1
- Edge 12
- Internet Explorer 9
- Firefox 3.5
- Safari 3.1
- Opera 10.5