简体   繁体   English

为什么只有第一个鼠标悬停和鼠标悬停有效?

[英]Why is only the first mouse-over and mouse-out working?

I have loaded a set of images and applied .mouseout() and .mouseover() on them. 我已经加载了一组图像,并在它们上应用了.mouseout().mouseover() The problem is that after the first .mouseover() event the image gets larger and after .mouseout() is fired the image is returned back to its previous size. 问题在于,在第一个.mouseover()事件之后,图像会变大,并且在.mouseout()之后,图像会恢复为先前的大小。 After the first time, no .mouseover() event gets fired in Firefox, but in Chrome it works. 第一次之后,没有.mouseover()事件在Firefox中触发,但是在Chrome中有效。 The problem is that the Chrome z-index property does not put the mouseover image on top of other images. 问题是Chrome z-index属性不会将mouseover图像放在其他图像之上。 What is the reason for these problems and how can I solve them? 这些问题的原因是什么,我该如何解决?

 var images = ['http://www.rd.com/wp-content/uploads/sites/2/2016/04/01-cat-wants-to-tell-you-laptop.jpg', 'http://r.ddmcdn.com/s_f/o_1/cx_462/cy_245/cw_1349/ch_1349/w_720/APL/uploads/2015/06/caturday-shutterstock_149320799.jpg', 'http://r.ddmcdn.com/s_f/o_1/cx_462/cy_245/cw_1349/ch_1349/w_720/APL/uploads/2015/06/caturday-shutterstock_149320799.jpg', 'http://pershanpet.ir/wp-content/uploads/2016/05/144-jpravafcbn.jpg', 'http://www.animal-whisper.com/images/pic28.jpg']; function loadImage(url) { var promise = new Promise((resolve, reject) => { var image = new Image(); image.onload = function() { resolve(image); }; image.onerror = function() { var msg = "could not load image at url " + url; reject(new Error(msg)); }; image.src = url; image.style.width = '200px'; image.style.height = '200px'; }); return promise; } Promise.all( images.map(function(elem) { return loadImage(elem); }) ).then((img) => { img.forEach(each => { each.addEventListener('mouseover', function(event) { this.style.zIndex = '2000'; this.style.transform = 'scale(1.5,1.5)'; }); each.addEventListener('mouseout', function(event) { this.style.transform = 'scale(1,1)'; this.style.zIndex = '-1'; }); addImg(each); }); }); function addImg(img) { document.body.appendChild(img); } 

Maybe your images get under some other elements with z-index that is greater than -1 . 也许您的图像处于z-index大于-1其他元素之下。 On mouseout try this.style.zIndex = '0' . 在将mouseout尝试this.style.zIndex = '0'

It appears that you're setting the image's z-index to -1 . 您似乎将图像的z-index-1 Any reason for that? 有什么理由吗?

Try setting this.style.zIndex = '1'; 尝试设置this.style.zIndex = '1'; on mouseout. 在mouseout上。

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

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