- This topic has 9 replies, 7 voices, and was last updated 2 years, 10 months ago by
Fernando.
-
AuthorPosts
-
March 27, 2021 at 8:13 pm #1712517
webyogi
Hi,
First, big thank you for the wonderful suggestion to switch to the Gutenberg Editor, once I got used it to, I realized how easy and fast it is to build things.
Ok so I am building a website using generatepress and generateblocks. For one of the pages I want to implement parallax effect. Now I know I can do it using sections but can I achieve that by using blocks? I I could not find any option in blocks under background to setup Parallax Effect.
How do I achieve Parallax Effect using blocks?
Thanks!
March 28, 2021 at 10:25 am #1713130Tom
Lead DeveloperLead DeveloperHi there,
We don’t have a parallax option by default at the moment as it requires adding javascript.
However, you can implement your own javascript quite easily. For example: https://codepen.io/Prachl/pen/jjKzEy
So, for example, you can add this as a
wp_footer
hook:<script> window.addEventListener('scroll', throttle(parallax, 14)); function throttle(fn, wait) { var time = Date.now(); return function() { if ((time + wait - Date.now()) < 0) { fn(); time = Date.now(); } } }; function parallax() { var scrolled = window.pageYOffset; var parallax = document.querySelector(".parallax"); // You can adjust the 0.4 to change the speed var coords = (scrolled * 0.4) + 'px' parallax.style.transform = 'translateY(' + coords + ')'; }; </script>
Then add this custom CSS:
.parallax { overflow-y: hidden; }
Then add
parallax
as a custom class to the block you want to enable parallax on.Hope this helps 🙂
April 12, 2021 at 10:46 pm #1732009webyogi
Worked like a charm!
April 13, 2021 at 7:35 am #1732812Tom
Lead DeveloperLead DeveloperAwesome 🙂
July 16, 2021 at 11:31 am #1858365GAMUT
This fix works well on desktop across browsers, however it fails to load on mobile. What can be done to make this snippet work across mobile as well?
July 16, 2021 at 2:08 pm #1858475David
StaffCustomer SupportHi there,
can you start a new topic where you can share a link to the site – we can take a look.
August 3, 2022 at 9:50 am #2302249Antonio
Hello,
I’ve tried this workaround without success.I have this structure:
[CONTAINER 1]
[CONTAINER 2]
[CONTAINER 3]
[CONTAINER 4]
etc.If I add the .parallax class to [CONTAINER 2], then I see a that container vertically aligned centered in the page (and a white area in the initial position of the container).
I’ve seen the code and [CONTAINER 2] gets the following transformation.
element.style {
transform: translateY(3921.2px);
}Unfortunately the site is actually running on my local machine, so I can’t share you the link.
Any suggestion?
Thank you
August 3, 2022 at 7:52 pm #2302690Fernando Customer Support
Hi Antonio,
It would be hard to assess the issue without seeing the site.
It would be best to reach out to a developer with regards to this.
Hope this clarifies!
November 7, 2022 at 8:43 am #2405055Lars
You can also manipulate only the background position of the image:
function parallax() { var scrolled = window.pageYOffset; var parallax = document.querySelector(".parallax"); // You can adjust the 0.4 to change the speed let offset = window.pageYOffset; parallax.style.backgroundPositionY = offset * 0.7 + "px"; };
November 7, 2022 at 5:00 pm #2405512Fernando Customer Support
Thank you for sharing Lars!
-
AuthorPosts
- You must be logged in to reply to this topic.