首頁  >  文章  >  web前端  >  如何透過懸停在圖像或圖標上來翻譯它?

如何透過懸停在圖像或圖標上來翻譯它?

王林
王林轉載
2023-08-31 15:13:021177瀏覽

如何透過懸停在圖像或圖標上來翻譯它?

在網頁開發中,互動性是提供令人難忘的使用者體驗的關鍵。常用的技術是在圖像或圖示上懸停以顯示更多資訊或改變外觀。透過懸停在圖像或圖示上翻譯是為您的網站增添一些動態和趣味的好方法。

在本文中,我們將學習如何在懸停時翻譯圖像或圖示。為了完成這個任務,我們將學習使用僅限HTML和CSS的不同方法。

在懸停時翻譯圖像或圖示的不同方法

方法1:CSS過渡效果

透過使用CSS過渡,可以實現在懸停時翻譯圖像或圖示的第一種方法。 CSS過渡用於平滑地改變屬性值,例如在懸停在一個元素上時等等。使用過渡,可以指定動畫的持續時間和時間函數。

文法

以下是使用CSS過渡來轉換影像或圖示的語法。

<img  src="your-image.jpg" class="trans-image" alt="如何透過懸停在圖像或圖標上來翻譯它?" >
<style>
   .trans-image {
      transition: transform 0.3s ease-in-out;
   }
   .trans-image:hover {
      transform: translateX(20px);
   }
</style>

Example

在下面的範例中,我們使用了一個帶有類別名為「trans-image」的圖片標籤。在CSS部分,我們將過渡屬性設為“transform”,持續時間為0.3秒,並使用“ease-in-out”的緩動函數。當我們懸停在元素上時,如果是圖像,則將transform屬性設為向右平移30像素,如果是圖標,則向右平移20像素。

<html>
<head>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
   <style>
      .translate-image {
         transition: transform 0.7s ease-in-out;
      }
      .translate-image:hover {
         transform: translateX(30px);
      }
      #icon {
         transition: transform 0.7s ease-in-out;
      }
      #icon:hover {
         transform: translateX(20px);
      }        
   </style>
</head>
<body>
   <h2>Translating image and icon using CSS Transitions</h2>
   <p> Hover over the below image or icon to see the transition </p>
   <!-- Translating image on hover using CSS transitions -->
   <img  src="https://www.tutorialspoint.com/static/images/logo.png?v2" class="translate-image" alt="如何透過懸停在圖像或圖標上來翻譯它?" >
   <br>
   <!-- Translating icon on hover using CSS transitions -->
   <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" />
</body>
</html>

方法二:CSS動畫

將圖像或圖示在懸停時進行翻譯的第一種方法是使用CSS動畫。 CSS允許使用HTML來對元素進行動畫處理,而無需使用JavaScript或Flash。在這裡,我們可以根據需要更改任意數量的CSS屬性,任意次數。

要使用CSS動畫,我們首先必須為動畫指定一些關鍵影格。關鍵影格確定元素在某些時間點上的樣式。使用動畫可以讓我們創造比過渡更複雜和動態的效果。

文法

以下是使用CSS動畫來轉換圖像或圖示的語法。

<i class="your-icon"></i>
<style>
   .your-icon {
      display: inline-block;
      width: 50px;
      height: 50px;
      background-color: #ccc;
      animation: translate 0.3s ease-in-out;
   }
   .your-icon:hover {
      animation-name: translate-hover;
   }
   @keyframes translate {
      from {
         transform: translateX(0);
      }
      to {
         transform: translateX(10px);
      }
   }
   @keyframes translate-hover {
      from {
         transform: translateX(10px);
      }
      to {
         transform: translateX(20px);
      }
   }
</style>

Example

在下面的範例中,我們使用了一個class為"icon"的"i"標籤和一個class為"image"的如何透過懸停在圖像或圖標上來翻譯它?標籤。在這裡,我們將display屬性設定為"inline-block"。我們也將animation屬性設為"translate",持續時間為0.3秒,緩動函數為"ease-in-out"。現在當我們懸停時,透過使用關鍵影格將動畫名稱設為"translate-hover",將圖示和圖片向右移動10像素,然後在後續懸停時向右移動20像素。

