Product Slider for Dawn Theme - No apps or subscript- Just a little bit of code

{% if youLike === true %} Hit Subscribe {% endif %}

Did you use Dawn theme and was disappointed that they do not have a product slider?
Don't worry. In this tutorial, we will create one.
  1. Go to your Online store > Themes > Actions > Edit code
  2. Go to Section folder and open the main-product.liquid
  3. Find <slider-component> block and comment it out or delete it if you want

Comment it out by placing “<!--” before the slider-component and “-->” after the slider-component. Code should look like below

 <!-- <slider-component>                                              </slider-component> -->

   

      4. Before the commented out <slider-component>, add the code below

  <div class="slider-container" >
    <a class="skip-to-content-link button visually-hidden" href="#ProductInfo-{{ section.id }}">
{{ "accessibility.skip_to_product_info" | t }}
   </a>
    <ul class="large-image" id="large-image">
        {%- for media in product.media -%}
        <li class="large-image-item product__media-item grid__item slider__slide
{% if media.media_type != 'image' %} hide-modal {% endif %}"
data-media-id="{{ section.id }}-{{ media.id }}">
{% render 'product-thumbnail', media: media, position: forloop.index, loop: section.settings.enable_video_looping, modal_id: section.id, xr_button: true %}
       </li>
       {%- endfor -%}
    </ul>

<slider-component >
   <ul class="product-slider-box slider" role="list">

 {%- assign variant_images = product.images| where: 'attached_to_variant?', true | map: 'src' -%}

{%- for media in product.media -%}
      <li class="product-slider slider__slide" onclick="newLargeImage(this)" data-media-id="{{ section.id }}-{{ media.id }}"

{% if section.settings.hide_variants and variant_images contains media.src %} style="display: none;" {% endif %}>

{% if media.media_type == 'video' %}
          <div class="video-btn" style="display: block" >
             <span class="product__media-icon motion-reduce v-btn" aria-hidden="true">
{%- liquid
case media.media_type
when 'video' or 'external_video'
render 'icon-play'
when 'model'
render 'icon-3d-model'
else
render 'icon-zoom'
endcase
-%}
            </span>
     <img class="slide-image"
src="{{ media.preview_image | img_url: 'large' }}"
alt="{{ thumbnailAlt }}">
         </div>
{% else %}
     <img class="slide-image"
src="{{ media.preview_image | img_url: 'large' }}"
alt="{{ thumbnailAlt }}">
{% endif %}
   </li>
{% endfor %}
</ul>
<div class="slider-buttons {% if product.media.size < 5 %}hidden{% endif %}" style="display: flex">
   <button type="button" class="slider-button slider-button--prev bigger-slider" name="previous" aria-label="{{ 'accessibility.previous_slide' | t }}">{% render 'icon-caret' %}</button>
      <div class="slider-counter caption">
           <span class="slider-counter--current">1</span>
           <span aria-hidden="true"> / </span>
           <span class="visually-hidden">{{ 'accessibility.of' | t }}</span>
           <span class="slider-counter--total">{% if section.settings.hide_variants %}{{ product.media.size | minus: variant_images.size | plus: 1 }}{% else %}{{ product.media.size }}{% endif %}</span>
      </div>
    <button type="button" class="slider-button slider-button--next bigger-slider" name="next" aria-label="{{ 'accessibility.next_slide' | t }}">{% render 'icon-caret' %}</button>
    </div>
   </slider-component>
</div>



       5. On the top part, paste the code below, and click SAVE

  <script>

window.onload = (event) => {
document.querySelector("li:nth-child(1) > img").classList.add('active-thumb');
};

      function newLargeImage(x) {
            let clickedImage = x.dataset.mediaId;
            const newImage = document.querySelector(`[data-media-id="${clickedImage}"]`);
            const parentData = newImage.parentElement;

            parentData.prepend(newImage);
            newImage.style.display = "block";

document.querySelectorAll('.slide-image')
.forEach(thumb => thumb. classList.remove('active-thumb'));
const sliderComp = document.querySelector("slider-component");
document.querySelector(`[data-media-id="${clickedImage}"] > img`).classList.add('active-thumb');
};

</script>

 

       6. I see that the component-slider.css is also being use for multi-column section, therefore, we have to create a new .css folder. Go to Asset folder, click 'create new asset', create blank. Name the folder 'product-slider'. Folder should show like below

 product-slider.css

 

     7. Now open, the created CSS folder and paste the code below.

 .slider-container {
margin: 0 auto;
width: 100%;
overflow: auto;
display: flex;
flex-flow: column wrap;
align-items: center;
position: relative;
}

.large-image {
width: 100%;
position: relative;
overflow:hidden;
}


.large-image-item {
max-width: 100%;
min-width: 50%;
max-height: 100%;
padding: 0;
}

.large-image-item:not(:first-child){
display: none !important;
}

ul {
padding-inline-start: 0px !important;
}

slider-component {
position: relative;
display: block;
width:100%;
}

.slider__slide {
scroll-snap-align: start;
flex-shrink: 0;
}

.product-slider-box {
flex-wrap: inherit;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
margin-bottom: 1rem;
width: 100%;
display: flex;
}

.product-slider {
height: 100%;
width: 23%;
display: inline-block;
}

.product-slider:not(:first-child){
margin-left: 1rem;
}

.slide-image {
width: 100%;
height: 100%;
object-fit: cover;
}

.slider {
scrollbar-color: rgb(var(--color-foreground)) rgba(var(--color-foreground), 0.04);
-ms-overflow-style: none;
scrollbar-width: none;
}

.slider::-webkit-scrollbar {
height: 0.4rem;
width: 0.4rem;
display: none;
}

.no-js .slider {
-ms-overflow-style: auto;
scrollbar-width: auto;
}

.no-js .slider::-webkit-scrollbar {
display: initial;
}

.slider::-webkit-scrollbar-thumb {
background-color: rgb(var(--color-foreground));
border-radius: 0.4rem;
border: 0;
}

.slider::-webkit-scrollbar-track {
background: rgba(var(--color-foreground), 0.04);
border-radius: 0.4rem;
}

.slider-counter {
margin: 0 1.2rem;
}

.slider-buttons {
display: flex;
align-items: center;
justify-content: center;
}

.slider-button {
color: rgba(var(--color-foreground), 0.75);
background: transparent;
border: none;
cursor: pointer;
width: 44px;
height: 44px;
}

.slider-button:not([disabled]):hover {
color: rgb(var(--color-foreground));
}

.slider-button .icon {
height: 0.6rem;
}

.slider-button[disabled] .icon {
color: rgba(var(--color-foreground), 0.3);
}

.slider-button--next .icon {
margin-right: -0.2rem;
transform: rotate(-90deg) translateX(0.15rem);
}

.slider-button--prev .icon {
margin-left: -0.2rem;
transform: rotate(90deg) translateX(-0.15rem);
}

.slider-button--next:not([disabled]):hover .icon {
transform: rotate(-90deg) translateX(0.15rem) scale(1.07);
}

.slider-button--prev:not([disabled]):hover .icon {
transform: rotate(90deg) translateX(-0.15rem) scale(1.07);
}

/* video button when image is a video type */
.video-btn {
width: 100%;
height: 100%;
position: relative;
}

.v-btn {
top: 2% !important;
left: 2% !important;
}

.hide-modal modal-opener {
display: none;
}

.active-thumb {
opacity: 1 !important;
border: solid 2px var(--gradient-base-accent-1);
box-sizing: border-box;
}

.slide-image {
opacity: .5;

}


    8. Next, go back to the main-product.liquid. At the top part, comment out this code. You can also just delete it. To comment it out, place (<!-- ) before the code and (--->) after. Just like we did in the slider-component. Code should look like this.

 <!--{{ 'component-slider.css' | asset_url | stylesheet_tag }}-->

