【问题标题】:Image overlay is flickering?图像叠加闪烁?
【发布时间】:2012-05-03 10:59:32
【问题描述】:

****事实是所有这些解决方案都有效,只是不适用于项目,所以我将重新提出问题,略有不同****

基本上我有一个图像,当有人将鼠标光标移到它上面时,它会显示一个 div(其中包含一个图像 - 也就是播放按钮)。当它们移动时,它们的光标在图像之外,播放按钮就会消失。

它可以工作,但是如果你将诅咒移到播放按钮上,它会像疯了一样闪烁,因此我必须以某种方式混淆我的事件..

提前感谢您的帮助...

HTML

<div id="cont">
    <div id="art"  style= "position: absolute; left: 20px; top: 40px;">
        <a href="#"><img src="http://www.newmusicproducer.com/img/ic_play.png"></a>
    </div>
    <img src="https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275" alt="smile" id="imgSmile"></img>

jQuery

$(document).ready(function() {
    $('#art').hide();             
    $('#imgSmile').mouseenter(function() {
        $('#art').show();
    });

    $('#imgSmile').mouseleave(function() {   
        $('#art').hide();
    });
});​

Here's a fiddle

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    仅使用 CSS 就可以达到同样的效果:

    <div>
        <a href="#"><img src="http://www.newmusicproducer.com/img/ic_play.png"></a>
    </div>
    
    div
    {
     background-image:url('https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275');
        width:100px;
        height:100px;
        position:relative;
    }
    div a
    {
     position:absolute;
        top:20px;
        left:30px;
        display:none;
    }
    div:hover a
    {
        display:block;
    }
    

    ​-- SEE DEMO --


    编辑: 如果您需要在具有不同链接 URL 的同一页面上多次使用它,我制作了一个需要最少 HTML 的版本,使用一点 jQuery 来应用其余部分:

    <div class="play kirkbridge" data-playUrl="/myPlayUrl"></div>
    

    http://jsfiddle.net/tW68d/16/

    这可能有点矫枉过正,这取决于你的使用情况。

    【讨论】:

    • +1 不错的解决方案。虽然如果您需要完全兼容浏览器,它在
    • @jamcoupe 你是对的 IE7 很好,我的错。但它肯定不能在 IE6 中工作。
    • @RoryMcCrossan 它也不适用于诺基亚 3210。如果用户使用这些技术中的任何一种,他们不应该期望事情能正常工作。
    • @Curt LOL 我知道:D 只是确保人们在决定如何做某事时了解所有事实。事实上 IE6 仍然占有约 7% 的份额,这意味着你还不能完全忽略它。不幸的是。
    • @RoryMcCrossan 是的,这是真的!
    【解决方案2】:

    问题是鼠标悬停在新图像上导致原始图像上的鼠标悬停。尝试改用hover(),同时将这两个元素作为选择器。

    $(document).ready(function() {
        $('#art').hide();
        $('#imgSmile, #art').hover(
            function() {
                $('#art').show();
            }, function() {
                $('#art').hide();
            }
        );
    });
    

    Updated fiddle

    【讨论】:

    • 我在 FF12 中没有闪烁 - 你使用的是什么浏览器?
    • 我正在使用 Chrome,但它必须在所有浏览器中工作,尤其是 IE。上面的 CSS 示例在 IE9 中确实有效。
    • 我刚刚在 Chrome 18.0.1025.168 m 中测试过,那里也不闪烁?
    【解决方案3】:

    这是另一种方法:

    http://jsfiddle.net/aramk/ZqVZ6/1/

    $(document).ready(function(){
    
        var art = $('#art');
        art.hide();
        var updatePlayButton = function() {
            if (art.is(':visible')) {
                art.hide();
            } else {
                art.show();
            }
        }
    
        $('#cont').hover(updatePlayButton);
    
    });​
    

    我会使用包装 DIV,因为当按钮与微笑图像重叠并且你的鼠标在它上面时,你会失去微笑图像的焦点并开始闪烁。这将避免这种情况。

    【讨论】:

      【解决方案4】:

      如果可能的话,也可以使用 CSS 来完成。

      这是小提琴http://jsfiddle.net/xasy4/

      <div id="imgSmile">
           <div id="art">
              <a href="#"><img src="http://www.newmusicproducer.com/img/ic_play.png">
              </a>
           </div>
          </div>
      

      CSS

       #imgSmile
      {
          background: url('https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275') no-repeat center Transparent;
          width: 100px;
          height: 100px;
      }
      #imgSmile > #art
      {
          position: absolute; left: 20px; top: 40px; opacity: 0;
      }
      #imgSmile:hover > #art
      {
          position: absolute; left: 20px; top: 40px; opacity: 1;
      }
      

      【讨论】:

        【解决方案5】:

        您可以使用.on() 方法,
        而不是将两个元素都设置为选择器并使用事件(e) 检查器,例如:

        demo jsBin

        $(document).ready(function(){
        
            $('#art').hide();
        
        
            $('#imgSmile, #art').on('mouseenter mouseleave',function(e){
                if(e.type === 'mouseenter'){
                    $('#art').show();
                }else{
                    $('#art').hide();   
                }
            });
        
        });
        


        我最喜欢的是使用三元运算符,例如:
        $('#imgSmile, #art').on('mouseenter mouseleave',function(e){
            var et = e.type === 'mouseenter' ? $('#art').show() :  $('#art').hide();  
        });
        

        Demo with ternaly operator


        您可以在具体情况下使用.toggle() 方法:

        $('#imgSmile, #art').on('mouseenter mouseleave',function(e){
            $('#art').toggle(); 
        });
        

        demo with .toggle()

        【讨论】:

          【解决方案6】:

          http://jsfiddle.net/jqkBx/1/

          为什么不直接使用 CSS 来实现悬停效果?

          HTML

          <div id="wrapper">
            <img src="https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275" alt="smile" id="imgSmile" />
          
            <div id="art" style="position: absolute; left: 20px; top: 40px;">
               <a href="#"><img src="http://www.newmusicproducer.com/img/ic_play.png" /></a>
            </div>
            </div>
          

          CSS

          #art{
              display:none;
          }
          #wrapper{
              width:100px;
              height:100px;        
          }
          #wrapper:hover #art{
              display:block;        
          }
          

          【讨论】:

            【解决方案7】:
            $(document).ready(function() {
                $('#art').hide();             
                $('#imgSmile').mouseenter(function() {
                    $('#art').show();
                });
            $('#art').mouseenter(function() {
                    $('#art').show();
                });
                $('#imgSmile').mouseleave(function() {   
                    $('#art').hide();
                });
            });​
            

            只需添加#art鼠标进入事件

            【讨论】:

              猜你喜欢
              • 2011-12-23
              • 1970-01-01
              • 2012-08-22
              • 2015-04-25
              • 2012-05-13
              • 2010-10-29
              • 1970-01-01
              • 2021-02-21
              • 1970-01-01
              相关资源
              最近更新 更多