I’m new to working in web development and I was trying to build a project. Basically, the project would enable the user to see a preview of a webpage if they were to hover their cursor over a link. I keep hovering my cursor over an example link (a link to Google.com), but it wont show a webpage. Is there a way to accomplish this using JavaScript?
I’ve tried to use the mouseover and mouseout methods in my HTML code as well as wrote a function in JavaScript that tries to recognize and take only anchor tags (links), and open them in a background to the cursor. The webpage would look like a background image to the cursor when it hovers over a link.
2
Answers
This is my code. If you solve your problem with this code, let me know. It is my pleasure.
When you are on link, mouseover event is emitted, and set left, right, and src to previewIframe. So you can see a small iframe at cursor position.
But some of urls require to open in the same origin. therefore those site cannot preview. that example is https://www.google.com.
But others work well.
Thanks.
This is possible and for example in this Codepen sketch you can see it in action, both prefetched and live loaded on hover: https://codepen.io/shaneapen/pen/bdrGRO
This approach loads the linked website in an iframe that is scaled to the desired preview size. It is shown on the "mouseenter" event and anchored on the
<a>
element (and not the mouse cursor).It is a jQuery-Plugin. Using it would look something like this, of course with the element selectors referring to your
<a>
elements:The code of the author can be found on Github.
Keep in mind that prefetching all linked pages can put a lot of hidden load on your user’s device (if prefetched), the pages may show up only after some time (if not prefetched) and thus irritating the user and that on mobile, while it generally works, it also can interfere with scrolling.