Velo Example: Coding a More Robust Custom Element
With Velo, you can add special behaviors and additional functionality to custom elements that you added to your site. First you create and code the element and its behavior in a JavaScript file. Then implement how your site should handle events on the custom element with Velo.
This article assumes here you know a bit about designing elements with CSS properties in JavaScript and working with web components. This article provides an overview of the code to get you started, providing links and pointers along the way.
What This Article Will Demonstrate
In this article, we'll use this company logo as our custom element:
In this example, we'll learn how to change the logo's behavior when visitors perform actions on our site, such as making the logo spin, resize, and shake.
Display a Custom Element
Review this basic example to learn how to display a logo. The example displays the logo without any additional changes to the logo's behavior.
Note: This article provides basic instructions for creating custom elements. For full instructions and examples, see the MDN documentation.
Here is the sample code provided in the basic example:
1
To review:
- We created variables for the different parts of the logo custom element. We separated the logo into two images so that we can manipulate each one separately.
- We used the
connectedCallback()
function to display the logo images when the page is connected and ready. - We registered the custom element with
customElements.define()
.
Add the Custom Element to Your Site
Because this example uses Velo, we can use it to host our custom element.
- In the Public & Backend section of the Code sidebar (Wix Studio), or the Velo sidebar (Wix Editor), upload the the custom element's JavaScript file to the Public > custom-elements folder.
- Add the custom element to our page.
- In Settings, choose Velo File and select the file you uploaded.
Make sure the custom element displays on the site.
Modify the Behavior of the Custom Element
Now we'll update our custom element so that it behaves differently when site visitors interact with the page.
For example, if a visitor scrolls down, the logo spins, and the text gets smaller until it disappears.
![](https://d2x3xhvgiqkx42.cloudfront.net/12345678-1234-1234-1234-1234567890ab/76096845-8b12-44f1-91f6-3dc2e838fdd9/2019/11/05/4e5d6f94-407b-4ef0-9a38-82843a204d77/1d492955-09e6-48e6-8872-b469fec943ff.gif)
We can also set up the custom element to behave differently if a site visitor interacts with some other element on the page.
For example, if a visitor hovers over a different element on the page, such as a menu, the logo shakes.
Implement the Behaviors on Your Server
This article assumes here you know a bit about designing elements with CSS properties in JavaScript and working with web components. This article provides an overview of the code to get you started, providing links and pointers along the way.
Set Up Variables and Styles
We first create three images that together comprise the company logo: LOGO_TXT
, LOGO_IMG
, and LOGO_PHONE
. By separating the logo into three parts, each part of the logo can be controlled separately. Each of these images are stored in the Wix Media Gallery.
We also create a STYLE
variable that defines custom element behaviors using CSS properties.
- The
animation
property sets multiple animations at one time as a shortcut. - The
animation-iteration-count
property sets how many times an animation should repeat. - The
transform
property is for rotating and resizing elements.
1
Define Functions
Rotate and Resize the Custom Element
The scrollHandler()
function rotates the logo and changes its size.
1
Check if the Size of the Custom Element Has Changed
In our example, as we scroll up or down, our logo changes size.
This function checks if the element is getting smaller or bigger, and fires a sizeChange
event for Velo to handle. The dispatchEvent()
function creates the new custom event. The event is triggered when the size of the custom element gets bigger or smaller.
Later, when we code in Velo, we use the Velo API on()
function to handle the sizeChange
event.
1
Add a logo image to the page
The createImg()
function takes each part of the logo and adds it to the page. This is equivalent to adding the image to the page's DOM.
1
Create the Custom Element Class
This code defines the allEffects
custom element class.
-
The class is designed to listen for the mouse wheel to indicate the page is being scrolled, and calls the
scrollHandler()
function using the style we defined earlier. In this example, if the visitor scrolls to the bottom of the page, the logo gets smaller. If the visitor scrolls to the top, the logo gets larger. -
Classes can also contain lifecycle callback functions. Lifecycle callback functions are triggered at specific, predetermined points. This code uses the
connectedCallback()
function, which is called when the custom element is available for rendering on the site. At this point, our custom element is displayed on the page, without the phone number. -
Another lifecycle callback function used in this code is the
attributeChangedCallback()
function. This function is called whenever an attribute of the custom element is added, removed, or changed. This function works together with a staticget observedAttributes()
method to determine what attributes changed. In this code, we check if the site visitor's mouse is hovering over the menu ("hoveringmenu"), and we check if the footer is currently displayed in the site visitor's viewport ("footershown"). If the mouse is over the menu, our logo shakes. If the footer is in view, the phone number is not shown.
1
Register the Custom Element
This part of the code actually defines the custom element that we specified using the parameters, functions, and class above. Use the customElements.define
method to register the custom element class allEffects
, and associate the custom element with the tag name for the custom element in the Velo representation of the custom element,my-logo-effects
.
1
The Custom Element Complete Code
For your convenience, here is the custom element's code in one code snippet:
1
Code the Behaviors with Velo
Custom element behaviors are coded both in the custom elements' JavaScript file and also in Velo.
You can customize how the custom element behaves for a single page on the site or for all pages on the site.
On a Single Page
In our example, use the on()
function to define what happens when the sizeChange
event is triggered.
As we scroll down the current page:
- The logo gets smaller, as defined in the custom element's JavaScript file.
- The menu disappears, as defined here in Velo.
We defined this sizeChange
event in the JavaScript file that contains our custom element's implementation in Line 11
above: new CustomEvent('sizeChange')
;
Creating events is the custom element's way of "communicating" with Velo. Events let Velo know that something has to be handled, and parameters can be sent with the event.
1
On All Site Pages
If we want the behavior to apply to all pages of the site, we code custom element behaviors in the masterPage.js file located in the Page Code section of the Code sidebar (Wix Studio), or the Velo sidebar (Wix Editor).
In the following code, we create events for the actions that a site visitor might perform that impact our custom element.
We then create and set attributes on the custom element using setAttribute()
. This is Velo's way of "communicating" with the custom element. We can pass the custom element attributes and their values so that the custom element's implementation can behave accordingly.
In the code snippet below, we see that the custom element's behavior will change for the entire site in the following cases:
- If the footer enters or leaves the viewport.
- If the site visitor's mouse hovers or stops hovering over the menu.
1