Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
           CodeHim.com

Free Web Design Code & Scripts

       
  • Home
  • Code Snippets
    • Accordion
    • Creative
    • Carousel
    • Date & Time
    • Gallery
    • LightBox
    • Menu & Nav
    • Modal
    • Others
    • Portfolio
    • Social Media
    • Text & Input
    • Video Player
    • Zoom
  • HTML & CSS
  • Bootstrap
  • JavaScript
  • Collections
  • About
  • Contact
  • Buy Me a Coffee

Home / Vanilla JavaScript / JavaScript Link Preview on Hover

JavaScript Link Preview on Hover

January 22, 2024January 14, 2024 by Asif Mughal
JavaScript Link Preview on Hover
Share This:
Code Snippet:Link Preview on Hover
Author: Elliott
Published: January 14, 2024
Last Updated: January 22, 2024
Downloads: 1,801
License: MIT
Edit Code online: View on CodePen
Read More
 Demo
Download (File path is not allowed!)

The JavaScript code snippet helps you to preview the link on hover. When you hover over a link, it reveals an image, title, and description in a card. This feature helps users quickly preview linked content without clicking.

You can use this code in your web project to provide a convenient link preview feature. Moreover, you can customize the preview card with additional CSS according to your website’s design.

How to Create Link Preview On Hover Using JavaScript

1. To get started, add the HTML structure to your webpage. Inside a <section>, create links that you want to have link previews for. Each link should have the class “link-with-preview” and custom data attributes for image, title, and text.

<section>
	<h1>Link Preview</h1>
	<p>
		Once upon a time there were two
		<a href="https://www.codepen.io/wildtype" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="https://images.unsplash.com/photo-1587042538225-e30d1247eddd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" data-title="Mountain Friends 🏔" data-text="Introduced by their tectonic plate neighbors when they were very young.">
			mountain friends
		</a>
		with a
		<a href="https://www.codepen.io/wildtype" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="https://images.unsplash.com/photo-1506355683710-bd071c0a5828?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" data-title="Raging River 🌊" data-text="With the exception that this one is not very exciting.">raging river
		</a>
		between. That's the end of
		<a href="https://www.codepen.io/wildtype" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="https://images.unsplash.com/photo-1516202180772-d888b16cf6dd?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=80" data-title="Our Story" data-text="Looking at all these pictures makes me really not want to be inside right now.">our story</a>.
	</p>

	<div class="card">
		<img src="" class="card-img-top" />
		<div class="card-body">
			<h5 class="card-title"></h5>
			<p class="card-text"></p>
		</div>
	</div>
</section>

2. Apply some CSS to style the link previews and the card that will display the preview information. Customize the appearance to match your website’s design.

:root {
	font-size: 16px;
}

body {
	font-size: 24px;
	font-family: sans-serif;
}

h1 {
	font-weight: 600;
	line-height: 1.5;
}

section a {
	color: rgb(255, 144, 85);
}

section a:hover {
	text-decoration: none;
	color: rgb(255, 180, 89);
}

.card {
       background: #fff;
	width: 15rem;
	display: none;
	font-size: 1rem;
	color: black;
	position: absolute;
	z-index: 100;
	bottom: 30px;
	left: 50%;
	transform: translateX(-50%);
       box-shadow: 1px 2px 8px rgba(0, 0, 0, 0.35);
}

.link-with-preview {
	position: relative;
}

.card img {
	width: 15rem;
}

.card-title {
	font-size: 1.25rem;
}
.card-title,
.card-text{
    padding: 12px;
}
@media (max-width: 600px) {
	:root {
		font-size: 10px;
	}
	h1 {
		font-size: 4rem;
	}
}

3. Finally, copy and paste the following JavaScript code into your website’s JavaScript file. This code defines functions for showing and hiding link previews and adds event listeners to the links.

const card = document.querySelector(".card");

const hideLinkPreview = () => card.style.display = "none";

const showLinkPreview = e => {
  const image = e.currentTarget.getAttribute("data-image");
  card.querySelector("img").setAttribute("src", image);

  const title = e.currentTarget.getAttribute("data-title");
  card.querySelector("h5").textContent = title;

  const text = e.currentTarget.getAttribute("data-text");
  card.querySelector("p").textContent = text;

  e.currentTarget.appendChild(card);

  return card.style.display = "inline-block";
};

document.querySelectorAll(".link-with-preview").forEach(el => {
  el.addEventListener("mouseover", showLinkPreview);
  el.addEventListener("mouseleave", hideLinkPreview);
});

That’s all! hopefully, you have successfully created a link preview feature on your website. If you have any questions or suggestions, feel free to comment below.

Similar Code Snippets:

  • Scroll Progress Bar in JavaScript

    Scroll Progress Bar in JavaScript

  • Crop Image in Canvas Using JavaScript

    Crop Image in Canvas Using JavaScript

  • Resize Image JavaScript Using Canvas

    Resize Image JavaScript Using Canvas

  • Simple HTML Color Mixer in JavaScript

    Simple HTML Color Mixer in JavaScript

  • JavaScript Timer Countdown Minutes Seconds

    JavaScript Timer Countdown Minutes Seconds

  • Product Recommendation Based On Selection in JavaScript

    Product Recommendation Based On Selection in JavaScript

  • JavaScript Detect Media Query Change

    JavaScript Detect Media Query Changes

  • Find Duplicates in Array JavaScript

    Find Duplicates in Array JavaScript

  • Sticky Notes JavaScript Source Code

    Sticky Notes JavaScript Source Code

