Shopify themes, liquid, logos, and UX
Hi,
Theme: Dawn 2.0 (Version 6.0)
I have a brand that provides short 9second clips of products worn on models.
Id like to achieve two things.
1) When a product is hovered, a secondary image appears, id like the video to be on the collection page as the secondary image that will autoplay on hover.
2) When a customer clicks the product, they need to click play to play the video, id prefer if the short clip was autoplayed.
Any ideas of where to begin in the code section to achieve the above ?
Ive tried placing off autoplay="true" but I can't seem to find where to place it as Im not seeing html for video in product page.
Solved! Go to the solution
This is an accepted solution.
This is possible with videos with no sound as your browser will block autoplay videos with sound.
For this solution, the product video needs to be the second media item of the product.
in base.css find
.media.media--hover-effect > img + img
.media.media--hover-effect > img + img,
.media.media--hover-effect > img + video {
opacity: 0;
}
.card__media .media img
.card__media .media img,
.card__media .media video {
height: 100%;
object-fit: cover;
object-position: center center;
width: 100%;
}
card-wrapper:hover .media.media--hover-effect >img+img
.card-wrapper:hover .media.media--hover-effect > img + img,
.card-wrapper:hover .media.media--hover-effect > img + video {
opacity: 1;
transition: transform var(--duration-long) ease;
transform: scale(1.03);
}
motion-reduce
{%- if card_product.media[1] != null and show_secondary_image -%}
{% liquid
assign alternateMediaIsVideo = false
if card_product.media[1].media_type == 'video'
assign alternateMediaIsVideo = true
endif
%}
{% unless alternateMediaIsVideo %}
<img
srcset="
{%- if card_product.media[1].width >= 165 -%}{{ card_product.media[1] | image_url: width: 165 }} 165w,{%- endif -%}
{%- if card_product.media[1].width >= 360 -%}{{ card_product.media[1] | image_url: width: 360 }} 360w,{%- endif -%}
{%- if card_product.media[1].width >= 533 -%}{{ card_product.media[1] | image_url: width: 533 }} 533w,{%- endif -%}
{%- if card_product.media[1].width >= 720 -%}{{ card_product.media[1] | image_url: width: 720 }} 720w,{%- endif -%}
{%- if card_product.media[1].width >= 940 -%}{{ card_product.media[1] | image_url: width: 940 }} 940w,{%- endif -%}
{%- if card_product.media[1].width >= 1066 -%}{{ card_product.media[1] | image_url: width: 1066 }} 1066w,{%- endif -%}
{{ card_product.media[1] | image_url }} {{ card_product.media[1].width }}w
"
src="{{ card_product.media[1] | image_url: width: 533 }}"
sizes="(min-width: {{ settings.page_width }}px) {{ settings.page_width | minus: 130 | divided_by: 4 }}px, (min-width: 990px) calc((100vw - 130px) / 4), (min-width: 750px) calc((100vw - 120px) / 3), calc((100vw - 35px) / 2)"
alt=""
class="motion-reduce"
loading="lazy"
width="{{ card_product.media[1].width }}"
height="{{ card_product.media[1].height }}"
>
{% else %}
{{
card_product.media[1]
| video_tag:
controls: false,
muted: true,
loop: true,
autoplay: true,
image_size: '1066x',
class: 'motion-reduce'
}}
{% endunless %}
{%- endif -%}
This is an accepted solution.
Awesome, dude!
Seeing that I'm not the only one trying to autoplay product video on collection pages, I took the liberty to slightly change the code so that it's possible to have product images and videos in any order.
This is an accepted solution.
Improved and updated version for dawn 13 in a Gist here
https://github.com/peterbrunton/shopify-gists/blob/main/product-card-rollover-video
SAME!
This is an accepted solution.
This is possible with videos with no sound as your browser will block autoplay videos with sound.
For this solution, the product video needs to be the second media item of the product.
in base.css find
.media.media--hover-effect > img + img
.media.media--hover-effect > img + img,
.media.media--hover-effect > img + video {
opacity: 0;
}
.card__media .media img
.card__media .media img,
.card__media .media video {
height: 100%;
object-fit: cover;
object-position: center center;
width: 100%;
}
card-wrapper:hover .media.media--hover-effect >img+img
.card-wrapper:hover .media.media--hover-effect > img + img,
.card-wrapper:hover .media.media--hover-effect > img + video {
opacity: 1;
transition: transform var(--duration-long) ease;
transform: scale(1.03);
}
motion-reduce
{%- if card_product.media[1] != null and show_secondary_image -%}
{% liquid
assign alternateMediaIsVideo = false
if card_product.media[1].media_type == 'video'
assign alternateMediaIsVideo = true
endif
%}
{% unless alternateMediaIsVideo %}
<img
srcset="
{%- if card_product.media[1].width >= 165 -%}{{ card_product.media[1] | image_url: width: 165 }} 165w,{%- endif -%}
{%- if card_product.media[1].width >= 360 -%}{{ card_product.media[1] | image_url: width: 360 }} 360w,{%- endif -%}
{%- if card_product.media[1].width >= 533 -%}{{ card_product.media[1] | image_url: width: 533 }} 533w,{%- endif -%}
{%- if card_product.media[1].width >= 720 -%}{{ card_product.media[1] | image_url: width: 720 }} 720w,{%- endif -%}
{%- if card_product.media[1].width >= 940 -%}{{ card_product.media[1] | image_url: width: 940 }} 940w,{%- endif -%}
{%- if card_product.media[1].width >= 1066 -%}{{ card_product.media[1] | image_url: width: 1066 }} 1066w,{%- endif -%}
{{ card_product.media[1] | image_url }} {{ card_product.media[1].width }}w
"
src="{{ card_product.media[1] | image_url: width: 533 }}"
sizes="(min-width: {{ settings.page_width }}px) {{ settings.page_width | minus: 130 | divided_by: 4 }}px, (min-width: 990px) calc((100vw - 130px) / 4), (min-width: 750px) calc((100vw - 120px) / 3), calc((100vw - 35px) / 2)"
alt=""
class="motion-reduce"
loading="lazy"
width="{{ card_product.media[1].width }}"
height="{{ card_product.media[1].height }}"
>
{% else %}
{{
card_product.media[1]
| video_tag:
controls: false,
muted: true,
loop: true,
autoplay: true,
image_size: '1066x',
class: 'motion-reduce'
}}
{% endunless %}
{%- endif -%}
It works. Thank you very much
It works in Dawn Theme, I recently changed the theme.
I cann't find the same code...How can I make it work in my new theme "Lorenza"?
will this work on mobile?
There is no hover on handheld devices
This is an accepted solution.
Awesome, dude!
Seeing that I'm not the only one trying to autoplay product video on collection pages, I took the liberty to slightly change the code so that it's possible to have product images and videos in any order.
Hi! It works great, thanks for the tip.
The issue I'm dealing with, it loads and plays all videos in the page in the background. Is it possible to make videos only load and play on hover and pause when the mouse isn't over the thumbnail?
Ya should be possible with a javascript function that pauses all videos then creates an on mouse-enter event on the product card that plays the video.
I am looking to do this as well, however I am using theme "Crave" does this same code apply?
I'm not familiar with the crave theme, but as a Shopify theme it should use Dawn as its base and therefore the class names should be the same. Easiest thing to do to check is to duplicate your theme and then follow the first part of each of the instructions, go to the file and search for the tag that will be replaced if all the tags are there id give it a shot, but if you are not familiar with code and you hit a snag you'll need a coder.
Is these work on Product card ??
The code definitely works to autoplay video OVER the main image. I'm looking for the video to play ONLY when hovering over the main image. Did anyone work this out?
Edited*
The code works. For some reason some of the coding didn't save so I had to redo it. Cheers
Hey Pete, thank you for sharing that. It works great on product cards, but unfortunately it doesn't work on product page. I still need to click play to play the video and I can see controls. Are you able to help me so that also works? I would appreciate it!
I managed to disable controls in product-thumbnail.liquid, but still I need to press play on video.
I tried to mute the video, because that could potentially block the autoplay, but still that wasn't it.
I noticed that it works on thumbnail carousel layout, but not on two columns layout (that I use on my store).
This is an accepted solution.
Improved and updated version for dawn 13 in a Gist here
https://github.com/peterbrunton/shopify-gists/blob/main/product-card-rollover-video
Fantastic! I added this.video.currentTime = 0; to the mouseleave and it works just how I want it to. Thanks!
It ruined my theme, all pictures disappeared on my Taste theme 😕
It's a different theme. You can revert your files.
There's a 'Recent changes' drop down in the code editor, just roll it back to the last save before you added the code that will work for sections and snippets.
You should make code changes to offline themes, so duplicate your live theme and work off that if everything works then publish that theme.
There's plenty of resources online for stuff like this.
Thiw worked great for me! Question: is there a way to have each video reset to the beginning when they are rolled off. At present the videos seem to be auto playing all the time, even when not rolled over. This creates the effect that sometimes when they are rolled over they are in the middle of the video, which is less that optimal. Thanks for any ideas!
the updated version here [https://github.com/peterbrunton/shopify-gists/blob/main/product-card-rollover-video]
plays on mouseenter and pauses on mouseleave
class RolloverVideo extends HTMLElement {
connectedCallback() {
this.video = this.querySelector('video');
if (this.video) {
this.video.muted = true;
const cardWrapper = this.closest('.card-wrapper');
cardWrapper.addEventListener('mouseenter', () => {
if (this.video.paused) {
this.video.play();
}
});
cardWrapper.addEventListener('mouseleave', () => {
this.video.pause();
});
}
}
}
customElements.define('rollover-video', RolloverVideo);
Thank you for the help. You're the DARK KNIGHT.
Thank You for the solution, it is really helpful, and it is fixed in one time only Thank You
I have been looking for it for a while. You are a savior.
We recently spoke with Zopi developers @Zopi about how dropshipping businesses can enha...
By JasonH Oct 23, 2024A big shout out to all of the merchants who participated in our AMA with 2H Media: Holi...
By Jacqui Oct 21, 2024We want to take a moment to celebrate the incredible ways you all engage with the Shopi...
By JasonH Oct 15, 2024