在 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脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)