懸停時放大影像

使用自訂程式碼為滑鼠懸停時的影像添加適合桌面和行動裝置的放大鏡。

You may have images in your website that are rich in intricate details — such as product photos or artwork. By using some custom code, you can enable your visitors to hover over and amplify specific parts of these images — all without having to move a muscle. 

An image of pink, red and green boxes and watches is magnified on hover using custom code placed into a page’s custom code settings in the Webflow Designer.

This tutorial will guide you through the process of enlarging your images by inserting custom code into a page or a project by following these steps: 

  1. Insert the custom code
  2. Assign a class name
  3. Customize the magnifier

<script type="text/javascript" src="https://assets.website-files.com/5e2755779d8b8ea1d6373797/5ea88e92dce9b4b66f165490_zoom.txt"></script>
<script>
  jQuery(function(){

  if(!$.fn.imagezoomsl){

      $('.msg').show();
      return;
    }
    else $('.msg').hide();

    // plugin initialization
    $('.image').imagezoomsl({

      innerzoommagnifier: true,
      classmagnifier: "round-loope",
      magnifiersize: [150, 150],
      // disables the scrolling of the document with the mouse wheel when the cursor is over the image
      disablewheel: false
    });
  });
</script>
    

Insert the custom code

There are two methods to insert the custom code into your project: 

  • Include the code in the entire project
  • Integrate the code in an individual page

To insert the code into your entire project:

  1. Access your project’s 設定
  2. 選擇 自訂程式碼 標籤
  3. Insert the code into the 頁尾碼 section (e.g., “Before </body> tag”)
  4. 點選 儲存變更

筆記: You can insert the code into your project using hotkeys 指令+V (在 Mac 上)或 控制+V (在 Windows 上)。

The Custom code tab is selected in a project’s settings to show where custom code across a project can be placed. 
The image magnification custom code should be placed in the Footer code section of the project’s settings, and Before the </body> 標籤。

To insert the code into an individual page: 

  1. 啟動 設計師
  2. 訪問 頁面面板 and select the page settings cog icon of the desired page
  3. 捲動到 自訂程式碼 section within the page settings
  4. Insert the code into the 前 </body> 標籤 部分
  5. 點選 節省

筆記: You can paste the code into your project using hotkeys 指令+V (在 Mac 上)或 控制+V (在 Windows 上)。

To access a page’s settings, open the Pages panel and hover over the page to see a cog icon to click. 
A page’s custom code settings are highlighted, specifically the Before </body> tag section.

You have successfully integrated the custom code into your project!

Assign a class name

Let’s assign a unique class name to the images that we want to apply the magnification effect to:

  1. 選擇 影像 you wish to magnify
  2. 訪問 風格面板 並點擊 選擇器字段
  3. Give it a 班級 (e.g., “Magnify”) 
The selector field of an image shows a new class of “Magnify” added to it.

Let’s include the “Magnify” class in our code: 

  1. 訪問 頁面面板 and select the page settings cog icon of the page where your custom magnifier code is placed
  2. 捲動到 前 </body> 標籤 section in your page’s settings
  3. Replace “.影像” with your class name (e.g., “.magnify”) 
  4. 點選 節省
After inserting the code for image enlargement into the custom code of the page settings, ensure to modify the code to correspond to the newly defined “Magnify” class.

有用的提示: Instead of replicating or copying classes in an ongoing project, consider using a Global class 或一個 Combo class to establish a unique class for images intended for magnification. Ensure that the distinct magnification class name has been included in your custom code according to the previously outlined steps. Discover more about Global classes and Combo classes.

Go ahead and publish your project and navigate to your live site to witness your personalized code in operation. Simply hover over the image where you’ve applied the class name (such as images identified with the class name “Magnify”), and observe a magnifying glass tracking your cursor movements as you glide it across the image.

Customize the magnifying tool

By default, the magnifying glass is a square measuring 150 by 150 pixels. Let’s modify its dimensions, shape, and introduce a drop shadow effect.

The default setting in our custom code specifies the magnifier size as 150 by 150 pixels. Let’s enlarge it:

  1. 訪問 頁面面板 and select the page settings cog icon on the page containing your tailored magnifying code.
  2. 向下捲動到 前 </body> 標籤 section within your page settings and pinpoint the “magnifiersize: [150, 150]” snippet of code.
  3. Adjust both 150 pixel values to a larger figure (e.g., 300 pixels for each).
  4. Save your changes.
To adjust the size of the magnifier, you must modify the pixel values in the magnifier size section of the custom code within the page’s settings.

Let’s transform the magnifier shape from a square to a circle by setting a foundational style on a fresh 分區塊:

  1. 打開 航海家 並選擇 身體 元素。
  2. 利用 快速尋找 to insert a 分區塊 as the concluding element on the page.
  3. 選擇 分區塊, launch the 風格面板, and click into the 選擇器字段.
  4. 指定一個類別名 分區塊 to enable the custom code to recognize the new magnifier shape (e.g., “round-loope”).
  5. 使用權 風格面板 > 邊框 and define a 半徑 of 50%.

At this stage, the Div block might appear oval, but the magnifier’s size is controlled by the 300 pixels you previously set.

內幕提示: 命令 + (在 Mac 上)或 控制 + (on Windows) to trigger 快速尋找 and promptly append elements to your project.

The Selector field of the styled Div block displays a novel class of “round-loope” appended to it.
The Border radius of the “round-loope” Div block is adjusted to 50%.

To introduce more depth to the magnifier, let’s include a shadow behind it:

  1. 選擇 分區塊.
  2. 訪問 風格面板 > 效果.
  3. 納入一個 盒子陰影 by selecting the plus icon and customize the shadow according to your preference.

Since our “round-loope” 分區塊 solely affects the styling of our magnifier, we do not want the 分區塊 to be visible on our page. Let’s conceal it from view:

  1. Select the “round-loope” 分區塊.
  2. 打開 風格面板 > 佈局.
  3. 調整 展示沒有任何.
  4. 發布 the project and explore your site to preview the newly styled magnifier.
An image showcasing pink, red, and green boxes and watches magnified upon hovering using custom code integrated into the page’s custom code settings within the Webflow Designer.

The magnifying tool also functions on mobile gadgets. Users can tap on the image to view close-ups, especially on devices like tablets and smartphones.

An image presenting pink, red, and green boxes and watches magnified on mobile devices by tapping on segmented areas of the image. This capability is achieved through custom code integrated into the page’s custom code settings in the Webflow Designer.

Congratulations! You’ve enhanced the potential of your images with magnification capabilities!

麥伊凡
Ewan Mak 的最新帖子 (看全部)