在 React 中使用 CSS 拖放圖片
React 是一個用於建立使用者介面的流行 JavaScript 函式庫,其靈活性和多功能性使其成為建立互動式應用程式的絕佳選擇。在本教學中,我們將向您展示如何在 React 中僅使用 CSS 建立圖片拖放功能。
步驟 1 —
首先,讓我們建立一個 React 專案。您可以使用 Create React App 或任何其他最適合您的設定方法。讓我們使用 create-react-app 製作一個 React 應用程式。
npx create-react-app drag-and-drop
步驟 2 —
將 App.js 和 App.css 替換為以下程式碼。
App.js
import './App.css'; function App() { return ( <div className="App"> <h2 className="heading">Select Image:</h2> <div className="image-area"> </div> </div> ); } export default App;
App.css
.App { text-align: center; width: 100vw; height: 100vh; } .heading { font-size: 32px; font-weight: 500; }
步驟 3 —
現在在 src 目錄中建立一個新檔案和元件 ImageContainer.js,並使用一個 div 作為拖放容器。
ImageContainer.js
import React from 'react'; const ImageContainer = () => { return ( <div className="image-container"> </div> ); }; export default ImageContainer;
然後在src目錄下製作一個CSS檔案ImageContainer.css,並在圖片容器中加入一些樣式。
ImageContainer.css
.image-container { width: 60%; height: 90%; display: flex; align-items: center; justify-content: center; border: 2px dashed rgba(0, 0, 0, .3); }
步驟 4 —
現在我們將在 .image-container 類別中使用一個帶有輸入欄位和輸入文字標題的 div,並在 ImageContainer.css 檔案中添加一些樣式。我們還將獲取圖像 URL 的狀態和更新狀態的 onChage 函數。
ImageContainer.js 將是
import React from 'react'; import './ImageContainer.css'; const ImageContainer = () => { const [url, setUrl] = React.useState(''); const onChange = (e) => { const files = e.target.files; files.length > 0 && setUrl(URL.createObjectURL(files[0])); }; return ( <div className="image-container"> <div className="upload-container"> <input type="file" className="input-file" accept=".png, .jpg, .jpeg" onChange={onChange} /> <p>Drag & Drop here</p> <p>or</p> <p>Click</p> </div> </div> ); }; export default ImageContainer;
ImageContainer.css 將是
.image-container { width: 60%; height: 90%; display: flex; align-items: center; justify-content: center; border: 2px dashed rgba(0, 0, 0, .3); } .upload-container { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background-color: white; } .upload-container>p { font-size: 18px; margin: 4px; font-weight: 500; } .input-file { display: block; border: none; position: absolute; top: 0; left: 0; right: 0; bottom: 0; opacity: 0; }
步驟 5 —
現在我們將有條件地預覽影像檔案。如果您放置了影像,則會渲染該影像和/或渲染拖放區域。
ImageContainer.js 將是
import React from 'react'; import './ImageContainer.css'; const ImageContainer = () => { const [url, setUrl] = React.useState(''); const onChange = (e) => { const files = e.target.files; files.length > 0 && setUrl(URL.createObjectURL(files[0])); }; return ( <div className="image-container"> { url ? <img className='image-view' > <h2> Step 6 — </h2> <p>Now we will import the ImageContainer component in our App.js and run our application using the npm start command and have fun while coding.</p> <p><strong>App.js will be</strong><br> </p> <pre class="brush:php;toolbar:false">import './App.css'; import ImageContainer from './ImageContainer'; function App() { return ( <div className="App"> <h2 className="heading">Select Image:</h2> <div className="image-area"> <ImageContainer /> </div> </div> ); } export default App;
在本教學中,我們向您展示如何在 React 中僅使用 CSS 建立圖片拖放功能。
以上是在 React 中使用 CSS 拖放圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在本週的平台新聞綜述中,Chrome引入了一個用於加載的新屬性,Web開發人員的可訪問性規範以及BBC Move