<html>
<head>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
   <style>
      .image {
         display: inline-block;
         width: 100px;
         height: 100px;
         animation: translate 0.3s ease-in-out;
      }
      .image:hover {animation-name: translate-hover;}
      #icon {
         display: inline-block;
         width: 100px;
         height: 100px;
         animation: translate 0.3s ease-in-out;
      }
      #icon:hover {animation-name: translate-hover;}
      @keyframes translate {
         from {transform: translateX(0);}
         to {transform: translateX(10px);}
      }
      @keyframes translate-hover {
         from {transform: translateX(10px);}
         to {transform: translateX(20px);}
      }
   </style>
</head>
<body>
   <h2>Translating image and icon using CSS Animations</h2>
   <p> Hover over the imgae orr icon to see the effect</p>
   <!-- Translating image on hover using CSS Animations -->
   <img  src="https://fastly.picsum.photos/id/213/200/300.jpg?hmac=t-54teMEgFL3q9WPaRq2t7YdGCU9aIRw77OCaHlSVRs" class="image" alt="如何透過懸停在圖像或圖標上來翻譯它?" > <br>
   <!-- Translating icon on hover using CSS Animations -->
   <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" />
</body>
</html>

方法三:CSS Grid

透過使用CSS網格,將圖像或圖示在懸停時進行翻譯的第一種方法是。 CSS網格使用基於網格的佈局系統,具有行和列,使得設計網頁更容易,而無需使用浮動和定位。在這裡,我們使用grid-row和grid-column屬性指定網格項目的位置,然後對要翻譯的網格項目套用CSS transform屬性,例如旋轉或平移。

文法

以下是使用CSS網格來轉換影像或圖示的語法。

<div class="grid-container">
   <img  src="your-image.jpg" class="trans-image" alt="如何透過懸停在圖像或圖標上來翻譯它?" >
</div>
<style>
   .grid-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 1fr);
      grid-gap: 10px;
   }
   .trans-image {
      grid-row: 2 / 3;
      grid-column: 2 / 3;
      transition: transform 0.3s ease-in-out;
   }
   .trans-image:hover {
      grid-column: 3 / 4;
      transform: translateX(10px);
   }
</style>

Example

In the below example, we have defined a "div" tag with a class of "container". Here, in CSS we have set the display property to "grid", and define the grid template with three columns and three rows , each with a fraction unit of 1. To transform the image and icon, we have used the transition property to "transform" with a duration of 0.3 seconds and an easing function of "ease-in-out” which when hovered trans the image or icon 10 pixels to the right.

<html>
<head>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
   <style>
      .image {
         grid-row: 2 / 3;
         grid-column: 2 / 3;
         transition: transform 0.3s ease-in-out;
      }
      .image:hover {
         grid-column: 3 / 4;
         transform: translateX(10px);
      }
      #icon {
         grid-row: 2 / 3;
         grid-column: 2 / 3;
         transition: transform 0.3s ease-in-out;
      }
      #icon:hover {
         grid-column: 3 / 4;
         transform: translateX(10px);
      }
   </style>
</head>
<body>
   <div>
      <h2>Translating image and icon using CSS Grid</h2>
      <p> Hover over the image or icon to see the effect </p>
      <!-- Translating image on hover using CSS Grid -->
      <img  src="https://www.tutorialspoint.com/static/images/logo.png?v2" class="image" alt="如何透過懸停在圖像或圖標上來翻譯它?" >
      <br>
      <!-- Translating icon on hover using CSS Grid -->
      <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" />
   </div>
</body>
</html>

結論

將互動性添加到我們的網站可以增強用戶體驗,而實現這一目標的方法是在懸停時翻譯圖像或圖示。這種效果可以使用HTML和CSS實現,有不同的方法可以實現,例如使用CSS過渡或動畫或網格。所有這些方法都允許我們指定動畫的持續時間和時間函數,並創建動態效果。使用這些技術,我們可以創建一個更具吸引力的網站,讓您的訪客留下深刻的印象。

以上是如何透過懸停在圖像或圖標上來翻譯它?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除