簡體   English   中英

為什么只有第一個鼠標懸停和鼠標懸停有效?

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

我已經加載了一組圖像,並在它們上應用了.mouseout().mouseover() 問題在於,在第一個.mouseover()事件之后,圖像會變大,並且在.mouseout()之后,圖像會恢復為先前的大小。 第一次之后,沒有.mouseover()事件在Firefox中觸發,但是在Chrome中有效。 問題是Chrome z-index屬性不會將mouseover圖像放在其他圖像之上。 這些問題的原因是什么,我該如何解決?

 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); } 

也許您的圖像處於z-index大於-1其他元素之下。 在將mouseout嘗試this.style.zIndex = '0'

您似乎將圖像的z-index-1 有什么理由嗎?

嘗試設置this.style.zIndex = '1'; 在mouseout上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM