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`) }
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; } }
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! |
<<: Docker adds a bridge and sets the IP address range
>>: Discussion on the browsing design method of web page content
This article example shares the specific code of ...
Table of contents 1. Use scripts to encrypt TLS f...
I recently discovered a pitfall in regular expres...
Preface This article mainly introduces the releva...
Strictly speaking, nginx does not have a health c...
Table of contents What is the reason for the sudd...
Preface: I used the official nginx proxy_cache as...
Table of contents question Server layer and stora...
Preface The string types of MySQL database are CH...
win10 + Ubuntu 20.04 LTS dual system installation...
Table of contents Preface 1. With vue-cli 1. Defi...
Initialize Data DROP TABLE IF EXISTS `test_01`; C...
1. this.$router.push() 1. Vue <template> &l...
Preface In order to reflect the difference betwee...
Table of contents 1. CentOS7+MySQL8.0, yum source...