add this after. 

 {{ 'product-slider.css' | asset_url | stylesheet_tag }}

 

    9.  We need to go to the Asset folder again, and open global.js. Find the slider-component. Since slider component is a global code, meaning, some section also uses the same code. We have to add an if statement. Please disregard the video regarding this section in global.js.

At the SliderComponent constructor add the code below.

 this.productSlider = this.querySelector('.product-slider-box');

   10. At the initPages function we need to change the totalPages. Replace the code below:

this.totalPages = sliderItemsToShow.length - this.slidesPerPage + 1;

 with this

  if (this.productSlider) { this.totalPages = sliderItemsToShow.length - this.slidesPerPage + 4} else {
this.totalPages = sliderItemsToShow.length - this.slidesPerPage + 1;
}

    10.  Lastly, at the update function we need to change the currentPage, change the 1 into 4. Replace the code below, and click SAVE

this.currentPage = Math.round(this.slider.scrollLeft / this.sliderLastItem.clientWidth) + 1;

 to this

if (this.productSlider) {this.currentPage = Math.round(this.slider.scrollLeft / this.sliderLastItem.clientWidth) + 4} else {
this.currentPage = Math.round(this.slider.scrollLeft / this.sliderLastItem.clientWidth) + 1;
}

 

The global.js code should look like this:

 

That's it! You just transform the product page of Dawn theme. Let me know if you have questions or having issues with the code so we can fix it and have an updated code. (",)

 

Update 09/22/21: I end up changing the the code in the Asset/global.js so we do not break the slider component if we use it in the other section

I change the CSS code and remove the resize observer. Large-image will be the same height with the large-image-item.

Update 09/23/21: Issues of having two large image and slider missing fixed. Updated code to use feature hide_variants 

Update 09/25/21: Fix the issue with thumbnail disappears when adding a section that uses a slider-component. Updated the code in the step #4.

Update 09/26/21: Added a video play button when thumbnail image is a video

Update 09/30/21: Fix the issue regarding the video button. Reposition the codes for the video-btn and added some css codes

 Update 10/01/21: Fix the issue of stacking large images in mobile. I added a resizeObserver in the Javascript to fix the issue of big gaps between the large-image and the slider. If you don't want it you just delete the code

Update 10/03/21: Added overflow: hidden to large-image css. I would recommend not to have product images with extreme height as your first variant image. Also added some CSS so when active, thumbnail image will have border box and opacity is 1. See code in green. Thumbnail border color is dependent on your var(--gradient-base-accent-1)

Update 10/04/21: Change the v-btn in the product-slider.css file due to issue when having a mobile video

 Update 10/10/21: I know I fix the issue regarding the space between the large image and the slider. I did remove the resizeObserver and also the padding-bottom of the large image to fix this. I updated the code

Update 10/11/21: I change how the border and opacity is applied. Please see the highlighted green code

Update 10/15/21: I added an onload function so when the window loaded the first image in the thumbnail will get the active property. Code is pink. 

Also added a code for those who still want the option to hide the variants when variants selected. (NOTE: this is the original code from Dawn). See code in blue.

30 comentários

Hi,
Thanks for this imp article.

But color variant image change feature does not work.

Any suggestions ?

Thanks

Test den

How would you make the images in the slider the same size / ratio of the first picture so they all show uniform.

Rochelle Harris

@Ikhlaque I hope you follow the global.js code instruction
@Larry Evans I do have this https://made4uo.com/blogs/dawn-theme-tweaks-and-trick/convert-dawn-to-debut-product-page. Dots for me is redundant to be honest when it comes to product page. Since you are calling stuff three times already, adding the dots would potentially have risk on breaking the code
@Barry Somebody was asking me that, stay tuned
@Syam Sundar. Please message me in “Chat with us.” I can do the code for you

Made4Uo

It did not work for me. Please help

Shyam Sundar

I am also looking for next and previous buttons for slideshow which is on top of the main image. Is this an easy implementation?

Barry

Deixe um comentário