Use CSS variables to achieve cool and amazing floating effects

Use CSS variables to achieve cool and amazing floating effects

Recently, I found a fun hover animation from the Grover website and got some inspiration of my own. This animation is that when you move the mouse over the subscription button, the corresponding color gradient will be displayed. This idea is simple, but it makes the button stand out, people notice it immediately, and increase the probability of clicking it.

How can we achieve this effect and make our website stand out? In fact, it is not as difficult as you think!

Tracking location

The first thing we need to do is get the position of the mouse.

document.querySelector('.button').onmousemove = (e) => {
  const x = e.pageX - e.target.offsetLeft
  const y = e.pageY - e.target.offsetTop

  e.target.style.setProperty('--x', `${ x }px`)
  e.target.style.setProperty('--y', `${ y }px`)

}
  1. Select the element and wait until the user moves the mouse over it;
  2. Calculate the position relative to the element;
  3. Store the coordinates in CSS variables.

Yes, with just 9 lines of code you can get information about where the user has placed their mouse cursor. You can use this information to achieve amazing results, but let’s finish the CSS code first.

Animated gradient

We start by storing the coordinates in CSS variables so that we can use them at any time.

.button {
  position: relative;
  appearance: none;
  background: #f72359;
  padding: 1em 2em;
  border: none;
  color: white;
  font-size: 1.2em;
  cursor: pointer;
  outline: none;
  overflow: hidden;
  border-radius: 100px;

  span {
    position: relative;
  }

  &::before {
    --size: 0;  

    content: '';
    position: absolute;
    left: var(--x);
    top: var(--y);
    width: var(--size);
    height: var(--size);
    background: radial-gradient(circle closest-side, #4405f7, transparent);
    transform: translate(-50%, -50%);
    transition: width .2s ease, height .2s ease;
  }

  &:hover::before {
    --size: 400px;
  }
}

  1. Wrap the text in a span to avoid it appearing above the button.
  2. Initialize width and height to 0px, and when the user hovers over the button, change it to 400px. Don't forget to set this transition so that it appears instantly like the wind :dash:;
  3. Track the mouse position using coordinates;
  4. Apply a radial-gradient to the background property, using the closest-side circle. Closest-side can cover the entire surface.

Summarize

The above is what I introduced to you about using CSS variables to achieve cool and amazing floating effects. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Docker adds a bridge and sets the IP address range

>>:  Discussion on the browsing design method of web page content

Recommend

Simple example of adding and removing HTML nodes

Simple example of adding and removing HTML nodes ...

Vue+echarts realizes stacked bar chart

This article shares the specific code of Vue+echa...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...

Docker deploys net5 program to achieve cross-platform functions

Deployment environment: docker container, liunx s...

Uniapp uses Baidu Voice to realize the function of converting recording to text

After three days of encountering various difficul...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

Use of nginx custom variables and built-in predefined variables

Overview Nginx can use variables to simplify conf...

Linux kernel device driver system call notes

/**************************** * System call******...

How to find the my.ini configuration file in MySQL 5.6 under Windows

Make a note so you can come back and check it lat...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

Summary of basic knowledge and operations of MySQL database

This article uses examples to explain the basic k...

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

Solve the problem of specifying udp port number in docker

When Docker starts a container, it specifies the ...

Implementation of tomcat deployment project and integration with IDEA

Table of contents 3 ways to deploy projects with ...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...