Make Your Site Respond when Elements Scroll into View

Hey guys, I’m back. I know it’s been a while.

This time we’re going to take a dip in the code pool, but don’t worry, we’ll stay in the shallow end.

Even if you don’t have a lot of experience with coding, there are some neat effects you can create using the Properties panel and just a line or two of code. In this post we’ll take a look at the onViewportEnter API. (You can look at the bottom of this post to see how little code you need to make this work.)

Like the API docs explain, onViewportEnter “Adds an event handler (just another way of saying it runs some code) that runs when an element is scrolled into the viewable part of the current window. An element enters the viewport when the page is scrolled to show any part of the element. An element enters the viewport even if it is hidden or collapsed.”

Simply put, onViewportEnter makes something happen when the element you add it to scrolls into view.

Let’s look at a simple use case. Suppose you have a repeater on your page, and you want it to display only 10 items at a time, but your collection has 100’s of items. You want to add a “Discover More” button at the bottom of the repeater which will trigger the dataset to load the next 10 items, but you want that button to fade in only when the user scrolls to the end of the repeater. Here’s what you would do.

  1. Make sure your dataset “Number of items to display” is set to 10.
  2. Make sure your button is set to “Hidden on load” in the Properties panel.
  3. Select your button and add an onClick event to it in the Properties panel. A function is added
    to your code panel for the button’s onClick event.
  4. In that function add this code “$w(”#yourDataset").loadMore();"
    a. This tells the dataset to load the next 10 items when the button is clicked.
    b. Make sure to replace “yourDataset” with the name of your actual dataset.
  5. Add an anchor to your page and position it over the button. We’re going to use this anchor
    to see when the button scrolls into view. We need to do this because the button starts off
    hidden so it’s never in view to start.
  6. Select the anchor and add an onViewportEnter event handler to it in the Properties panel. A
    function is added to your code panel for the onViewportEnter event.
  7. In that function add this code. “$w(”#button").show(“FadeIn”);"
    a. This makes the button display with the Fade In effect when the anchor scrolls into view.
    b. Make sure to replace “button” with the name of your actual button.

Now, when you scroll to the bottom of the repeater the Discover More button will fade in, which the user can click to load the next 10 items in the repeater.

Of course, you may want that button to disappear until you need it again. To do that, just add an onViewportLeave event handler to your anchor also, and add the code to make your button FadeOut. Now the button fades out when the repeater loads and will fade in again when the user scrolls down more.

Here’s what all the code looks like:

export function button1_click(event, $w) {
	$w("#dataset1").loadMore();
}
export function anchor1_viewportEnter(event, $w) {
	$w("#button1").show("FadeIn");
}
export function anchor1_viewportLeave(event, $w) {
	$w("#button1").hide("FadeOut");
}

And here it is in action:


Obviously, this is just one application of this API. You can make your site respond in any way you want when any element scrolls into view.

-Jeff

6 Likes

Hi,

I’ve set an infinite scrolling function (by automatically loading more items when the user reaches the bottom) so my question is: is it possible to disable automatic scrolling by clicking on a button, so if the users wishes to access the footer, he’s able to?

Hey Tristan,

Can you please post this in the regular Q&A? I’m happy to answer questions about my post, but I don’t have the bandwidth for more than that. My apologies.

-Jeff

Hi Jeff,

Thanks for this simple code. The only problem is that the button I added is greyed-out and not clickable.
Any idea how to fix this?

  • Michiel

Hey Michiel,

Sounds like you don’t have “Enabled by default” selected in the Properties panel. You need that selected, but Hidden on load not selected.

-Jeff

Thank you Jeff, that solved the greyed-out issue. I can click on it now, but it will not load the next items. Maybe the code isn’t correct?

Here’s a screenshot of the propertiespanel of the button.

Hi again Michiel,

I think you want that first checkbox selected, although I don’t know the translated string. If it means “Hidden on load” you need to select it. But that won’t solve your loading problem.

Just to double check - You set the dataset to display fewer items than you have in your collection, right?
If that’s not the problem, can you share your site URL with me because from what I can see the code looks ok.

-Jeff

Hi Jeff,

The URL is https://www.vandenboschverlichting.nl/sale. I set the dataset to display to 6 (while there are 20+ items).

  • Michiel

You added the onClick event to button1, but the button you want is button9.

THANK YOU JEFF! That is quite embarrassing… I changed it and now it works. :grimacing:

No worries, been there done that.

Hi Jeff,

I already did but still no answer :

Hi Jeff,
Is there Anyway to do this with Pro Gallery? i really need to post videos And images on the same but theres no field type working for both

Great performance! I just wonder about memory usage, mostly for users. Because I’ve got a lot of complaints about it. https://cozyhousetoday.com/best-splitting-maul-reviews - here it is, and they said not just once but couple of days, that visual items appears too slow for them. And it would be great to consider about prevention of memory usage leaks.

A little late to the party here so, sorry for that…

Jeff,

In essence, this could be applied to an Anchor menu, disregarding the button part of the code… right? My knowledge and understanding of code is next to nothing, so I’m looking for a copy/paste solution here :sweat_smile:

I want my anchor menu to appear at a certain point on the page, and then disappear if they scroll back up to that point. I had posted on here, and someone was kind enough to link the ViewPointEnter page, but my attempt pathetically failed.

Thanks in advance,

Dev

Update…

I just attempted to do this, and for some reason the Anchor Menu is not abiding by the “Hidden on Load” command. I unchecked and rechecked the box, saved, closed out, and reloaded Wix, and it is still fading into appearance when the page loads. I double checked the code to make sure there wasn’t anything random throne in (because I was messing with it prior to reading this thread).

I have an Anchor Menu attached to a Box for background & legibility purposes, so I added it in there where I assumed the code would go, not sure if that’s correct though.

Here is what my code looks like:

export function anchor6_viewportEnter(event, $w) {
    $w("#anchorMenu1").show("FadeIn");
    $w("#box15").show("FadeIn");
}

export function anchor7_viewportLeave(event, $w) {
    $w("#anchorMenu1").hide("FadeOut");
    $w("#box15").hide("FadeOut");
}

Here is a screenshot. The Anchor Menu is hidden in editor, but when I preview it appears… if you’d like to see what I mean go to https://www.devonbatista.com/communities It will fade into the top right corner as soon as the page loads.


Thank you! (again)

Hey @emaildevonbatista ,

I think the problem is that you’ve set the menu to show on viewportEnter for anchor6, but anchor6 is already in the viewport when the page loads, so the menu shows.

Viewport enter creates a reaction when a user scrolls something into view on the screen that was previously off-screen. Whatever code you put in that function (export function anchor6_viewportEnter) is going to run when that element is in view. In your case the anchor is in view when the page loads so your menu gets shown.

If you move the anchors that you want to control the menu visibility to somewhere “beneath the fold” - meaning to a place where users have to scroll to see them, you’ll see things working the way you want. You just need to figure out the exact placement of the anchors.

This should fix things. If not let me know and I may be able to drill down some more.

-Jeff