
- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 特性
- ReactJS - 優點與缺點
- ReactJS - 架構
- ReactJS - 建立React應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在Expense Manager應用中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理API
- ReactJS - 無狀態元件
- ReactJS - 使用React Hooks進行狀態管理
- ReactJS - 使用React Hooks進行元件生命週期管理
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - Map
- ReactJS - 表格
- ReactJS - 使用Flux管理狀態
- ReactJS - 測試
- ReactJS - CLI命令
- ReactJS - 構建和部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks簡介
- ReactJS - 使用useState
- ReactJS - 使用useEffect
- ReactJS - 使用useContext
- ReactJS - 使用useRef
- ReactJS - 使用useReducer
- ReactJS - 使用useCallback
- ReactJS - 使用useMemo
- ReactJS - 自定義Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發Refs
- ReactJS - Fragments
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - 無ES6 ECMAScript的React
- ReactJS - 無JSX的React
- ReactJS - 調和
- ReactJS - Refs和DOM
- ReactJS - Render Props
- ReactJS - 靜態型別檢查
- ReactJS - Strict Mode
- ReactJS - Web Components
- 附加概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - UI事件處理器
在Web開發中,構建易於使用且能夠根據我們與之互動而變化的網站和應用程式是一件大事。想象一下,當我們點選按鈕、向下滾動網頁或在鍵盤上輸入內容時——這些操作就是我們所說的使用者介面事件。它們就像構建模組,讓我們能夠在網站上執行操作。因此,在本教程中,我們將瞭解UI事件處理器。
首先,讓我們定義UI事件。它是一個術語,用於描述網頁上的簡單使用者介面事件,例如點選連結、向下滾動頁面或移動滑鼠。UI事件是事件型別族譜的成員,其中包括MouseEvent、TouchEvent、FocusEvent等。它們各自表示使用者可以在網站上執行的不同活動。
語法
<div onScroll={e => console.log('I am in onScroll method')} />
引數
e − 這是一個React事件物件。
UI事件屬性
序號 | 屬性及描述 |
---|---|
1 | UIEvent.detail 一個數字,根據事件型別而變化,提供關於事件的具體細節。 |
2 | UIEvent.sourceCapabilities 提供關於生成觸控事件的物理裝置的資訊。 |
3 | UIEvent.view 返回生成事件的瀏覽器視窗或框架的引用。 |
示例
示例
我們可以在React元件中的div元素上使用onScroll事件來確定使用者何時在該div內滾動。當滾動事件發生時,事件處理器被啟用,幫助我們執行某些操作。以下是我們的App.js檔案的程式碼:
我們匯入了React並建立了App函式元件。我們定義了onScroll函式,它是onScroll事件處理器。在這個例子中,當div被滾動時,它會在控制檯列印“I am onScroll”。
我們在return語句中插入一個div元素,並將onScroll屬性設定為onScroll方法。我們還向div新增額外的CSS樣式以使其可滾動。
import React from "react"; function App() { const onScroll = (e) => { console.log("I am onScroll"); }; return ( <div onScroll={onScroll} style={{ height: "200px", // Add fixed height to create scrollable div overflow: "scroll" // Add scrollbars }} > <div style={{ height: "600px" }}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </div> ); } export default App;
輸出

當我們啟動React應用程式並在div內滾動時,每次發生滾動事件時,控制檯都會顯示“I am onScroll”。這是一個簡單的例子,說明如何使用onScroll事件處理器在React元件中響應使用者的滾動操作。
示例 - 滾動追蹤應用
此應用程式在我們向下滾動時跟蹤並顯示頁面的當前垂直滾動位置。它使用onScroll事件來更新滾動位置並在頁面上顯示它。程式碼如下:
import React, { useState } from 'react'; function App() { const [scrollPosition, setScrollPosition] = useState(0); const handleScroll = () => { setScrollPosition(window.scrollY); }; window.addEventListener('scroll', handleScroll); return ( <div> <h1>Scroll Tracker</h1> <p>Scroll Position: {scrollPosition}</p> <div style={{ height: '100vh', border: '1px solid #ccc' }}> Scroll down to see the position. </div> </div> ); } export default App;
輸出

示例 - 滾動到頂部按鈕
在這個應用中,我們將有一個“滾動到頂部”按鈕,當我們向下滾動頁面時,該按鈕會顯示出來。當我們點選該按鈕時,它會平滑地將頁面滾動到頂部。它利用onScroll事件來確定何時顯示按鈕,並在點選按鈕時觸發scrollTo函式。
import React, { useState } from 'react'; function App() { const [showButton, setShowButton] = useState(false); const handleScroll = () => { const scrollY = window.scrollY; setShowButton(scrollY > 200); // Show button when scrolled down }; const scrollToTop = () => { window.scrollTo({ top: 0, behavior: 'smooth', }); }; window.addEventListener('scroll', handleScroll); return ( <div> <h1>Scroll to Top Button</h1> <div style={{ height: '150vh', border: '1px solid #ccc' }}> Scroll down to see the button. </div> {showButton && ( <button onClick={scrollToTop} style={{ position: 'fixed', bottom: '20px', right: '20px' }}> Scroll to Top </button> )} </div> ); } export default App;
輸出

總結
UI事件處理器允許我們響應Web使用者的活動。這些屬性描述了互動,例如點選了什麼或滾動到多遠。Web開發者可以使用這些處理器和特性來建立流暢且引人入勝的使用者體驗。