」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用 CSS 建立自訂滑鼠遊標

使用 CSS 建立自訂滑鼠遊標

發佈於2024-11-08
瀏覽:122

Written by Samson Omojola✏️

Editor’s note: This article was last updated by Njong Emy on 5 August 2024 to update content and code blocks, as well as to offer a brief comparison of CSS and JavaScript cursors to understand scenarios when you would want to use JavaScript.

Acting as the middleman between your users and your website, cursors can either limit or greatly enhance the way your users experience your site. This is why sleek, intentionally designed, custom cursors have become a significant part of UI and UX today.

Custom cursors are an opportunity to give your users direction in an engaging way and create a memorable, immersive experience for them on your website.

In this tutorial, we’ll take a look at what custom cursors are and learn how to use CSS (and JavaScript) to create custom cursors that will give your website a creative edge. To follow along with this tutorial, you should have some knowledge of HTML, CSS, and JavaScript.

Overview of cursors in CSS

We already interact with custom cursors every day. When you hover over buttons and the pointer cursor changes to a hand, or you hover over some text and the cursor changes to a text cursor, this interactivity is achieved through custom cursors.

However, there are many other creative experiences we can provide to our users with cursors. Before we dive into creating custom cursors, you should know that CSS provides you with cursors out of the box for some frequently performed tasks.

These cursors show you what can be done at the exact location you are hovering over. Examples include cursors indicating that you should click links, drag and drop elements, zoom in and out on things, and more.

All you have to do is specify the type of cursor you want using the CSS cursor property. Some of the available cursors in CSS include:


.auto            { cursor: auto; }
.default         { cursor: default; }
.none            { cursor: none; }
.context-menu    { cursor: context-menu; }
.help            { cursor: help; }
.pointer         { cursor: pointer; }
.progress        { cursor: progress; }
.wait            { cursor: wait; }
.cell            { cursor: cell; }
.crosshair       { cursor: crosshair; }
.text            { cursor: text; }
.vertical-text   { cursor: vertical-text; }
.alias           { cursor: alias; }
.copy            { cursor: copy; }
.move            { cursor: move; }
.no-drop         { cursor: no-drop; }
.not-allowed     { cursor: not-allowed; }
.all-scroll      { cursor: all-scroll; }
.col-resize      { cursor: col-resize; }
.row-resize      { cursor: row-resize; }
.n-resize        { cursor: n-resize; }
.e-resize        { cursor: e-resize; }
.s-resize        { cursor: s-resize; }
.w-resize        { cursor: w-resize; }
.ns-resize       { cursor: ns-resize; }
.ew-resize       { cursor: ew-resize; }
.ne-resize       { cursor: ne-resize; }
.nw-resize       { cursor: nw-resize; }
.se-resize       { cursor: se-resize; }
.sw-resize       { cursor: sw-resize; }
.nesw-resize     { cursor: nesw-resize; }
.nwse-resize     { cursor: nwse-resize; }


Hover over the boxes below to see the cursors in action:

See the Pen Untitled by Samson Omojola (@Caesar222) on CodePen.

Check out the complete list of CSS cursors here.

While these cursors are useful and have some basic styling, we can certainly get more creative.

How to create a custom cursor with CSS

Creating a custom cursor with CSS is a pretty straightforward process. The first step is to find the image you want to use to replace the default cursor. You can either design one yourself or get a free PNG that suits your needs from an icon library such as FontAwesome.

Next, point the CSS cursor property to the location of the image using url. Now, the cursor property knows that it's meant to use whatever image is at that URL as its cursor:


body {
      cursor: url('path-to-image.png'), auto;
}


To ensure that this cursor is used on all parts of your website, the best place to use the cursor property is in the body tag of your HTML. However, if you want, you can assign custom cursors to specific elements instead of the whole website.

You can also add a fallback value to your cursor property. When using custom CSS properties, this value ensures that if the image that serves as your custom property is missing or cannot be loaded, then your users will have another option.

In this case, auto is the fallback descriptor for your custom cursor property. Your users will see the regular cursor if the custom one is unavailable. You can also provide more than one custom cursor (multiple fallbacks) for your website. All you have to do is add their paths to the cursor property:


