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> </head> <body> <div id="app"></div> <script type="text/babel"> function App() { const scrollToElement = (elementId) => { const element = document.getElementById(elementId); if (element) { // Scroll to the top of the element element.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }; return ( <div className='container'> <h3>React Js Scroll to Element</h3> <button onClick={() => scrollToElement('elementToScrollTo')}> Scroll to div1 </button> <button onClick={() => scrollToElement('div2')}> Scroll to div2 </button> <button onClick={() => scrollToElement('div3')}> Scroll to div3 </button> <div id="elementToScrollTo" style={{ height: '300px', backgroundColor: 'blue' }}> <h3>Element 1</h3> {/* Content for the elementToScrollTo */} </div> <div id="div2" style={{ height: '300px', backgroundColor: 'red' }}> <h3>Element 2</h3> {/* Content for the first additional div */} </div> <div id="div3" style={{ height: '300px', backgroundColor: 'yellow' }}> <h3>Element 3</h3> {/* Content for the second additional div */} </div> <div id="div4" style={{ height: '300px', backgroundColor: 'pink' }}> <h3>Element 4</h3> {/* Content for the second additional div */} </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; 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); padding: 20px; } button { padding: 10px 15px; font-size: 16px; background-color: #4CAF50; color: #fff; border: none; border-radius: 4px; cursor: pointer; margin: 20px; } button:hover { background-color: #45a049; } </style> </body> </html>