Re: Make button scroll to top of page

Solved

Make button scroll to top of page

Viajey
New Member
5 0 0

I have a button in the image with text element, and I'd like it to scroll the user to the top of the page when clicked. I used this code before, but it only worked in the customize section, not on the actual website.

<style>

      html {

          scroll-behavior: smooth;

      }

    </style>

    <script>

      document.addEventListener('DOMContentLoaded', function() {

          let buttonScrollTop = document.querySelector('.button[data-shopify-editor-block*="button_PfN8Qi"]');

 

          if(buttonScrollTop) {

              buttonScrollTop.addEventListener('click', function(e){

                  e.preventDefault();

                  document.body.scrollTop = 0;

                  document.documentElement.scrollTop = 0;

              });

          }

      });

Accepted Solution (1)
Dan-From-Ryviu
Shopify Partner
9136 1828 1863

This is an accepted solution.

Simply add this value in the link of that button

#MainProduct-template--16290336473132__main

 

- Helpful? Like and Accept solution! ‌Support me: Buy coffee
- Ryviu - Reviews & QA app, collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy app: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

View solution in original post

Replies 8 (8)

MaverickStudio
Shopify Partner
101 32 34

Hey @Viajey ,

Welcome to the community!

Can you send over the link and the store password?

♥ If you found my solution helpful, please consider giving it a Like and marking it as the ✔ Accepted Solution


Try our WhatsApp Chat Button Widget with multipe agents.Supachat: WhatsApp Chat Button


A Product of Maverick Studio

Viajey
New Member
5 0 0

For sure. The website is viajey.com and the password is abc123

Dan-From-Ryviu
Shopify Partner
9136 1828 1863

Hi @Viajey 

Maybe you get an element of the editor, so it works only for the editor. Please check on live site and get correct element of button to make it work Screenshot 2024-07-31 at 09.02.10.png

- Helpful? Like and Accept solution! ‌Support me: Buy coffee
- Ryviu - Reviews & QA app, collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy app: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

Viajey
New Member
5 0 0

This is what the element is on the live website. How would I add this to my code?

<a role="link" aria-disabled="true" class="button button--primary">
Buy Now
</a>

Dan-From-Ryviu
Shopify Partner
9136 1828 1863

Could you please show me the code and indicate which section you would like to scroll to when the button is clicked?

- Helpful? Like and Accept solution! ‌Support me: Buy coffee
- Ryviu - Reviews & QA app, collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy app: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

Viajey
New Member
5 0 0

The elements are on this link, and the store password is abc123 

https://viajey.com/products/wrap-travel-pillow

The "Buy Now" button in the first image should be clicked to bring the user to the section in the second image.

Screen Shot 2024-08-01 at 8.29.22 AM.png

Screen Shot 2024-08-01 at 8.29.41 AM.png

Dan-From-Ryviu
Shopify Partner
9136 1828 1863

This is an accepted solution.

Simply add this value in the link of that button

#MainProduct-template--16290336473132__main

 

- Helpful? Like and Accept solution! ‌Support me: Buy coffee
- Ryviu - Reviews & QA app, collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy app: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

Viajey
New Member
5 0 0

it worked! Thank you!