- This topic has 17 replies, 5 voices, and was last updated 3 years, 4 months ago by
Leo.
-
AuthorPosts
-
December 1, 2020 at 11:42 am #1561819
Frans
Hi,
Is there already a Generateblocks block that mimics a section with parallax-effect of the background image? I have a page with sections and created columns with LightweightGridColumns. Can I replace LWGC and the sections with GenerateBlocks ?
Thanks !
Frans
December 1, 2020 at 1:33 pm #1562135Tom
Lead DeveloperLead DeveloperHi there,
Not at this time. However, you can easily implement your own parallax script and target a custom class with it that you can then add to your Container blocks.
You can absolutely replace Lightweight Grid Columns with GenerateBlocks – it’s 1000x better 🙂
December 2, 2020 at 3:33 am #1563350Frans
Thanks ! Can you recommend a parallax script ? I found a css solution at w3schools.com, but it does not work on mobile, whereas the section solution of GPP does work on mobile.
Is there another css solution ? Javascript solution as a hook ?
Thanks !
Frans
December 2, 2020 at 11:10 am #1564332Tom
Lead DeveloperLead DeveloperThis is one solution:
function your_parallax_element( selector, context ) { context = context || document; var elements = context.querySelectorAll( selector ); return Array.prototype.slice.call( elements ); } window.addEventListener( "scroll", function() { var scrolledHeight= window.pageYOffset; your_parallax_element( ".YOUR-PARALLAX-CLASS-HERE" ).forEach( function( el, index, array ) { var limit = el.offsetTop + el.offsetHeight; if( scrolledHeight > el.offsetTop && scrolledHeight <= limit ) { el.style.backgroundPositionY = ( scrolledHeight - el.offsetTop ) / 2 + "px"; } else { el.style.backgroundPositionY = "0"; } }); });You just need to update
YOUR-PARALLAX-CLASS-HEREwith the class you choose for your Containers.December 2, 2020 at 12:54 pm #1564486Frans
Thanks Tom, but maybe I use this script in the wrong way: it does not work.
I added the script in a GPP Element as hook: tried wp_head, wp_footer
Changed YOUR-PARALLAX-CLASS-HERE in the script
Went to the container (on a page), added my parallax class in the CSS Classes or Element ID
Tried div and section in the Element Tag box
Added my parallax class to the Extra CSS, but did not know what to add in the classThanks !
Frans
December 3, 2020 at 3:39 am #1565404David
StaffCustomer SupportHi there,
JS should go in the
wp_footerhookLets say we want to use
your-parallaxas our class.
This line of the JS becomes this:your_parallax_element( ".your-parallax" ).forEach( function( el, index, array ) {Then select the Container Block, go to Advanced -> Additional CSS Class(es) and add:
your-parallaxDecember 3, 2020 at 11:55 am #1566305Frans
Thanks ! It works fine !
I think my mistake: I didn’t put the
<script>..</script>tag in the hook element around the above script. Sorry !Frans
December 4, 2020 at 8:53 am #1567522David
StaffCustomer SupportGlad to hear you found the issue!
January 24, 2021 at 2:40 am #1631130Rafał
Hi there!
How could it work??
Wherehero.parallaxcomes from in the code?January 24, 2021 at 5:03 am #1631254David
StaffCustomer SupportHi there,
can you explain the issue you’re having ?
January 24, 2021 at 8:24 am #1631586Rafał
Hi David,
Tom’s JS doesn’t work for me.
I wonder what’s going on the line:
el.style.backgroundPositionY = ( scrolledHeight - el.offsetTop ) / hero.parallax + "px";
hero.parallax
??January 24, 2021 at 3:48 pm #1631917Tom
Lead DeveloperLead DeveloperAh, try replacing
hero.parallaxwith a number like2. That should fix it.January 25, 2021 at 1:42 am #1632350Rafał
Thanks, Tom!
I haven’t figured out it’s just a placeholder for “speed” factor 😉
But, there’s still some issue: it does not start changing background position until element touches the top edge of the screen.Added
var viewportHeight = $(window).height();
then
if ( scrolledHeight > el.offsetTop - viewportHeight && scrolledHeight <= limit )
allows to start effect while element appears on the screen.January 25, 2021 at 5:25 am #1632598Rafał
I’ve tried to deal with similar effect on an image, taking advantage of
object-fit: cover;, andobject-positionproperty, setting percentage values for Y axis.
Example HTML:<figure class="wp-block-image size-full PARALLAX-CLASS-HERE"> <img …/>… </figure>My code (with logic from Goran Mottram / Stackoverflow):
(function($) { $(window).scroll(function() { setParalax( $('.PARALLAX-CLASS-HERE') ); }); function setParalax( elements ) { elements.each(function(i, element) { var scrollTop = $(window).scrollTop(); var viewportHeight = $(window).height(); var elementHeight = $(this).height(); var offsetTop = $(this).offset().top; var lowerBound = offsetTop - viewportHeight; var upperBound = offsetTop + elementHeight; var screenScope = (scrollTop - lowerBound) / (upperBound - lowerBound) * 100; $(element).find('img').css("object-position", "50% " + screenScope + "%"); }); }; })(jQuery);Working! 🙂
Though I’m not good at JS. Could you improve its logic or performance?January 26, 2021 at 9:11 am #1634500Tom
Lead DeveloperLead DeveloperThat looks good to me! Performance somewhat goes out the window when it comes to parallax, but at least the code is minimal 🙂
-
AuthorPosts
- The topic ‘Can sections already be replaced by GenerateBlocks ?’ is closed to new replies.