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 id="Select-Image">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" alt="在 React 中使用 CSS 拖放圖片" > <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 id="Select-Image">Select Image:</h2> <div classname="image-area"> <imagecontainer></imagecontainer> </div> </div> ); } export default App;
在本教學中,我們向您展示如何在 React 中僅使用 CSS 建立圖片拖放功能。
以上是在 React 中使用 CSS 拖放圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

我知道,我知道:有大量的內容管理系統選項可用,而我進行了幾個測試,但實際上沒有一個是一個,y&#039;知道嗎?怪異的定價模型,艱難的自定義,有些甚至最終成為整個&

鏈接CSS文件到HTML可以通過在HTML的部分使用元素實現。 1)使用標籤鏈接本地CSS文件。 2)多個CSS文件可通過添加多個標籤實現。 3)外部CSS文件使用絕對URL鏈接,如。 4)確保正確使用文件路徑和CSS文件加載順序,優化性能可使用CSS預處理器合併文件。

選擇Flexbox還是Grid取決於佈局需求:1)Flexbox適用於一維佈局,如導航欄;2)Grid適合二維佈局,如雜誌式佈局。兩者在項目中可結合使用,提升佈局效果。

包含CSS文件的最佳方法是使用標籤在HTML的部分引入外部CSS文件。 1.使用標籤引入外部CSS文件,如。 2.對於小型調整,可以使用內聯CSS,但應謹慎使用。 3.大型項目可使用CSS預處理器如Sass或Less,通過@import導入其他CSS文件。 4.為了性能,應合併CSS文件並使用CDN,同時使用工具如CSSNano進行壓縮。

是的,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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

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

Atom編輯器mac版下載
最受歡迎的的開源編輯器