【问题标题】:How do you overlay a div over an img, and show another div onmouseover?你如何在图像上覆盖一个 div,并在鼠标悬停时显示另一个 div?
【发布时间】:2009-07-19 09:01:52
【问题描述】:

我有一个列表缩略图,点击后会指向更大的对应项。我想在缩略图底部边缘的 div 中显示图像标题。此外,当鼠标移到缩略图上时,它会在整个内容上显示第二个 div(包含文本)。

最好的方法是什么?如果需要 js 库,则首选 jquery。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    如果您准备好使用jQuery,那么这里是Sam Dunn 的精彩帖子。

    【讨论】:

      【解决方案2】:
      <div>
          <img id="image-1" src="yourimage.png" title="My title" />
          <p id="image-1-title"></p>
      </div>
      
      
      $(document).ready(function(){
          $("img[id^='image-']").each(function(i){
              var id = $(this).attr("id");
              var title = $(this).attr("title");
              $("#"+id+"-title").html(title);
          });
      });
      

      这应该适用于图像标题:)

      【讨论】:

        【解决方案3】:

        我前段时间写了一个小插件,它可以让你做你需要做的事情,并带有悬停动画。它最初是为了添加悬停背景动画,但我想它应该能够做你需要的。

            /*
             * jQuery Animate Background (v0.2)
             * Copyright (c) 2009 Mario "Kuroir" Ricalde ( http://twitter.com/exolimpo )
             * Usage:
             *  $("h1").animateBg();
             *  Con Campos Opcionales:
             *  $("div").animateBg({time:130,add:"span"});
            */
            (function($){
                $.fn.animateBg = function(opciones){
                    $.fn.animateBg.defecto = {
                        time : "fast", add : "span"
                    };
                    var config = $.extend({}, $.fn.animateBg.defecto, opciones);
                    this.each(function(){
                        var c = config;
                        var title = $(this).attr("title");
                        $(this).append("<"+ c.add +">" + title + "</"+ c.add +">").hover(function(){
                            $(this).children(c.add).fadeIn(c.time);
                        },function(){
                            $(this).children(c.add).fadeOut(c.time);
                        });
                    });
                }
            })(jQuery);
        
        $(document).ready(function(){
            $("#thumb-list p img").animateBg();
        });
        
        <div id="thumb-list">
            <p><img src="lol.jpg" title="My title" /></p>
        </div>
        

        对不起,如果代码不起作用。我不在家,无法测试。

        【讨论】:

          猜你喜欢
          • 2012-04-22
          • 1970-01-01
          • 2013-06-15
          • 1970-01-01
          • 2015-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多