An in-depth exploration of CSS mouse pointer interaction effects
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;
Everyone should know this. Generally speaking, choosing different mouse pointer styles in different scenarios is also a way to improve the user experience.
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; }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 a10px x 10px circular div, set to absolute positioning based on
:
<div></div>
#g-pointer { position: absolute; top: 0; left: 0; width: 10px; height: 10px; background: #000; border-radius: 50%; }Then, in On the page, we get a circular black dot:
mousemove## on the body. #, coincide the position of the small circle with the real-time mouse pointer position: <pre class="brush:php;toolbar:false">const element = document.getElementById("g-pointer");
const body = document.querySelector("body");
function setPosition(x, y) {
element.style.transform = `translate(${x}px, ${y}px)`;
}
body.addEventListener('mousemove', (e) => {
window.requestAnimationFrame(function(){
setPosition(e.clientX - 5, e.clientY - 5);
});
});</pre>
In this way, if
is not set, the effect will be like this:
Add
to the body, which is equivalent to simulating a mouse pointer:
On this basis, since the current mouse pointer is actually a
, 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
to enable the simulated mouse pointer to intelligently move between different background colors. Next change your color.
https:// github.com/chokcoco/iCSS/issues/169
Complete code:
Lorem ipsum dolor sit amet
<div></div> <div></div>
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; }
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); }); });
We can perfectly restore the effect of the question picture:
https://codepen.io/Chokcoco/pen/rNJQXXV
伪类事件触发
有一点需要注意的是,利用模拟的鼠标指针去 Hover 元素,Click 元素的时候,会发现这些事件都无法触发。
这是由于,此时被隐藏的指针下面,其实悬浮的我们模拟鼠标指针,因此,所有的 Hover、Click 事件都触发在了这个元素之上。
当然,这个也非常好解决,我们只需要给模拟指针的元素,添加上 pointer-events: none
,阻止默认的鼠标事件,让事件透传即可:
{ pointer-events: none; }
鼠标跟随,不仅于此
当然,这里核心就是一个鼠标跟随动画,配合上 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!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment