screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React function App() { const [items, setItems] = useState([ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, { id: 4, text: 'Item 4' }, { id: 5, text: 'Item 5' }, { id: 6, text: 'Item 6' }, ]); const handleDragStart = (e, index) => { e.dataTransfer.setData('index', index); }; const handleDragOver = (e) => { e.preventDefault(); }; const handleDrop = (e, newIndex) => { e.preventDefault(); const oldIndex = e.dataTransfer.getData('index'); const newItems = [...items]; const [draggedItem] = newItems.splice(oldIndex, 1); newItems.splice(newIndex, 0, draggedItem); setItems(newItems); }; return ( <div className='container'> <h2>React Js Drag and Drop List Example</h2> <ul> {items.map((item, index) => ( <li key={item.id} draggable onDragStart={(e) => handleDragStart(e, index)} onDragOver={handleDragOver} onDrop={(e) => handleDrop(e, index)} > {item.text} </li> ))} </ul> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { text-align: center; padding: 20px; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); margin: 0 auto; max-width: 600px; } ul { list-style: none; padding: 0; } li { padding: 10px; margin: 5px 0; background-color: #fff; border: 1px solid #ccc; border-radius: 5px; cursor: pointer; transition: background-color 0.3s, transform 0.3s; } li:hover { background-color: #f0f0f0; } li.drag-over { border: 2px dashed #999; } </style> </body> </html>