【问题标题】:DIV menu ontop of <li> on hover [closed]悬停时 <li> 顶部的 DIV 菜单 [关闭]
【发布时间】:2010-05-30 22:56:10
【问题描述】:

大家好,这是我第一次在 stackflow 上发帖,但我之前使用过人们的答案。真是一个很棒的网站。

任何人解决我的问题。我有一些'li'标签。当我将鼠标悬停在这些“li”上时,我需要一个带有一些按钮等的 DIV 出现在“li”上。基本上它是一种菜单。 'li's 是不可预测的长度,通常从 1 行到 5 行。

dribbble.com 主页就是我试图实现的一个很好的例子。将鼠标悬停在图像上(尽管我使用的是 'li's),然后会出现一个漂亮的 lil 信息。

我完全没有使用 javascript 或 jqry 的经验,我只是一个有一些 CSS 的 PHP 人。我做后端工作。任何人,任何人都可以告诉我如何做到这一点并包括一个基本的例子吗?真的很感激。

帮助?

【问题讨论】:

    标签: javascript jquery css menu mouseover


    【解决方案1】:

    假设你有以下结构:

    <ul id="myMenu">
        <li><div class="inactive">Menu 1</div><div class="active">..some icons..</div></li>
        <li><div class="inactive">Menu 2</div><div class="active">..some icons..</div></li>
        <li><div class="inactive">Menu 3</div><div class="active">..some icons..</div></li>
    </ul>
    

    这是一个非常基本的菜单,我相信你会有更复杂的东西,但是通过使用包装的 div,这将使你的结构设置更加容易。

    你的 CSS 看起来像这样:

    /* Initially hide rollover content */
    #myMenu li div.active {
        display: none;
        /* Use the following if you want to overlay, otherwise delete */
        z-index: 2;
        position: absolute;
        top: 0px; left: 0px;
    }
    

    假设您使用的是 jQuery,则 javascript 看起来像这样:

    // DOM Ready
    jQuery(function($) {
        // Reference to menu
        $("#myMenu").delegate("li", "mouseenter mouseleave", function(evt) {
            var $this = $(this);
            switch(evt.type) {
                // When the client mouses over
                case "mouseover":
                    // To swap content use this
                    $this.find(".inactive").hide(); // Remove this line to overlay
                    $this.find(".active").show();
                break;
                // When the client mouses out
                case "mouseout":
                    // To swap content use this
                    $this.find(".inactive").show(); // Remove this line to overlay
                    $this.find(".active").hide();
                break;
            }
        });
    });
    

    编辑:在 css 和 javascript 中有错字,对不起兄弟

    【讨论】:

    • 好的,我在一个新页面上添加了脚本 css 和结构。我包含了 Jquery 文件。没有任何效果。就像我说的,我是新手。无论如何感谢您的帮助:)
    • 我已经纠正了一些小错误。祝你好运......你可以在这里看到它的实际效果:jsfiddle.net/fcyVv
    【解决方案2】:

    创建一个 .js 文件并将此代码粘贴进去。

    (function($) {
        $.fn.tooltip = function(options) {
    
            var 
              defaults = {
                  background: '#e3e3e3',
                  color: 'black',
                  rounded: false
              },
              settings = $.extend({}, defaults, options);
    
            this.each(function() {
                var $this = $(this);
                var title = this.title;
    
                if ($this.is('a') && $this.attr('title') != '') {
                    this.title = '';
                    $this.hover(function(e) {
                        // mouse over
                        $('<div id="tooltip" />')
                          .appendTo('body')
                          .text(title)
                          .hide()
                          .css({
                              backgroundColor: settings.background,
                              color: settings.color,
                              top: e.pageY + 10,
                              left: e.pageX + 20
                          })
                          .fadeIn(350);
    
                        if (settings.rounded) {
                            $('#tooltip').addClass('rounded');
                        }
                    }, function() {
                        // mouse out
                        $('#tooltip').remove();
                    });
                }
    
                $this.mousemove(function(e) {
                    $('#tooltip').css({
                        top: e.pageY + 10,
                        left: e.pageX + 20
                    });
                });
    
            });
            // returns the jQuery object to allow for chainability.
            return this;
        }
    })(jQuery);
    

    这应该出现在您的页面中;

    <script src="../../Scripts/jQuery.tooltip.js" type="text/javascript"></script>
    
    <style>
    #tooltip {
        background: url(../images/search.png) no-repeat 5px 50%;
        border: 1px solid #BFBFBF;
        float: left;
        font-size: 12px;
        max-width: 160px;
        padding: 1em 1em 1em 3em;
        position: absolute;
    }
    
    .rounded {
        -moz-border-radius: 3px;
        -webkit-border-radius: 3px;
    }
    
    </style>
    
    <script type="text/javascript">
        $('.tooltip').tooltip();    
    </script> 
    

    此链接将为您提供工具提示;

    <a href="#" class="tooltip rounded" title="The other day, I bla bla with WordPress.">over the years</a>
    

    现在,只要您有“工具提示”类,您就可以将&lt;a&gt; 替换为您想要的任何内容。然后你可以在里面放置按钮等。

    这不是完整的解决方案,但它应该让您非常接近。

    【讨论】:

    • 嘿,格里格,谢谢.. 我添加了代码并查看了它,更改了它等等。当我打开实际页面时,我得到的只是这些年来的文本。没有悬停,没有点击。我知道你说这不是完整的解决方案,我查看了它并尝试在应该添加的地方添加东西,但就像我说的那样,我对 javascript 没有经验。无论如何谢谢:)
    • 先看一下script标签。该路径需要指向您创建的文件,并且名称需要反映您为文件指定的名称。您还需要添加另一个脚本引用,我忘记了这一点,在我给您的加载文件 jquery-1.3.2.min.js 的脚本引用之上。我不知道你的路径所以适应反映这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多