body {
      cursor: url('path-to-image.png'), url('path-to-image-2.svg'), url('path-to-image-3.jpeg'), auto;
}


There are three fallback cursors in the code above.

Because they draw attention to elements you want to highlight on your website, custom cursors are best used in specific scenarios, such as:

  • Indication of special interactions
  • Storytelling purposes
  • Gaming interactions
  • In hover-effects

How to change a mouse cursor to a pointer

Say you have a table and you’d like the mouse cursor to change to a pointer (i.e., the hand icon) whenever a user hovers over a row in the table. You can use the CSS cursor property to achieve this.

Here’s an example:


Name Age City
John 30 New York
Jane 25 Chicago
Bill 35 Los Angeles

In the above code, we use the tr:hover selector to apply the cursor property to all table rows when the mouse hovers over them. The cursor property is set to pointer, which changes the mouse cursor to a hand icon.

How to hide a mouse cursor with CSS

To hide the mouse cursor with CSS, you can use the cursor property and set its value to none. Here’s an example:


This will hide the mouse cursor throughout the entire webpage. If you only want to hide the mouse cursor for a specific element, you can apply the cursor property to that individual element instead of the body element.

There are several situations in which hiding the mouse cursor might be useful, such as:

  • In a game or interactive application, hiding the mouse cursor could help create a more immersive experience for the user
  • In a presentation or slideshow, hiding the mouse cursor could reduce distractions and keep the focus on the content
  • In fullscreen video or media, hiding the mouse cursor could help prevent the user from accidentally clicking on controls or other elements

Remember that hiding the mouse cursor can be confusing or disorienting for some users, depending on the use case. This strategy should be used carefully and only when necessary.

CSS vs. JavaScript cursors

In as much as custom cursors can be created with CSS, using JavaScript also has its added advantages. Before we dive into how to create custom cursors with JavaScript, let's take a look at the advantages and disadvantages of creating custom cursors with CSS, and the same for JavaScript.

There are numerous reasons why it is preferable to create cursors with CSS:

  • Simplicity: With just a line of CSS, it is possible to toggle between numerous default types of cursors
  • Browser support: CSS custom cursors are fully supported by all browsers, so you don’t need any extra configurations to operate your websites on multiple browsers

The main disadvantage of using CSS for custom cursors is that adding animation or further customizing the cursor would involve more complexity. This is where JavaScript comes in. With JavaScript, you can add more complex interactions when the user interacts with the cursor, i.e., hovering, clicking, or moving over specific elements. By listening to specific events, the cursor's movements can then be updated. Cursors can also be more easily animated with JavaScript. With only CSS, creating complex cursor transitions may prove to be more difficult.

Just as simplicity and browser support are reasons why you should use CSS, complexity and cross-browser issues are reasons why you shouldn't use JavaScript.

How to create a custom cursor with JavaScript

Creating a custom cursor with JavaScript involves manipulating DOM elements. We'll create some DOM elements, which will serve as our custom cursor, and then use JavaScript to manipulate them. Then, as we move our cursor around, those custom elements will move around as our cursor.

Rather than design an image or download an image online, we will use CSS to design a custom cursor. I want us to use something animated that draws users in. Move your cursor around the box below to see an example of what I’m describing:

See the Pen Untitled by Samson Omojola (@Caesar222) on CodePen.

As you can see, the cursor consists of two elements — one large circle and one small one. We’ll create two div elements and give them class names:


Next, we’ll create the circles with CSS. In the code below, we assign a width and height of 50px each to the big circle. To make it a circle, we give it a border radius of 50%.

The small circle will be hollow, so we give it a border and border-radius of 50%. Then, we assign it a width and height of 6px each.

We disable the default cursor by giving cursor a value of none so that we can render the custom cursor in its place.

To add animation to the big circle, we use @keyframes.

Our animation-duration is 2.0s. At the start of this duration, we set background-color to green and opacity to 0.2. Halfway through, we set the circle’s background-color to orange. At the end of the 2s duration, we set the circle’s background-color to red.

To make the animation repeat over and over again, we set animation-iteration-count to infinite:


body{
  background-color: #171717;
  cursor: none;
  height: 120vh;
}
.small{
  width: 6px;
  height: 6px;
  border: 2px solid #fff;
  border-radius: 50%;
}
.big{
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-bottom: 20rem;
  animation-name: stretch;
            animation-duration: 2.0s;
            animation-timing-function: ease-out;
            animation-direction: alternate;
            animation-iteration-count: infinite;
            animation-play-state: running;
}

@keyframes stretch {
            0% {

                opacity: 0.2;
                background-color: green;
                border-radius: 100%;
            }

            50% {
                background-color: orange;
            }

            100% {
                background-color: red;
            }
        }


Now, to make the elements move as you move your mouse, we’ll use JavaScript.

In the code below, we use an event listener to listen for whenever a user moves their mouse over our webpage. Whenever this event takes place, we use a function to get the x and y coordinates of the mouse. We then use the x and y coordinates to move our div elements around as a representative of our cursor:


const cursorSmall = document.querySelector('.small');
const cursorBig = document.querySelector('.big');

const positionElement = (e)=> {
  const mouseY = e.clientY;
  const mouseX = e.clientX;

  cursorSmall.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`;

  cursorBig.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`;

}

window.addEventListener('mousemove', positionElement)


See the complete code alongside the interactive cursor in the below CodePen:

See the Pen Untitled by Samson Omojola (@Caesar222) on CodePen.

As you can see, we used querySelector to get access to the two div elements on the DOM. Then, we added an event listener to the webpage to listen for and detect when the user moves their mouse.

When this happens, the event listener triggers a positionElement function. In this function, we mapped these coordinates to the div elements whose DOM positions we have already selected.

We move the div elements in correspondence with the x and y coordinates of the mouse using transform and translate3d. transform repositions elements in horizontal and vertical directions, while translate3d repositions elements in 3D space.

As the mouse moves, its x and y values change, transform and translate3d get updated with the help of the event listener, and the position of each div changes in correspondence.

UX and browser compatibility when creating a custom cursor

When creating a custom cursor, you should always keep the user’s best interests in mind. One way to do this is by creating a cursor that best represents or translates the essence of an element. While custom cursors give your website a unique touch, it is important to not overdo it. Overcustomizing your cursor can frustrate and confuse your users by complicating their experience on your website.

Before you decide to create a custom cursor, you should consider its functionality. For example, will a custom cursor's features work well on an old browser?

Note that some older browsers can’t support many new CSS and JavaScript features. If you design certain areas of your website around a custom cursor that uses technologies beyond your users’ browsers, they won’t be able to engage with your website.

Creating a custom cursor with accessibility in mind

As mentioned previously, custom cursors can be helpful for users with impaired mobility and other accessibility requirements. For example, people with low vision may need very large mouse pointers that are easy to track or mouse pointers with high-contrast colors that make them stand out from various backgrounds.

A mouse can also be programmed to invert the colors of any elements it hovers over. This makes it easy for users to track what they’re reading or seeing on the screen. The text cursor or caret can also be thickened so that users with low vision don’t lose track of where the cursor is on the page while typing.

Large cursors can also benefit users with motor impairment, as they are easier to move and place on elements than small cursors.

Some users may prefer to keep the default cursor for familiarity. The use of media queries such as prefers-reduced-motion can be used to toggle or disable custom cursors for users who find them distracting:


@media
(prefers-reduced-motion: reduce) {
  *{
    cursor: auto; /*reverts to the default cursor */
  }
}


Another important technique to improve accessibility is by disabling custom cursors for screen readers. These may interfere with screen readers and create confusion for the user. Adding aria-hidden = "true" to the elements that make use of the custom cursor, ensures that they are not exposed to assistive technologies like screen readers, thereby preventing confusion.

Conclusion

In this tutorial, we learned about CSS cursors available out of the box, how to create custom cursors with CSS, how to give your webpage multiple cursors, and how to use animation in CSS and JavaScript to create custom cursors. We’ve also seen the pros and cons of using CSS and JavaScript when handling custom cursors, and when to make use of cursors that are not the default.

Custom cursors can be a great way to draw your users in, keep them engaged, and direct them efficiently if implemented correctly. If you have any further thoughts or questions about creating custom cursors with CSS, let me know in the comment section.


Is your frontend hogging your users' CPU?

