You might have images in your site that are filled with intricate details — for instance, product photos or artwork. With a little custom code, you can give your visitors the power to hover over and magnify portions of these images — and they won’t even have to lift a finger to do it.
In this lesson you’ll learn how to magnify your images by adding custom code to a page or a project doing the following:
- Paste the custom code
- Add a class name
- Style the magnifier
Paste the custom code
There are two ways you can paste the custom code into your project:
- Add the code to the entire project
- Add the code to an individual page
To paste the code into your entire project:
- Go to your project’s Settings
- Choose the Custom code tab
- Paste the code into the Footer code section (e.g., “Before </body> tag”)
- Click Save changes
Note: You can paste the code into your project using shortcut keys Command + V (on Mac) or Control + V (on Windows).
To paste the code into an individual page:
- Open the Designer
- Open the Pages panel and click the page settings cog icon of the page you want to affect
- Scroll down to the Custom code section of the page settings
- Paste the code into the Before </body> tag section
- Click Save
Note: You can paste the code into your project using shortcut keys Command + V (on Mac) or Control + V (on Windows).
You've successfully pasted the custom code into your project!
Add a class name
Let’s add a unique class name to the images we want to receive the magnification effect:
- Select the Image you want to magnify
- Open the Style panel and click into the Selector field
- Give it a class (e.g., “Magnify”)
Let’s add the “Magnify” class to our code:
- Open the Pages panel and click the page settings cog icon of the page containing your custom magnifier code
- Scroll to the Before </body> tag section in your page’s settings
- Replace “.image” with your class name (eg., “.magnify”)
- Click Save
Pro tip: Instead of recreating or duplicating classes within an existing project, you can use a Global class or a Combo class to create a unique class for images you want to magnify. Just make sure your unique magnification class name has been added to your custom code following the steps above. Read more about Global classes and Combo classes.
Publish your project and visit your live site to see your custom code in action. Hover over the image you’ve added the class name to (e.g., images with the class name “Magnify”) and you’ll see a magnifier following your mouse position as you move it over the image.
Style the magnifier
By default, the magnifier is a 150 by 150 pixel square shape. Let’s change its size and shape, and give it a drop shadow.
Out of the box, our custom code sets the magnifier size to 150 by 150 pixels. Let’s make it larger:
- Open the Pages panel and click the page settings cog icon of the page containing your custom magnifier code
- Scroll to the Before </body> tag section in your page’s settings and locate the “magnifiersize: [150, 150]” section of code
- Change each 150 pixel value to a larger value (e.g., 300 pixels for each)
- Click Save
Let’s change the magnifier shape from a square to a circle by setting a style foundation on a new Div block:
- Open the Navigator and select the Body element
- Use Quick find to add a Div block as the last element on the page
- Select the Div block, open the Style panel and click into the Selector field
- Give the Div block a class name so the custom code can recognize the new magnifier shape (e.g., “round-loope”)
- Open Style panel > Borders and set a Radius of 50%
The Div block may appear oval at this point, but the custom code controls the size of the magnifier — the 300 pixels you set earlier.
Pro tip: Press Command + E (on Mac) and Control + E (on Windows) to open Quick find and quickly add elements to your project.
To give the magnifier more dimension, let’s add a drop shadow behind it:
- Select the Div block
- Open Style panel > Effects
- Add a Box shadow by clicking the plus icon, and style the shadow to your liking
Because our “round-loope” Div block is only being used to affect our magnifier’s styling, we don’t actually want the Div block to be visible on our page. Let’s hide it from view:
- Select the “round-loope” Div block
- Open Style panel > Layout
- Set Display to none
- Publish the project and visit your site to preview the newly styled magnifier
The magnifier also works on mobile devices. You can tap on the image to get a closer look when using devices like tablets and smartphones.
Congratulations! You’ve just magnified your images’ possibilities!