screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/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> <!-- Latest compiled and minified CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- Latest compiled JavaScript --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useRef, useEffect } = React; function App() { const [draggable, setDraggable] = useState(false); const [resizable1Width, setResizable1Width] = useState(20); const startDrag = () => { setDraggable(true); document.addEventListener("mousemove", handleDrag); document.addEventListener("mouseup", stopDrag); }; const handleDrag = (event) => { if (draggable) { const draggableWidth = event.clientX - blockRef.current.getBoundingClientRect().left; const blockWidth = blockRef.current.offsetWidth; const newResizable1Width = (draggableWidth / blockWidth) * 100; setResizable1Width(newResizable1Width.toFixed(2)); } }; const stopDrag = () => { setDraggable(false); document.removeEventListener("mousemove", handleDrag); document.removeEventListener("mouseup", stopDrag); }; const blockRef = React.useRef(null); return ( <div className='container'> <h3>ReactJS Resize div on drag and drop</h3> <div className="Block" ref={blockRef}> <div id="Resizable1" style={{ width: `${resizable1Width}%`, backgroundColor: "#C51162" }} className="ResizableBlock" ></div> <div id="Draggable" draggable="true" onMouseDown={startDrag} className="Draggable" ></div> <div id="Resizable2" style={{ width: `${100 - resizable1Width}%`, backgroundColor: "#AA00FF" }} className="ResizableBlock" ></div> </div> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); width: 600px; margin: 0 auto; padding: 20px; } .Block { display: flex; align-items: center; } .ResizableBlock { border: 1px solid black; height: 200px; } .Draggable { background: rgba(1, 1, 1, 0.2); border-bottom: 1px solid black; border-right: 1px solid black; border-top: 1px solid black; cursor: col-resize; height: 200px; width: 10px; } </style> </body> </html>