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"> const { useRef } = React function App() { const messagesRef = useRef(null); const messages = [ { 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' }, { id: 7, text: 'item 7' }, { id: 8, text: 'item 8' }, { id: 9, text: 'item 9' } ]; const scrollToBottom = () => { const container = messagesRef.current; container.scrollTop = container.scrollHeight; }; return ( <div className='container'> <h3>React Js Scroll div to bottom</h3> <div className="scrollable" ref={messagesRef}> {messages.map(message => ( <div key={message.id} className="message">{message.text}</div> ))} </div> <button onClick={scrollToBottom}>scrollToBottom</button> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 500px; 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; } .scrollable { width: 100%; max-height: 300px; overflow-y: scroll; padding: 10px; border: 1px solid #ccc; border-radius: 5px; } .message { background-color: #f2f2f2; padding: 10px; margin-bottom: 10px; border-radius: 5px; } button { background-color: #4caf50; color: white; padding: 10px 20px; border: none; border-radius: 5px; margin-top: 10px; cursor: pointer; } button:hover { background-color: #45a049; } button:focus { outline: none; } </style> </body> </html>