DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Telemetry Pipelines Workshop: Parsing Multiple Events
  • Ten Years of Beam: From Google's Dataflow Paper to 4 Trillion Events at LinkedIn
  • The Hidden Latency of Autoscaling
  • Evolving Spring Boot APIs to an Event-Driven Mesh

Trending

  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Velo by Wix: Imitating Hover Event on Repeater Container

Velo by Wix: Imitating Hover Event on Repeater Container

Velo API doesn't provide a hover event on the repeater container. In this post, we look at one way we can imitate the hover event.

By 
Alexander Zaytsev user avatar
Alexander Zaytsev
·
Jan. 10, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.4K Views

Join the DZone community and get the full member experience.

Join For Free

Motivation

We have a $w.Repeater component with items of users' cards. When we point with the mouse cursor over some item, we want to change the background color of this item to light blue color #CCE4F7, and when the cursor moves off of the item, we want to return the initial white color.

For this, we're going to use two other events that provide repeater API:

  • onMouseIn() runs when the mouse pointer is moved onto the element.
  • onMouseOut() runs when the mouse pointer is moved off of the element.

Also, repeater items don't have property style.backgroundColor for changing the background color of an element. But we can use background.src property for changing the background image. So in this way, we're going to use a one-pixel image.

Here is a one-pixel image.

Event Handlers

To start with, set handlers to onMouse{In/Out} events. We will use one function for two events by repeaters containers. We declare the handler function above and pass the function's name as an argument to container methods.

JavaScript
 




x
11


 
1
const items = [ /* here are our users */ ];
2
 
3
function imitateHover(event) {
4
  // our handler for containers
5
}
6
 
7
$w.onReady(function () {
8
  $w("#repeater1").data = items;
9
  $w("#container1").onMouseIn(imitateHover); // set handler
10
  $w("#container1").onMouseOut(imitateHover);
11
});


Please pay attention’ we don't nest any containers item into the repeater for adding handlers. Like here:

JavaScript
 




xxxxxxxxxx
1


 
1
// In this way, each time when onItemReady starts
2
// would be set a new handler for containers.
3
$w("#repeater1").onItemReady( ($item, itemData, index) => {
4
  $item("#container1").onMouseIn(imitateHover);
5
  $item("#container1").onMouseOut(imitateHover);
6
});


We set globally our handler on all #container1 with $w() selector. And it works well!

Imitate Hover

We use one function for two events. Therefore, we need to listen to which type of event is going. We're expecting two event types:

  1. event.type === "mouseenter" when onMouseIn() is running.
  2. event.type === "mouseleave" when onMouseOut() is running.

Let's see the code:

JavaScript
 




xxxxxxxxxx
1
15


 
1
function imitateHover(event) {
2
  if (event.type === "mouseenter") {
3
    console.log("we have mouseenter if onMouseIn() is running");
4
  }
5
 
6
  if (event.type === "mouseleave") {
7
    console.log("we have mouseleave if onMouseOut() is running");
8
  }
9
}
10
 
11
$w.onReady(function () {
12
  $w("#repeater1").data = items;
13
  $w("#container1").onMouseIn(imitateHover);
14
  $w("#container1").onMouseOut(imitateHover);
15
});


The object event always will be consistent with the current container item, which we point the mouse cursor. And we can change the background.src property of the container by event.target.

JavaScript
 




xxxxxxxxxx
1
19


 
1
// link to one pixel image
2
const HOVER_PNG = "https://static.wixstatic.com/media/e3b156_df544ca8daff4e66bc7714ebc7bf95f1~mv2.png";
3
 
4
function imitateHover(event) {
5
  // when the cursor over container then set image.
6
  if (event.type === "mouseenter") {
7
    event.target.background.src = HOVER_PNG;
8
  }
9
  
10
  if (event.type === "mouseleave") {
11
    // when the cursor is gone then remove the pixel image.
12
    event.target.background.src = "";
13
  }
14
}
15
 
16
$w.onReady(function () {
17
  $w("#repeater1").data = items;
18
  $w("#container1").onMouseIn(imitateHover);
19
  $w("#container1").onMouseOut(imitateHover);
20
});


Great! It works: DEMO.

One Pixel Image

We used the direct link to the one-pixel image. The size of this image is only 70 bytes. For example, if the link of this image has 82 chars length, it's 82 bytes. The link takes up more memory than the image. ¯\_(ツ)_/¯

data:URL

Data URLs are a protocol that allows embedded small files inline in documents as a string. It means we can convert a one-pixel PNG image to a string and pass it to background.src.

We can create needed images by 1x1 PNG generator #cce4f7ff.

JavaScript
 




xxxxxxxxxx
1
17


 
1
// one-pixel image encoded to base64
2
const HOVER_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mM88+R7PQAIUwMo5M6pSAAAAABJRU5ErkJggg==';
3
 
4
function imitateHover(event) {
5
  if (event.type === "mouseenter") {
6
    event.target.background.src = HOVER_PNG;
7
  }
8
  
9
  if (event.type === "mouseleave") {
10
    event.target.background.src = "";
11
  }
12
}
13
 
14
$w.onReady(function () {
15
  $w("#repeater1").data = items;
16
  $w("#container1").onMouseIn(imitateHover);
17
  $w("#container1").onMouseOut(imitateHover);
18
});


The data:URL image is a little longer than the direct link for this image. And other reason to use data:URL with the small image we don't send HTTP request for fetching this image.

Resources

  • Velo APIs
  • Data URLs MDN
  • 1x1 PNG generator

Posts

  • Storeon: An Event-Based State Manager for Corvid
Event Container Repeater

Published at DZone with permission of Alexander Zaytsev. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Telemetry Pipelines Workshop: Parsing Multiple Events
  • Ten Years of Beam: From Google's Dataflow Paper to 4 Trillion Events at LinkedIn
  • The Hidden Latency of Autoscaling
  • Evolving Spring Boot APIs to an Event-Driven Mesh

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook