Hello everyone. Today I am making a jquery plug-in for image magnifier. It is similar to the magnifying glass for product pictures on JD.com.
I found that after I bound mouseenter and mouseleave to the image object, when the mouse moves on the image, mouseenter and mouseleave will be triggered at the same time, causing flickering. But I don't expect mouseenter and mouseleave to be called repeatedly, or even at the same time. How to solve it? I have tried several methods online but to no avail.
Thank you all.
$.fn.magnify = function() { //var glassBox = options.glassBox || {}; //var detailBox = options.detailBox || {}; var glassBox = $('<span></span>'); glassBox.css({ "width": this.width() / 2 + 'px', "height": this.height() /2 + 'px', "background-color": 'grey', "position": "absolute", "left" : '0px', "top": '0px', 'opacity': '0.2', 'display': 'none', 'border': '1px solid blue', 'z-index': 10 }); $(this).after(glassBox); //glassBox.hide(); var detailBox = $('<div></div>'); var detailImage = $('<img src="/static/imghwm/default1.png" data-src="jquery-1.11.3.js" class="lazy" alt="jQuery: mouseenter event and mouseleave event cause flickering problem" ></img>'); detailBox.css({ "width": this.width(), "height": this.height(), "display": 'none', "position": 'relative', "overflow": 'hidden' //"background-color": 'lime' }); detailImage.css({ "position": 'absolute' }); detailImage.attr("src", this.attr("src")); detailBox.append(detailImage); this.after(detailBox); this.mouseenter(function(event){ //event.stopPropagation(); glassBox.get(0).style.display = 'block'; detailBox.get(0).style.display = 'block'; }); this.mouseleave(function(event){ //event.stopPropagation(); if (event.relatedTarget !== "span") { glassBox.get(0).style.display = 'none'; detailBox.get(0).style.display = 'none'; } }); this.mousemove(function(event) { /* Act on the event */ var x = event.pageX - this.offsetLeft - glassBox.width() / 2; var y = event.pageY - this.offsetTop - glassBox.height() /2; if (x < 0 ) { x = 0; } else if ( x > this.offsetWidth - glassBox.width()) { x = this.offsetWidth - glassBox.width(); } if (y < 0) { y = 0; } else if (y > this.offsetHeight - glassBox.height()) { y = this.offsetHeight - glassBox.height(); } glassBox.css({ left: x + 'px', top: y + 'px' }); detailImage.css({ "left": -3.5 * (event.pageX - this.offsetLeft) + 'px', "top": -3.5 * (event.pageY - this.offsetTop) + 'px' }); }); }
HTML is as follows:
<html> <head> <script ></script> <script src="magnify.js"></script> </head> <style> #imgTarget{ width: 300px; height: 200px; } #imageArea{ position: relative; } </style> <body> <div id="imageArea"> <img src="/static/imghwm/default1.png" data-src="car.jpg" class="lazy" id="imgTarget" / alt="jQuery: mouseenter event and mouseleave event cause flickering problem" > </div> <script> $( document ).ready( function(){ $("#imgTarget").magnify(); } ); </script> </body> </html>
The reason for flickering is as follows:
You bound the mouseenter mouseleave event to img
when mouseenter img , you generated another glassBox
glassBox on the upper layer of img, thus blocking img and the mouse cursor, so a mouseleave
was generated for img, so the code set glassBox display=none again , the mouse can re-enter to img
Solution 1
Instead of using mouseenter mouseleave, directly judge whether the mouse position is within the range of img in mousemove.
Solution 2
Create an overlay with the same size as img:
<div id="imageArea"> <img src="/static/imghwm/default1.png" data-src="card.jpg" class="lazy" id="imgTarget" / alt="jQuery: mouseenter event and mouseleave event cause flickering problem" > <div id="overlay"></div> </div>
#overlay { position: absolute; top: 0; left: 0; width: 300px; height: 200px; background: red; opacity: 0.5; z-index: 3; }
Ensure the z-index order of img glassBox overlay:
glassBox.css({ "width": this.width() / 2 + 'px', "height": this.height() /2 + 'px', "background-color": 'grey', "position": "absolute", "left" : '0px', "top": '0px', 'opacity': '0.2', 'display': 'none', 'border': '1px solid blue', 'z-index': 2 // 保证glassBox在overlay的下面,在img的上面 });
Events are bound On the overlay:
$( document ).ready( function(){ $("#overlay").magnify(); } );
The above is the detailed content of jQuery: mouseenter event and mouseleave event cause flickering problem. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft