How can I create an eye where the pupil follows the cursor's position in Webflow?

Published on
September 22, 2023

To create an eye where the pupil follows the cursor's position in Webflow, you can use custom code and interactions. Here's a step-by-step guide on how to achieve this effect:

  1. Set up the eye element:
  • Start by adding a div block to your Webflow project as the eye container.
  • Inside the container, add another div block to represent the eye itself and give it a class name for easier targeting.
  • Style the eye using Webflow's visual designer. Adjust the size, color, and any other styling properties to match your design.
  1. Add custom code:
  • Go to the custom code section of your Webflow project by clicking on the gear icon in the designer toolbar and selecting "Custom Code".
  • In the head section, add a style tag and write CSS to position the pupil within the eye. Use absolute positioning and set the initial top and left values to position it in the center. Give the pupil a class name to target it later.
  • Then, add a script tag and write JavaScript code to handle the cursor movement. Inside this code, you'll need to calculate the position of the cursor relative to the eye's position and update the top and left values of the pupil element accordingly.
  1. Implement cursor tracking:
  • Back in the Webflow designer, select the eye element and add an interaction to it. Choose the "Mouse Move" trigger and select the "While Page Scrolling" option to ensure the interaction remains active.
  • In the interaction settings, add two actions: one to add a class name to the pupil element when the interaction starts, and another to remove the class name when the interaction ends. The class name will be used for styling purposes, such as changing the color of the pupil.
  1. Style the pupil based on cursor position:
  • Go to the style panel and create a new combo class for the pupil element, which takes effect when the cursor-tracking class name is applied to it.
  • In this combo class, you can add custom styling properties like the background color or a CSS transform to give the impression of the pupil following the cursor's position.
  1. Preview and refine:
  • Save your changes and preview the page to see the eye effect in action.
  • If necessary, adjust the CSS and JavaScript code to fine-tune the positioning and styling of the pupil element.

By following these steps, you will be able to create an eye effect in Webflow where the pupil follows the cursor's position. Keep in mind that this method requires some knowledge of HTML, CSS, and JavaScript.

Additional Resources:

Qustions:

  1. How can I make the eye blink in Webflow?
  2. Is it possible to add multiple eyes with the cursor following effect?
  3. Can I achieve the same effect using CSS only?