Published on

Unraveling the Mystery of the Before and After Image Hover Effect on Shopify

Authors
  • avatar
    Name
    Entaice Braintrust
    Twitter

Unraveling the Mystery of the Before and After Image Hover Effect on Shopify

My journey into the world of “before and after” image hovers began quite by accident. It was a particularly monotonous Tuesday afternoon when a serendipitous encounter with a friend's online poster shop sparked an epiphany. Her shop, unlike mine at the time, had this magical quality — a kind of digital enchantment. It invited you in, letting you hover over images to reveal enchanting transformations: before an artsy scribble, after a Picasso-worthy masterpiece. It was captivating. What sorcery was this? Or was it just clever use of CSS?

Today, let’s weave this digital tapestry together, unearthing the secrets behind this mesmerizing wizardry. Our mission? Equip your beloved Shopify sanctuary with a similar before and after image effect — the kind that makes you want to show off to unsuspecting neighbors and swoon-worthy admirers alike.

Unpacking the Magic: A Step-by-Step Guide

With a twinkle in our eye, let’s embark on this delightful trek. It’s easier than catching a butterfly, I promise.

Step 1: Gathering the Ingredients

Just as any good witch needs her potion supplies, we need our ingredients handy — two images of the same artifact. Let's call them OurArtifact-Before.jpg and OurArtifact-After.jpg. Label these images distinctly, and upload them to your Shopify store assets.

Step 2: Building the Canvas

Imagine this as creating a piece of art. Navigate to your Shopify admin panel and go to Online Store > Themes. Select the theme you want to edit and go for Actions > Edit Code.

Place our before and after images wisely in a new section. Copy-paste this snippet into a new section file (hover-image-effect.liquid) so Shopify can allocate it like the work of art it is:

<div class="before-after-container">
  <img src="{{ 'OurArtifact-Before.jpg' | asset_url }}" alt="Before" />
  <div class="after" style="background-image: url('{{ 'OurArtifact-After.jpg' | asset_url }}');">
    <div class="after-overlay"></div>
  </div>
</div>

Step 3: Engaging the Pensive Powers of CSS

A blueprint without color is but a whisper of its potential. The essence of our magic is CSS.

In the same theme editor, navigate to your theme’s CSS file (typically theme.scss.liquid or styles.scss.liquid) and clasp this beauty of a code:

.before-after-container {
  position: relative;
  overflow: hidden;
}

.before-after-container img {
  display: block;
  transition: opacity 0.5s ease;
}

.before-after-container .after {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-size: cover;
  width: 50%;
  transition: width 0.5s ease;
}

.before-after-container:hover .after {
  width: 100%;
}

Step 4: Summoning JavaScript Sorcery

Ah, but no spell is fully cast without a sprinkle of JavaScript. Go back to Shopify’s inner sanctum — that is, the theme editor — and edge your way to the end of your JS file (often theme.js).

Adopt this miracle-working code snippet:

document.querySelectorAll('.before-after-container').forEach((item) => {
  item.addEventListener('mousemove', (e) => {
    const rect = e.target.getBoundingClientRect()
    const offsetX = e.clientX - rect.left
    const width = rect.width
    const percentage = (offsetX / width) * 100

    e.target.querySelector('.after').style.width = percentage + '%'
  })
})

Success: Bask in Your Creation!

And there you have it, my friend. We’ve unearthed the secret to a charming Shopify store. A place where the mundane transforms into the spectacular with a reverent hover of the mouse — without clicking, without complex gestures, just pure sorcery for the sake of aesthetic splendor.

If you've followed along, your store should now inspire the same rapt attention that my friend’s shop first inspired in me. Suddenly, you've joined an underground legion of aesthetically blessed online shopkeepers.

Remember, like any good magic, the beauty lies in practice and subtlety. Adjust, tweak, and twiddle until it perfectly reflects your store's unique spirit.

And now, with a flourish of our digital wand, we leave you to explore this newfound power. But do tell us if you’ve woven even more beauty into this enchantment. The world is ours to enchant, one hover at a time.