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.
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.
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:
To review:
connectedCallback()
function to display the logo images when the page is connected and ready.customElements.define()
.Because this example uses Velo, we can use it to host our custom element.
Make sure the custom element displays on the site.
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.
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.
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.
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.
animation
property sets multiple animations at one time as a shortcut.animation-iteration-count
property sets how many times an animation should repeat.transform
property is for rotating and resizing elements.
Rotate and Resize the Custom Element
The scrollHandler()
function rotates the logo and changes its size.
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.
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.
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 static get 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.
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
.
For your convenience, here is the custom element's code in one code snippet:
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.
In our example, use the on()
function to define what happens when the sizeChange
event is triggered.
As we scroll down the current page:
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.
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: