Solved

Autoplay a product video on product hover on collection page & product page.

MrGrif
Shopify Partner
22 2 4

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. 

MrGrif
Accepted Solutions (2)
pete_soc
Shopify Partner
13 1 6

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

 

 
and replace this code block with
 

 

.media.media--hover-effect > img + img,
.media.media--hover-effect > img + video {
  opacity: 0;
}

 

In component-card.css find

 

.card__media .media img

 

and replace code block with
 
 

 

.card__media .media img,
.card__media .media video {
height: 100%;
object-fit: cover;
object-position: center center;
width: 100%;
}

 

 
Find

 

card-wrapper:hover .media.media--hover-effect >img+img

 

 
and replace code block with 

 

.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);
}

 

 
In card-product.liquid search

 

motion-reduce

 

 you will see two image tags next to each other replace the second img tag with the following

 

 

{%- 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 -%}

 

 

View solution in original post

LVC
Tourist
6 1 6

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.

View solution in original post

Replies 16 (16)

Elissa_Paquette
Tourist
6 0 1

SAME!

pete_soc
Shopify Partner
13 1 6

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

 

 
and replace this code block with
 

 

.media.media--hover-effect > img + img,
.media.media--hover-effect > img + video {
  opacity: 0;
}

 

In component-card.css find

 

.card__media .media img

 

and replace code block with
 
 

 

.card__media .media img,
.card__media .media video {
height: 100%;
object-fit: cover;
object-position: center center;
width: 100%;
}

 

 
Find

 

card-wrapper:hover .media.media--hover-effect >img+img

 

 
and replace code block with 

 

.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);
}

 

 
In card-product.liquid search

 

motion-reduce

 

 you will see two image tags next to each other replace the second img tag with the following

 

 

{%- 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 -%}

 

 

trnngctu
Tourist
10 0 3

It works. Thank you very much

A new solopreneur
Shih-yin
Visitor
1 0 0

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"?

 

jellyfishprinta
Visitor
2 0 0

will this work on mobile? 

pete_soc
Shopify Partner
13 1 6

There is no hover on handheld devices

LVC
Tourist
6 1 6

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.

solindigital
Visitor
1 0 0

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?

pete_soc
Shopify Partner
13 1 6

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.  

spinnermation
Visitor
1 0 0

I am looking to do this as well, however I am using theme "Crave"  does this same code apply?

pete_soc
Shopify Partner
13 1 6

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.

RadionPoints08
Visitor
3 0 0

Is these work on Product card ??

 

 

xcsresources
Excursionist
13 0 3

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

ariel108
Visitor
3 0 0

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!

ariel108
Visitor
3 0 0

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). 

 

pete_soc
Shopify Partner
13 1 6

Improved and updated version for dawn 13 in a Gist here

https://github.com/peterbrunton/shopify-gists/blob/main/product-card-rollover-video