当前位置:网站首页>Interesting research on mouse pointer interaction
Interesting research on mouse pointer interaction
2022-06-30 15:51:00 【Sb_ Coco】
today , To achieve such an interesting interaction effect :
Change the original mouse pointer style , Modify it to the desired effect , And add some special interaction effects .
Modify mouse style
First , The first question is , We can see , Above picture , The style of the mouse pointer has been changed to a dot :
Normally it should be :
Of course , Here's the easy part , stay CSS in , We can go through cursor style , Modify the mouse pointer shape .
utilize cursor Modify mouse style
cursor CSS Property to set the type of mouse pointer , Displays the corresponding style when the mouse pointer hovers over the element .
cursor: auto;
cursor: pointer;
...
cursor: zoom-out;
/* Use pictures */
cursor: url(hand.cur)
/* Use pictures , And set up fallback The bottom line */
cursor: url(hand.cur), pointer;Everyone should know this , generally , In different scenes , Select different mouse pointer styles , It is also a means to improve the user experience .
Of course , In this interaction , We are not going to cursor Set the cursor to any style , Just the opposite , We need to hide him .
adopt cursor: none hide cursor
ad locum , We go through cursor: none Hide the mouse pointer of the page :
{
cursor: none;
}In this way , The mouse pointer on the page disappears :
Listen through global events , Simulate mouse pointer
since , Vanished , Let's simply simulate a mouse pointer .
We first implement a 10px x 10px Round div, Set to based on <body> Absolute positioning :
<div id="g-pointer"></div>#g-pointer {
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
background: #000;
border-radius: 50%;
}that , On the page , We get a round black spot :
next , Through event monitoring , monitor body Upper mousemove, The position of the small circle coincides with the position of the real-time mouse pointer :
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);
});
}); such , If not set cursor: none, It will be such an effect :
Give again body add cursor: none, It is equivalent to simulating a mouse pointer :
On this basis , Due to the current mouse pointer , It's actually a div, So we can add any interaction effect to it .
Take the example at the beginning of the article , We just need to use the hybrid mode mix-blend-mode: exclusion, The simulated mouse pointer can intelligently change its own color under different background colors .
There are still questions about the technique of mixed mode , Take a look at my article : Use hybrid mode , Let the text intelligently adapt to the background color
Complete code :
<p>Lorem ipsum dolor sit amet</p>
<div id="g-pointer-1"></div>
<div id="g-pointer-2"></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 title map :
Complete code , You can poke here :Mouse Cursor Transition
Pseudo class events trigger
One thing to note is that , Use the simulated mouse pointer to Hover Elements ,Click When the elements , You will find that these events cannot be triggered .
This is because , Under the hidden pointer , In fact, we simulate the hovering mouse pointer , therefore , be-all Hover、Click Events are triggered on this element .
Of course , This is also very easy to solve , We just need to give the element that simulates the pointer , add pointer-events: none, Block default mouse events , Let the event be transmitted through :
{
pointer-events: none;
}The mouse follow , Not only in the
Of course , The core here is a mouse following animation , Match up cursor: none.
and , The mouse follow , We don't have to use JavaScript.
I am here It's incredibly pure CSS Mouse following In the article , A kind of pure CSS Achieve the mouse following effect , You can also have a look at those who are interested .
Based on pure CSS Mouse follow for , coordination cursor: none, You can also make some interesting animation effects . It's like this. :
CodePen Demo -- Cancle transition & cursor none
Last
This concludes the article , I hope it helps you
More exciting CSS Technical articles summarized in mine Github -- iCSS , Continuous updating , Welcome to point a star Subscribe to the collection .
Any questions or Suggestions , Communicate more , Original article , Writing co., LTD. , Pretty good , If there is anything wrong with the text , All hope to inform .
边栏推荐
- Abstract meaning
- Flask Sqlalchemy - how to use custom query criteria ---orm (7)
- BYD is more and more like Huawei?
- String common API
- 【子矩阵数量统计】CF1181C Flag子矩阵数量统计
- Policy Center > Device and Network Abuse
- Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
- Open source STM32 USB-CAN project
- 《网络是怎么样连接的》读书笔记 - 汇总篇
- 4.6 floating point number
猜你喜欢

Interview experience of service end test engineer

Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0

Policy Center-User Data

CVPR 2022丨特斯联AI提出:基于图采样深度度量学习的可泛化行人重识别

How to do a good job in high concurrency system design? I have summarized three points

Using asp Net core creating web API series

Rte2021 review HDR technology product practice and exploration

Google Play 索引表

Policy Center > Google Play‘s Target API Level Policy
![Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.](/img/c1/99ad29789a669c4498fb93ce1fb009.png)
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
随机推荐
服务端测试工程师面试经验
BYD is more and more like Huawei?
C language foundation - pointer array - initialization method & constant pointer array, pointer constant array
【子矩阵数量统计】CF1181C Flag子矩阵数量统计
CloudXR如何推动XR的未来发展
Rte2021 review of the practice and the way of AI OPS landing
What would you choose between architecture optimization and business iteration?
Oracle 导出视图的创建语句
Parameter optimization - bias and variance
Some reference routines for cache update
Talk about why I started technical writing
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
topic: Privacy, Deception and Device Abuse
Cesium-1.72 learning (deploy offline resources)
Chapter 2 installation and use of vscode editor
Sword finger offer II 080 Combinatorial backtracking with k elements
智慧风电:数字孪生 3D 风机智能设备运维
4.7 type() function query data type
Policy Center > Deceptive Behavior
Abstract meaning