React 是一個用於建立使用者介面的流行 JavaScript 函式庫,其靈活性和多功能性使其成為建立互動式應用程式的絕佳選擇。在本教學中,我們將向您展示如何在 React 中僅使用 CSS 建立圖片拖放功能。
首先,讓我們建立一個 React 專案。您可以使用 Create React App 或任何其他最適合您的設定方法。讓我們使用 create-react-app 製作一個 React 應用程式。
npx create-react-app drag-and-drop
將 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; }
現在在 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); }
現在我們將在 .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; }
現在我們將有條件地預覽影像檔案。如果您放置了影像,則會渲染該影像和/或渲染拖放區域。
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中文網其他相關文章!