As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If you’re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.

Creating custom mouse cursors with CSS

LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app, mobile app, or website. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.

Modernize how you debug web and mobile apps — start monitoring for free.

版本聲明 本文轉載於:https://dev.to/logrocket/creating-custom-mouse-cursors-with-css-368l?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 我如何創建我的第一個開源專案。
    我如何創建我的第一個開源專案。
    這篇文章是關於我如何決定創建和分享一個簡單的工具來輸入剪貼簿內容,從而產生了 CBPK 專案。 動機 一切都是從我嘗試登入我的 AWS EC2 機器開始的。每次我想使用 Parsec 連接到 AWS EC2 時,我都必須輸入整個作業系統密碼,因為沒有辦法透過 Parsec 進行貼上...
    程式設計 發佈於2024-11-08
  • JavaScript 中的 Promise,4 人指南
    JavaScript 中的 Promise,4 人指南
    隨著 JavaScript 的不斷發展,理解非同步程式設計對於現代開發至關重要。 Promise 是一個強大的工具,可讓您更有效地處理非同步操作。這是有關如何在 JavaScript 專案中使用 Promise 的指南。 什麼是 Promise? Promise 是一個對象,表示非同步操作的最終...
    程式設計 發佈於2024-11-08
  • HTML 表單中的預設提交按鈕行為是什麼?
    HTML 表單中的預設提交按鈕行為是什麼?
    確定HTML 表單中的預設提交按鈕在未按一下特定提交按鈕的情況下提交HTML 表單時,例如按輸入或在JavaScript 中使用HTMLFormElement.submit(),瀏覽器需要確定多個提交按鈕(如果有)中的哪一個應被視為按下的按鈕。此確定對於觸發 onclick 事件處理程序和傳送到 W...
    程式設計 發佈於2024-11-08
  • 程式語言可以模仿變數運算子嗎?
    程式語言可以模仿變數運算子嗎?
    程式語言可以支援變數運算子嗎? 程式語言本身並不支援變數運算子的概念。但是,可以透過建立自訂解決方案來模擬它們的功能。 自訂運算子功能:一種常見方法是定義關聯運算子名稱的物件或映射及其對應的功能。例如,在 JavaScript 中,您可以建立下列物件:var operators = { ' ...
    程式設計 發佈於2024-11-08
  • 編寫 Pythonic 程式碼:更清晰語法的提示和技巧。
    編寫 Pythonic 程式碼:更清晰語法的提示和技巧。
    Python 是一種流行的程式語言,以其可讀性和簡單性而聞名。然而,即使是經驗豐富的 Python 開發人員也可以透過學習新方法來編寫更清晰、更有效率的程式碼而受益。 編寫 Python 程式碼的技巧 以下是編寫 Pythonic 程式碼的一些技巧: 使用描述性變數名稱。選擇清楚表明變數代表什麼的...
    程式設計 發佈於2024-11-08
  • 您可以使用哪些方法來處理 PHP 中的巢狀數組(遞歸或迭代)?
    您可以使用哪些方法來處理 PHP 中的巢狀數組(遞歸或迭代)?
    PHP foreach 與嵌套數組:遞歸方法在 PHP 中使用嵌套數組可能是一個挑戰。考慮一個要存取特定巢狀數組的數組,例如主數組的第二個元素。 可以使用巢狀循環方法來解決此問題:foreach ($tmpArray as $innerArray) { if (is_array($innerAr...
    程式設計 發佈於2024-11-08
  • ## **`std::vector::erase`傳回的迭代器在刪除後是否指向有效元素? **
    ## **`std::vector::erase`傳回的迭代器在刪除後是否指向有效元素? **
    std::vector 迭代器失效:詳細解釋std::vector 中迭代器失效的概念經常被討論。需要澄清的是,透過 std::vector::erase 擦除向量元素會使嚴格位於已擦除元素之後的迭代器無效。 但是,位於已擦除元素的確切位置的迭代器的有效性仍然不確定。從邏輯上講,人們可能會假設該迭代...
    程式設計 發佈於2024-11-08
  • 如何有效率地檢查字串中的字串擴展?
    如何有效率地檢查字串中的字串擴展?
    在字串中尋找字串給定字串 url_string 和一個副檔名清單 extensionsToCheck,確定是否有任何副檔名出現在字串中。一個簡單的方法是遍歷清單並檢查每個擴充功能:for extension in extensionsToCheck: if extension in url_...
    程式設計 發佈於2024-11-08
  • 以下是一些標題選項,請記住問答格式並專注於開發人員的實用性:

**直接切中要害:**

* **我應該使用什麼 Python SOAP 用戶端程式庫
    以下是一些標題選項,請記住問答格式並專注於開發人員的實用性: **直接切中要害:** * **我應該使用什麼 Python SOAP 用戶端程式庫
    有哪些 Python SOAP 用戶端程式庫可用,在哪裡可以找到它們的文件? 在 Python 的 SOAP 用戶端庫領域,存在各種選項,每種選項都滿足至特定要求。對於 SOAP 和 Python 的新手來說,選擇合適的函式庫的任務可能是艱鉅的。以下是可用 SOAP 用戶端程式庫及其文件的綜合指南。...
    程式設計 發佈於2024-11-08
  • 使用 Twig 透過 PHP 渲染 Markdown
    使用 Twig 透過 PHP 渲染 Markdown
    Twig 是使用 Symfony 开发 Web 应用程序时渲染 HTML 的首选模板引擎。 然而,Twig 的灵活性不仅仅限于生成 HTML 页面。它可以成为跨多个渠道交付内容的强大工具,例如生成 Markdown 文件、JSON 输出,甚至纯文本,所有这些都来自同一组内容。 这种适应性允许您为不同...
    程式設計 發佈於2024-11-08
  • 如何在沒有 JavaScript 的情況下使用 POST 參數執行 PHP 重定向?
    如何在沒有 JavaScript 的情況下使用 POST 參數執行 PHP 重定向?
    POST 參數的PHP 重定向提出的查詢涉及將使用者從一個網頁重新導向到另一個網頁,同時保留POST參數。原始方法涉及透過表單將 GET 參數傳輸到 POST 參數,該方法被認為不是最理想的,特別是對於停用 JavaScript 的使用者而言。該問題尋求一個純粹基於 PHP 的解決方案,用於重定向期...
    程式設計 發佈於2024-11-08
  • 如何在 React 中存取提供者外部的上下文時處理錯誤
    如何在 React 中存取提供者外部的上下文時處理錯誤
    使用 React 的 Context API 時,處理元件嘗試存取 Provider 外部上下文的情況非常重要。如果不這樣做,可能會導致意想不到的結果或難以追蹤的錯誤。 問題 當您使用 createContext() 建立上下文時,您可以選擇傳遞預設值。如果元件嘗試存取提供者外部的上下文,則傳回此...
    程式設計 發佈於2024-11-08
  • 如何在 Python 3 中從 Web 伺服器下載檔案?
    如何在 Python 3 中從 Web 伺服器下載檔案?
    如何在 Python 3 中從 Web 伺服器下載檔案簡介從 Web 伺服器下載檔案是許多程式設計中的常見任務項目。 Python 提供了幾個程式庫來簡化此過程,讓您可以輕鬆地從指定的 URL 下載檔案。 使用 urlretrieve 下載檔案import urllib.request url = ...
    程式設計 發佈於2024-11-08
  • Filament:刪除記錄時刪除附件
    Filament:刪除記錄時刪除附件
    Filament 允許您為記錄新增附件,但刪除記錄時不會刪除附件。 為了解決這個問題,我們有兩個選擇: 監聽模型刪除事件 當模型即將被刪除時,它會觸發刪除事件。我們可以監聽此事件來觸發負責在模型不再存在之前刪除任何附件的功能。 在模型類別中,我們可以新增 booted 方法來向模...
    程式設計 發佈於2024-11-08
  • 如何在信用記錄檢索中實現左連接?
    如何在信用記錄檢索中實現左連接?
    How to Perform Left Joins in Doctrine在函數 getHistory() 中,您嘗試擷取使用者的信用歷史記錄。但是,連接子句中的初始語法導致了錯誤。 要在 Doctrine 中執行左連接,可以使用以下語法:$qb ->select('a', 'u') ...
    程式設計 發佈於2024-11-08

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3