悬停时放大图像

使用自定义代码为鼠标悬停时的图像添加适合桌面和移动设备的放大镜。

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 上)或 Control + 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 上)或 Control + 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 classCombo 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 的最新帖子 (查看全部)