Asif Mughal

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.

Categories Vanilla JavaScript
Bootstrap 5 Toast Dynamic Message
JavaScript Parallax Tilt Effect Cards on Hover

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Categories

    • Accordion 22
    • Bootstrap 81
      • Bootstrap Accordion 1
      • Bootstrap Buttons 4
      • Bootstrap Cards 2
      • Bootstrap Carousel 2
      • Bootstrap Dropdowns 2
      • Bootstrap Forms 6
      • Bootstrap Jumbotron 1
      • Bootstrap Modals 2
      • Bootstrap Navbars 6
      • Bootstrap Panels 1
      • Bootstrap Progress Bars 1
      • Bootstrap Text & Input 3
    • Carousel 58
    • Chart & Graph 15
    • Date & Time 49
    • Gallery 39
    • HTML5 & CSS3 201
    • Layout 17
    • LightBox 19
    • Menu & Nav 95
    • Modal 18
    • Others 58
    • Portfolio 8
    • Social Media 10
    • Text & Input 120
    • Vanilla JavaScript 163
    • Zoom 12

You May Like

Alert (7) Analog Clock (4) Analog Clocks (5) Back to Top (4) Calculator (16) Calendar (5) Chart (3) Color Picker (4) Contact Us Forms (5) Countdown Timer (6) Datepicker (9) Digital Clocks (10) Drag and Drop (4) Dropdown Menu (23) File Uploader (4) Hamburger Menu (9) Horizontal Menu (24) Hover Effects (5) HTML5 Canvas (8) Image Sliders (40) Mega Menu (11) Modal (5) Multi-Level Menu (9) News Ticker (4) Notification (9) Off-Canvas Menu (10) Pagination (5) Parallax (8) Popup Lightbox (20) Progress Bar (5) Range Sliders (4) Scrolling Effects (28) Search Box (4) Select Dropdown (4) Shopping Cart (3) Side Menu (16) Slideshow (19) Social Share Buttons (3) Tabs (9) Text (11) Timeline (5) Timer (4) Tooltip (4) Typing Effect (6) Vertical Menu (14)

Latest Code Snippets

  • Common Open Source Security Vulnerabilities and How to Mitigate ThemCommon Open Source Security Vulnerabilities and How to Mitigate Them
    October 12, 2024
  • Animating Grid-column on Hover Using CSSAnimating Grid-column on Hover Using CSS
    April 1, 2024
  • Star Trails Animation On Mousemove in JavaScriptStar Trails Animation On Mousemove in JavaScript
    March 31, 2024
  • Gravity Button Using Pure CSSGravity Button Using Pure CSS
    March 31, 2024
  • Padlock Toggle Switch in CSSPadlock Toggle Switch in CSS
    March 31, 2024
  • Scroll-driven Animations in Pure CSSScroll-driven Animations in Pure CSS
    March 31, 2024
  • Interactive Notification Center UI in Vanilla JSInteractive Notification Center UI in Vanilla JS
    March 31, 2024
  • Emoji Spinner in Vanilla JavaScriptEmoji Spinner in Vanilla JavaScript
    March 31, 2024
  • Reveal Secret Code Using CSSReveal Secret Code Using CSS
    March 31, 2024
  • Toggle Button in CSS With Stretchable Elastic EffectToggle Button in CSS With Stretchable Elastic Effect
    March 31, 2024

Popular

  • 65+ Login Page in HTML with CSS Code Sample Simple to Difficult - 947,410 views
  • 25+ Best JavaScript Shopping Cart Examples with Demo - 211,942 views
  • Bootstrap Multiselect Dropdown with Checkboxes - 150,950 views
  • Bootstrap 5 Buttons with Icon and Text Tutorial & Demo - 112,360 views
  • 19+ Bootstrap Select Dropdown with Search Box Tutorial & Examples - 101,041 views
  • Bootstrap 5 Sidebar Menu with Submenu Collapse/Hover Tutorial Demo - 88,106 views
  • 99+ Social Media Buttons HTML Code Sample & Tutorial - 86,393 views
  • 19+ Bootstrap 5 Mega Menu Responsive/Drop Down Examples - 86,390 views
  • Bootstrap 4 Modal Popup Login Form Tutorial & Demo - 82,150 views
  • Bootstrap Vertical Menu with Submenu on Click - 80,202 views

Advertisement

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

CodeHim
  • About
  • Contact Us
  • Terms and Conditions
  • Privacy Policy
Top Categories
  • Bootstrap Snippets
  • JavaScript Snippets
  • HTML CSS Snippets
  • Menu & Nav Snippets
Get in Touch

Follow us on social media to be updated with latest web design code & scripts...

All Rights Reserved © 2026 - CodeHim Inc

Please Rel0ad/PressF5 this page if you can't click the download/preview link

F5
X