當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


CSS translateY()用法及代碼示例

translateY()函數是一個內置函數,用於沿垂直軸重新定位元素。

用法:

translateY( t )

參數:該函數接受單個參數t,該參數保持對應於y軸的平移長度。


以下示例說明了CSS中的translateY()函數:

範例1:

<!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>

輸出:

範例2:

<!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>

輸出:

支持的瀏覽器:translateY()函數支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • 蘋果瀏覽器
  • Opera


相關用法


注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 CSS | translateY() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。