简体   繁体   English

在h1标签上移动时闪烁

[英]Blinking when moving over h1 tag

I have the following code which shows some text when hovering the image. 我有以下代码,将鼠标悬停在图像上时会显示一些文本。 But there is some problem when cursor is moving over h1 tag, It's blinking at that time. 但是,当光标移到h1标签上时会出现一些问题,当时它正在闪烁。 Why it's happening? 为什么会这样呢?

 jQuery(function($) { $('.content1').mouseover(function() { $('.content').show(); }); $('.content').mouseout(function() { $('.content').hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img class="content1" src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/2000px-HTML5_logo_and_wordmark.svg.png" style="width:100%;position:relative"> <div class="content" style="position: absolute; width: 100%; height: 100%; left: 0px; top: 0px; text-align: center; color: white; display: none; background: rgba(0, 0, 0, 0.901961);"> <h1 style="color:white">hiiiiiiiiiiiiiiiiiii</h1> </div> 

The reason for this is that when the mouse is over the <h1> , a mouseout is triggered on .content . 这样做的原因是,当鼠标在<h1>一个mouseout被触发.content To fix this, use pointer-events: none; 要解决此问题,请使用pointer-events: none; for the <h1> . <h1> Read about pointer-events on MDN . 阅读有关MDN上的指针事件的信息

<h1 style="color:white; pointer-events: none;">hiiiiiiiiiiiiiiiiiii</h1>

Demo: http://jsfiddle.net/j3zqgsou/ 演示: http//jsfiddle.net/j3zqgsou/

The issue is you're using "mouseover", this fires an event every time you switch containers with your mouse. 问题是您使用的是“ mouseover”,每次您用鼠标切换容器时都会触发一个事件。 for example, you can see this silly behaviour here : 例如,您可以在这里看到这种愚蠢的行为:

https://api.jquery.com/mouseover/ https://api.jquery.com/mouseover/

at the bottom. 在底部。

This can be proven by using a simple CSS line : 这可以通过使用简单的CSS行来证明:

h1{
    pointer-events: none;
}

Which makes the H1 tag completely transparent to the mouse. 这使得H1标签对鼠标完全透明。

Fiddle! 小提琴! https://jsfiddle.net/yy5afj0o/ https://jsfiddle.net/yy5afj0o/

You can use following CSS to prevent it: 您可以使用以下CSS来防止这种情况:

h1 {
    pointer-events: none;
}

I also tried to use mouseenter instead of mouseover and mouseleave instead of mouseout . 我还尝试使用mouseenter代替mouseover ,使用mouseleave代替mouseout

This worked for me: 这对我有用:

jQuery(function($) {
  $('.content1').mouseenter(function() {
    $('.content').show();
  });
  $('.content').mouseleave(function() {
    $('.content').hide();
  });
});

Fiddle: http://jsfiddle.net/qvuj48yr/1/ 小提琴: http : //jsfiddle.net/qvuj48yr/1/

Information: Jquery mouseenter() vs mouseover() 信息: jQuery mouseenter()vs mouseover()

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

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