使用CSS 和JavaScript 反轉滑鼠懸停時的文字顏色
為了實現所需的懸停效果,其中黑色文字反轉為白色,同時保持黑色遊標的外觀,我們可以將CSS 剪輯路徑的功能與JavaScript 事件處理結合。
此方法包括建立兩層文字:主文本圖層和反轉文字圖層。倒置文字圖層位於主文字圖層後面,並將其文字顏色設為白色。
使用 JavaScript,我們捕捉滑鼠移動並動態調整倒置文字圖層的剪輯路徑。當滑鼠移動時,剪輯路徑會進行調整以顯示更多反轉的文本,從而產生主要文本反轉其顏色的錯覺。
以下是關鍵程式碼元件的細分:
<code class="css">/* Primary Text Layer */ h1 { color: #000; position: relative; } /* Inverted Text Layer */ h1:before { position: absolute; content: attr(data-text); /* Same text as primary layer */ color: #fff; background: #000; clip-path: circle(0 at var(--x, -100%) var(--y, -100%)); /* Dynamic Clip-path */ } /* Cursor */ .cursor { position: fixed; width: 40px; height: 40px; background: #000; border-radius: 50%; transform: translate(-50%, -50%); z-index: -2; }</code>
<code class="javascript">// Event Listener for Mouse Movement document.body.onmousemove = function(e) { // Update cursor position cursor.style.left = e.clientX + 'px'; cursor.style.top = e.clientY + 'px'; // Update clip-path of inverted layer based on mouse position h1.style.setProperty('--x', (e.clientX - p.top) + 'px'); h1.style.setProperty('--y', (e.clientY - p.left) + 'px'); };</code>
範例程式碼:
<code class="html"><h1 id="WORK">WORK</h1> <span class="cursor"></span></code>
結果:
當您將滑鼠停留在「WORK」文字上時,隨著滑鼠遊標的移動,黑色文字會逐漸變成白色。
以上是如何使用 CSS 剪輯路徑和 JavaScript 實現反轉文字顏色懸停效果?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境