今天遇到一个问题,想实现一个功能:
当鼠标移到div1上的时候,会出现div2。出现时div2在div1的上面,div2在出现后发生闪烁的问题。
于是开始找问题根源,发现原来是因为当我们触发div1的时候,div2出现,但是div2是存在于div1上面的,所以当div2出现后,会又一次触发下面div1的事件。通常我们可能给的事件是mouseover和mouseout,因为两个div叠加,div2出现时会多次触发div1的事件,所以就会发生闪烁问题。
解决:
1.一开始换mouseenter和mouseleave,但是发现还是一样的问题。
注:
mouseover()与mouseout() 表示鼠标移入和移出的时候触发,穿过子元素也会触发
mouseenter()和mouseleave() 表示鼠标穿过和穿出时候触发,穿过子元素不会触发
2.然后又添加e.stopPropagation();阻止冒泡和e.preventDefault();阻止默认事件,还是没有对症。
注:
e.stopPropagation(); //阻止冒泡之后,就不会形成冒泡向上传递了。
e.preventDefault(); //阻止默认行为
3.最后换为切换事件toggle切换事件也不顶事儿。
4.最终,如果想用js解决这个问题可能不容易,用js基本都会发生闪烁问题。那么我们使用css方式是不是可以解决呢?网上查了很多资料,发现也有很多人遇到这个问题,使用的是css中的hover来解决的。
具体使用方法:
给两个div的父元素,也就是共同包裹两个div的盒子一个hover,当父元素hover时,div2的样式设置为display:block;于是就顺利解决了这个问题,不会再出现闪烁问题。
下面上代码:
html部分:
<div class="fudiv"> <span class="div1"><img src="/static/imghwm/default1.png" data-src="img/jixinjia-partner1.jpg" class="lazy" alt=""/></span> <div class="div2"> <div class="box6"><img src="/static/imghwm/default1.png" data-src="img/qr-code-bg1%20(1).jpg" class="lazy" alt="p"/></div> <p>扫码有奖</p> </div></div>
css部分:
.fudiv:hover .div2 { display: block;}.div2{ display: none;}
原本jquery部分,这种方式会出现闪烁问题
<script> $(document).ready( function(){ $(".div1").mouseover( function(e){ e.stopPropagation(); $(".div2").show(); } ); $(".div1").mouseout( function(e){ e.stopPropagation(); $(".div2").toggle(); } ); } );</script>

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
