Shopify Dawn Disable Autoscroll On Variant Change

July 18th, 2022





While implementing a scroll-spy for a client, the page kept scrolling to the top.

Initially, I figured it had been caused by code I wrote but after removing all my code I quickly realized this was built-in behavior.

The function to remove is in setActiveMedia on line 47 of media-gallery.js in the assets folder of the theme.

if (!this.elements.thumbnails || this.dataset.desktopLayout === 'stacked') {
    activeMedia.scrollIntoView({behavior: 'smooth'});
}

Comment this line out and it will no longer scroll automatically when a new variant is selected.

The function commented out.

The function commented out.

This worked for me in Dawn 3.0.0 – 5.0.0 and likely works above those versions as well.


Comments

6 thoughts on "Shopify Dawn Disable Autoscroll On Variant Change"

  1. Erik says:

    This works on Craft as well. Thank you!

    1. joeybab3 says:

      Awesome happy to hear they’ve reused that snippet!

  2. Franklin says:

    Legend.

  3. Vladimir says:

    Thank you. Was driving me crazy. When selecting a variant the screen was jumping all over the place.

    1. joeybab3 says:

      It was so irritating to find the solution which is why I made this post in the first place haha

  4. Gabriel Devero says:

    love you

Leave a Reply

Your email address will not be published. Required fields are marked *