当前位置:网站首页>有意思的鼠標指針交互探究
有意思的鼠標指針交互探究
2022-07-03 06:11:00 【xuhss_com】
優質資源分享
學習路線指引(點擊解鎖) | 知識定比特 | 人群定比特 |
---|---|---|
🧡 Python實戰微信訂餐小程序 🧡 | 進階級 | 本課程是python flask+微信小程序的完美結合,從項目搭建到騰訊雲部署上線,打造一個全棧訂餐系統。 |
Python量化交易實戰 | 入門級 | 手把手帶你打造一個易擴展、更安全、效率更高的量化交易系統 |
今天,來實現這樣一個有意思的交互效果:
將原本的鼠標指針樣式,修改成自己想要的效果,並且添加上一些特殊的交互效果。
修改鼠標樣式
首先,第一個問題,我們可以看到,上圖中,鼠標指針的樣式被修改成了一個圓點:
正常而言應該是這樣:
當然,這裏比較簡單,在 CSS 中,我們可以通過 cursor
樣式,對鼠標指針形狀進行修改。
利用 cursor
修改鼠標樣式
cursor CSS 屬性設置鼠標指針的類型,在鼠標指針懸停在元素上時顯示相應樣式。
cursor: auto;
cursor: pointer;
...
cursor: zoom-out;
/* 使用圖片 */
cursor: url(hand.cur)
/* 使用圖片,並且設置 fallback 兜底 */
cursor: url(hand.cur), pointer;
這個大家應該都清楚,通常而言,在不同場景下,選擇不同鼠標指針樣式,也是一種提昇用戶體驗的手段。
當然,在本交互中,我們並非要將 cursor 光標設置成任一樣式,剛好相反,我們需要將他隱藏。
通過 cursor: none
隱藏光標
在這裏,我們通過 cursor: none
隱藏頁面的鼠標指針:
{
cursor: none;
}
如此一來,頁面上的鼠標指針就消失了:
通過全局事件監聽,模擬鼠標指針
既然,消失了,我們就簡單模擬一個鼠標指針。
我們首先實現一個 10px x 10px
的圓形 div,設置為基於 絕對定比特:
<div id="g-pointer">div>
#g-pointer {
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
background: #000;
border-radius: 50%;
}
那麼,在頁面上,我們就得到了一個圓形黑點:
接著,通過事件監聽,監聽 body 上的 mousemove
,將小圓形的比特置與實時鼠標指針比特置重合:
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);
});
});
這樣,如果不設置 cursor: none
,將會是這樣一個效果:
再給 body 加上 cursor: none
,就相當於模擬了一個鼠標指針:
在這個基礎上,由於現在的鼠標指針,實際上是個 div
,因此我們可以給它加上任意的交互效果。
以文章一開頭的例子為例,我們只需要借助混合模式 mix-blend-mode: exclusion
,就能够實現讓模擬的鼠標指針能够智能地在不同背景色下改變自己的顏色。
對於混合模式這個技巧還有所疑問的,可以看看我的這篇文章:利用混合模式,讓文字智能適配背景顏色
完整的代碼:
<p>Lorem ipsum dolor sit ametp>
<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);
});
});
我們就能完美還原出題圖的效果:
完整的代碼,你可以戳這裏:Mouse Cursor Transition
偽類事件觸發
有一點需要注意的是,利用模擬的鼠標指針去 Hover 元素,Click 元素的時候,會發現這些事件都無法觸發。
這是由於,此時被隱藏的指針下面,其實懸浮的我們模擬鼠標指針,因此,所有的 Hover、Click 事件都觸發在了這個元素之上。
當然,這個也非常好解决,我們只需要給模擬指針的元素,添加上 pointer-events: none
,阻止默認的鼠標事件,讓事件透傳即可:
{
pointer-events: none;
}
鼠標跟隨,不僅於此
當然,這裏核心就是一個鼠標跟隨動畫,配合上 cursor: none
。
而且,鼠標跟隨,我們不一定一定要使用 JavaScript。
我在 不可思議的純 CSS 實現鼠標跟隨 一文中,介紹了一種純 CSS 實現的鼠標跟隨效果,感興趣的也可以看看。
基於純 CSS 的鼠標跟隨,配合 cursor: none
,也可以制作出一些有意思的動畫效果。像是這樣:
CodePen Demo – Cancle transition & cursor none
最後
本文到此結束,希望對你有幫助
更多精彩 CSS 技術文章匯總在我的 Github – iCSS ,持續更新,歡迎點個 star 訂閱收藏。
如果還有什麼疑問或者建議,可以多多交流,原創文章,文筆有限,才疏學淺,文中若有不正之處,萬望告知。
想 Get 到最有意思的 CSS 資訊,千萬不要錯過我的 iCSS 公眾號 :
如果覺得文章對您有用,請隨意打賞。您的支持將鼓勵我繼續創作!打賞支持+## (_)打個賞喝個咖啡(_)
边栏推荐
- 1. 兩數之和
- Oauth2.0 - Introduction and use and explanation of authorization code mode
- When PHP uses env to obtain file parameters, it gets strings
- Apple submitted the new MAC model to the regulatory database before the spring conference
- 輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
- Migrate data from Amazon aurora to tidb
- Jedis source code analysis (I): jedis introduction, jedis module source code analysis
- [teacher Zhao Yuqiang] MySQL flashback
- 使用conda创建自己的深度学习环境
- Kubernetes notes (I) kubernetes cluster architecture
猜你喜欢
卷积神经网络CNN中的卷积操作详解
Tabbar settings
Zhiniu stock -- 03
智牛股项目--05
Phpstudy setting items can be accessed by other computers on the LAN
Bio, NiO, AIO details
[teacher Zhao Yuqiang] index in mongodb (Part 1)
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
pytorch 搭建神经网络最简版
Kubernetes notes (III) controller
随机推荐
CAD插件的安裝和自動加載dll、arx
Alibaba cloud Alipay sandbox payment
Analysis of Clickhouse mergetree principle
技术管理进阶——你了解成长的全貌吗?
Kubernetes notes (V) configuration management
Tabbar settings
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
Disruptor learning notes: basic use, core concepts and principles
Redis cluster creation, capacity expansion and capacity reduction
Kubernetes notes (10) kubernetes Monitoring & debugging
理解 期望(均值/估计值)和方差
1. 两数之和
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
Support vector machine for machine learning
Installation of CAD plug-ins and automatic loading of DLL and ARX
Advanced technology management - do you know the whole picture of growth?
BeanDefinitionRegistryPostProcessor
. Net program configuration file operation (INI, CFG, config)
About the difference between count (1), count (*), and count (column name)