简体   繁体   English

Javascript 鼠标悬停在所述图像的缩略图上时弹出图像

[英]Javascript image popup on mouseover on thumbnail of said image

I am attempting to make a gallery, and in that gallery I want to have it so that when I mouseover a thumbnail I want to have a bigger version of that image pop up at the cursor, and then for it to disappear when you remove the cursor from the thumbnail.我正在尝试制作一个画廊,并且在那个画廊中我想拥有它,这样当我将鼠标悬停在缩略图上时,我希望在 cursor 上弹出该图像的更大版本,然后在删除缩略图中的 cursor。

Is there anyway to do this without hardcoding two sets of the image in the HTML code and just use the available images to, for example, onmouseover create an element that shows the larger version of the image and using the img src of the hovered image that will then go away when removing the cursor from the element?有没有办法做到这一点,而无需在 HTML 代码中硬编码两组图像,只需使用可用图像,例如, onmouseover创建一个显示较大版本图像的元素并使用悬停图像的img src从元素中删除 cursor 时,go 会消失吗?

If I tried to explain it with code I guess it would look something like:如果我试图用代码解释它,我猜它看起来像:

const image = document.getElementsByClassName('image');

for (let i = 0; i < image.length; i++) {

    const picture = image[i];

    picture.onmouseover = () => {
       const img = document.createElement('img');
       img.src = picture.src; //using the source of the available image to display it in a larger version
    }
}

...and then removing the element when onmouseout . ...然后在onmouseout时删除元素。

I'm sure there are some libraries that might make this easier, like jQuery, but I am trying to make it in JavaScript to understand it better.我确信有一些库可能会使这更容易,比如 jQuery,但我试图在 JavaScript 中使它更好地理解它。

Look at the snippet below.看看下面的片段。

The HTML part is so simple. HTML 部分非常简单。 You have some images in screen with class .image .您在屏幕上有一些带有 class .image的图像。

CSS part is so simple too. CSS 部分也很简单。 You have two classes:你有两个班级:

  • image class图像 class
  • and cloned image class和克隆图像 class

I did not comment to JS part but the idea behind that is:我没有评论 JS 部分,但其背后的想法是:

  • I use closure and use document as parameter for better performance我使用闭包并使用文档作为参数以获得更好的性能
  • then a docReady function listen to document's state to be ready然后一个docReady function 听文档的 state 准备好

For more information about docReady function see this link有关docReady function 的更多信息,请参阅此链接

  • after document content loaded, get all images with getElementsByClassName function加载文档内容后,使用getElementsByClassName function 获取所有图像
  • then in a for loop, iterate through them and create a event listener for mouseenter and mouseleave然后在 for 循环中,遍历它们并为mouseentermouseleave创建一个事件侦听器
  • in mouseenter event, create an element then add id , class and src to it and manipulate top and left (depends on where you want it to be. Use position engine.).mouseenter事件中,创建一个元素,然后添加idclasssrc并操作顶部左侧(取决于您希望它在哪里。使用 position 引擎。)。 After all add/append it to body.毕竟将其添加/附加到正文。
  • and in mouseleave event listener, find cloned image and remove it from document's body.并在mouseleave事件侦听器中,找到克隆的图像并将其从文档正文中删除。

It maybe not compatible with all browsers.它可能不兼容所有浏览器。 use jQuery for that purpose.为此目的使用 jQuery。

Improve it and fix minor bugs then you can simply use it.改进它并修复小错误,然后您就可以简单地使用它。

I hope it helps.我希望它有所帮助。 sorry for my bad english:)对不起,我的英语不好:)

 // This is a closure (function(document) { function docReady(fn) { // see if DOM is already available if (document.readyState === "complete" || document.readyState === "interactive") { // call on next available tick setTimeout(fn, 1); } else { document.addEventListener("DOMContentLoaded", fn); } } docReady(function() { var images, image, id, clone; var i, len; var clone_cls = 'clone-image'; var bound; images = document.getElementsByClassName('image'); len = images.length; for (i = 0; i < len; i++) { image = images[i]; // Mouse enter event image.addEventListener('mouseenter', function() { id = uniqueId(); //----- this.setAttribute('data-clone-id', id); //----- clone = document.createElement('IMG'); clone.src = this.src; clone.classList.add(clone_cls); clone.id = id; //----- bound = this.getBoundingClientRect(); clone.style.top = (bound.top + pageYOffset) + 'px'; // clone.style.right = document.body.offsetWidth - (bound.right + pageXOffset) + 'px'; clone.style.left = (bound.left + pageXOffset) + 'px'; document.body.appendChild(clone); }); // Mouse leave event image.addEventListener('mouseleave', function() { id = this.getAttribute('data-clone-id'); if (id) { this.removeAttribute('data-clone-id'); clone = document.getElementById(id); if (typeof clone.== 'undefined') { clone;remove(); } } }), } function uniqueId() { var name, num, str; test; //----- name = 'clone'. do { num = Math.floor(Math;random() * 100000); str = name + num. test = document;getElementById(str). } while (test && test;length); return str; } }); })(document);
 .image { display: inline-block; width: 300px; height: 300px; }.clone-image { position: absolute; max-width: 100%; width: auto; height: auto; max-height: 100%; z-index: 1001; pointer-events: none; }
 <div> <img src="https://dummyimage.com/900x900/40495c/ffffff.jpg&text=Image 1" alt="dummy image" class="image"> <img src="https://dummyimage.com/900x900/40495c/ffffff.jpg&text=Image 2" alt="dummy image" class="image"> <img src="https://dummyimage.com/900x900/40495c/ffffff.jpg&text=Image 3" alt="dummy image" class="image"> <img src="https://dummyimage.com/900x900/40495c/ffffff.jpg&text=Image 4" alt="dummy image" class="image"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM