Table of Contents
Modify mouse style
Use cursor to modify the mouse style
伪类事件触发
鼠标跟随,不仅于此
Home Web Front-end CSS Tutorial An in-depth exploration of CSS mouse pointer interaction effects

An in-depth exploration of CSS mouse pointer interaction effects

Jun 24, 2022 pm 01:50 PM
css css3 mouse follow

Today, let’s implement such an interesting interactive effect. Let’s talk about front-end mouse pointer interaction through this exchange effect. I hope it will be helpful to everyone!

Modify the original mouse pointer style to the effect you want, and add some special interactive effects. [Recommended learning: css video tutorial]

Modify mouse style

First of all, the first question, we can see that in the picture above , the style of the mouse pointer is modified to a dot:

Normally it should be like this:

Of course, this is relatively simple. In CSS, we can modify the mouse pointer shape through the cursor style.

Use cursor to modify the mouse style

cursor CSS property sets the type of the mouse pointer and displays the corresponding style when the mouse pointer hovers over the element .

cursor: auto;
cursor: pointer;
...
cursor: zoom-out;
/* 使用图片 */
cursor: url(hand.cur)
/* 使用图片,并且设置 fallback 兜底 */
cursor: url(hand.cur), pointer;
Copy after login

Everyone should know this. Generally speaking, choosing different mouse pointer styles in different scenarios is also a way to improve the user experience.


##Of course, in this interaction,

We are not trying to set the cursor In any way, just the opposite, we need to hide it.

Hide the cursor via cursor: none

Here, we hide the mouse pointer of the page via

cursor: none:

{
    cursor: none;
}
Copy after login
In this way, the mouse pointer on the page disappears:

Simulate through global event listening Since the mouse pointer

has disappeared, we simply simulate a mouse pointer.

We first implement a

10px x 10px circular div, set to absolute positioning based on

:
<div></div>
Copy after login
#g-pointer {
    position: absolute;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    background: #000;
    border-radius: 50%;
}
Copy after login
Then, in On the page, we get a circular black dot:

An in-depth exploration of CSS mouse pointer interaction effects

Then, through event monitoring, listen for

mousemove## on the body. #, coincide the position of the small circle with the real-time mouse pointer position: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">const element = document.getElementById(&quot;g-pointer&quot;); const body = document.querySelector(&quot;body&quot;); function setPosition(x, y) {   element.style.transform  = `translate(${x}px, ${y}px)`;                 } body.addEventListener('mousemove', (e) =&gt; {   window.requestAnimationFrame(function(){     setPosition(e.clientX - 5, e.clientY - 5);   }); });</pre><div class="contentsignin">Copy after login</div></div> In this way, if

cursor: none

is not set, the effect will be like this:

Add

cursor: none

to the body, which is equivalent to simulating a mouse pointer:

On this basis, since the current mouse pointer is actually a

div

, so we can add any interactive effect to it. Taking the example at the beginning of the article as an example, we only need to use the mix mode

mix-blend-mode: exclusion

to enable the simulated mouse pointer to intelligently move between different background colors. Next change your color.

If you still have questions about the blending mode technique, you can read my article: Using blending mode to intelligently adapt text to the background color

https:// github.com/chokcoco/iCSS/issues/169

Complete code:

Lorem ipsum dolor sit amet

<div></div> <div></div>
Copy after login
body {
    cursor: none;
    background-color: #fff;
}
#g-pointer-1,
#g-pointer-2
{
    position: absolute;
    top: 0;
    left: 0;
    width: 12px;
    height: 12px;
    background: #999;
    border-radius: 50%;
    background-color: #fff;
    mix-blend-mode: exclusion;
    z-index: 1;
}
#g-pointer-2 {
    width: 42px;
    height: 42px;
    background: #222;
    transition: .2s ease-out;
}
Copy after login
const body = document.querySelector("body");
const element = document.getElementById("g-pointer-1");
const element2 = document.getElementById("g-pointer-2");
const halfAlementWidth = element.offsetWidth / 2;
const halfAlementWidth2 = element2.offsetWidth / 2;

function setPosition(x, y) { 
    element.style.transform  = `translate(${x - halfAlementWidth}px, ${y - halfAlementWidth}px)`;       
    element2.style.transform  = `translate(${x - halfAlementWidth2}px, ${y - halfAlementWidth2}px)`;
}

body.addEventListener('mousemove', (e) => {
  window.requestAnimationFrame(function(){
    setPosition(e.clientX, e.clientY);
  });
});
Copy after login

We can perfectly restore the effect of the question picture:

For the complete code, you can click here: Mouse Cursor Transition

https://codepen.io/Chokcoco/pen/rNJQXXV

伪类事件触发

有一点需要注意的是,利用模拟的鼠标指针去 Hover 元素,Click 元素的时候,会发现这些事件都无法触发。

这是由于,此时被隐藏的指针下面,其实悬浮的我们模拟鼠标指针,因此,所有的 Hover、Click 事件都触发在了这个元素之上。

当然,这个也非常好解决,我们只需要给模拟指针的元素,添加上 pointer-events: none,阻止默认的鼠标事件,让事件透传即可:

{
    pointer-events: none;
}
Copy after login

鼠标跟随,不仅于此

当然,这里核心就是一个鼠标跟随动画,配合上 cursor: none

而且,鼠标跟随,我们不一定一定要使用 JavaScript。

我在 不可思议的纯 CSS 实现鼠标跟随 一文中,介绍了一种纯 CSS 实现的鼠标跟随效果,感兴趣的也可以看看。

https://github.com/chokcoco/iCSS/issues/46

基于纯 CSS 的鼠标跟随,配合 cursor: none,也可以制作出一些有意思的动画效果。像是这样:

CodePen Demo -- Cancle  transition & cursor none

https://codepen.io/Chokcoco/pen/gOvZoVv

(学习视频分享:web前端

The above is the detailed content of An in-depth exploration of CSS mouse pointer interaction effects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles