Home >Web Front-end >CSS Tutorial >How to Create Live Page Previews in Popups Using Iframes and JavaScript?
How to Implement Live Previews in Popups for Linked Pages
In this scenario, you have a requirement to display a live preview of the linked page in a compact popup when the mouse hovers over the link. To achieve this, we can utilize an iframe to dynamically load and display the linked page's preview.
One way to accomplish this is by using JavaScript and CSS. Here's how you can implement it:
1. Markup:
<code class="html"><div class="box"> <iframe src="" width="500px" height="500px"></iframe> </div> <a href="https://en.wikipedia.org/">Wikipedia</a></code>
2. CSS:
<code class="css">.box { display: none; width: 100%; } a:hover + .box, .box:hover { display: block; position: relative; z-index: 100; }</code>
3. JavaScript:
<code class="js">// Get elements const iframe = document.querySelector('iframe'); const links = document.querySelectorAll('a'); // Add event listeners links.forEach(link => { link.addEventListener('mouseover', () => { iframe.src = link.href; }); });</code>
How it Works:
When the cursor hovers over the link, JavaScript detects the event and dynamically updates the src attribute of the iframe to the link's target URL. Subsequently, the iframe loads the preview of the linked page and displays it in the popup box.
The above is the detailed content of How to Create Live Page Previews in Popups Using Iframes and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!