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() { // Create refs for each image element const imageRefs = [ useRef(null), useRef(null), useRef(null), useRef(null), useRef(null) ]; // Function to handle the scroll action for a specific image const scrollToImage = (index) => { imageRefs[index].current.scrollIntoView({ behavior: 'smooth', block: 'center', // Change 'nearest' to 'center' inline: 'center', }); }; return ( <div className='container'> <h3>React Js Center Image in carousel onclick button</h3> <div className="image-buttons"> {/* Image 1 */} <button className="image-button" onClick={() => scrollToImage(0)}>Image 1</button> <button className="image-button" onClick={() => scrollToImage(1)}>Image 2</button> <button className="image-button" onClick={() => scrollToImage(2)}>Image 3</button> <button className="image-button" onClick={() => scrollToImage(3)}>Image 4</button> <button className="image-button" onClick={() => scrollToImage(4)}>Image 5</button> </div> <div className="image-container"> <div ref={imageRefs[0]} className="image-wrapper"> <img src={encodeURI("https://www.sarkarinaukriexams.com/images/import/sne4446407201.png")} alt="Image 1" /> </div> <div ref={imageRefs[1]} className="image-wrapper"> <img src={encodeURI("https://www.sarkarinaukriexams.com/images/import/sne86811832.png")} alt="Image 2" /> </div> <div ref={imageRefs[2]} className="image-wrapper"> <img src={encodeURI("https://www.sarkarinaukriexams.com/images/import/sne10272423583.png")} alt="Image 3" /> </div> <div ref={imageRefs[3]} className="image-wrapper"> <img src={encodeURI("https://www.sarkarinaukriexams.com/images/import/sne1586776004.png")} alt="Image 4" /> </div> <div ref={imageRefs[4]} className="image-wrapper"> <img src={encodeURI("https://www.sarkarinaukriexams.com/images/import/sne20464172895.png")} alt="Image 5" /> </div> </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { max-width: 500px; margin: 0 auto; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } .image-container { display: flex; overflow-x: scroll; padding: 0 20px; /* Add padding to the left and right sides */ } .image-wrapper { margin: 0 5px; } img { width: 300px; height: auto; margin: 0 5px; } .image-buttons { display: flex; justify-content: center; margin-bottom: 10px; } .image-button { margin: 5px; } </style> </body> </html>