Jquery Animate 菜单向下

标签 jquery css menu jquery-animate rollover

我正在研究 CSS jquery 菜单。我有这样的代码 http://jsfiddle.net/A6rUG/并且正在尝试向下而不是向上设置动画。 代码是 var slideNav = (函数() {

    var spanHeight,
        nav = $('#nav'),
        lis = $('li', nav),
        anchors = $('a', lis).css('padding', 0);

    $.each(anchors, function() {
        var a = $(this),
            val = a.text();

        a.html('<span>' + val + '</span> <span>' + val + '</span>')
         .parent('li')
            .height(a.children('span:first').outerHeight())
         .end()
         .children('span:first')
            .css('marginTop', 0) // strange for IE
    });

    spanHeight = lis.eq(0).height();

    lis.hover(function() {
        $(this).find('span:first').animate({
            marginTop : -56
        }, { duration: 200, queue : false });
            }, function() {
        $(this).find('span:first').animate({
            marginTop : 0
        }, { duration: 200, queue: false });
    });

})();

更改 marginTop 或 marginBottom 似乎并不能解决问题。有人知道会怎样吗?

最佳答案

这应该可以解决问题:

var slideNav = (function() {

    var spanHeight,
        nav = $('#nav'),
        lis = $('li', nav),
        anchors = $('a', lis).css('padding', 0);

    $.each(anchors, function() {
        var a = $(this),
            val = a.text();

        a.html('<span>' + val + '</span> <span>' + val + '</span>')
         .parent('li')
            .height(a.children('span:first').outerHeight())
         .end()
         .children('span:first')
            .css('marginTop', -56) // strange for IE
    });

    spanHeight = lis.eq(0).height();

    lis.hover(function() {
        $(this).find('span:first').animate({
            marginTop : 0
        }, { duration: 200, queue : false });
            }, function() {
        $(this).find('span:first').animate({
            marginTop : -56
        }, { duration: 200, queue: false });
    });

})();

和 CSS:

body {
     text-align: center;
     background: #676767;
     font-family: helvetica;
    }

    ul, li {
     margin: 0; padding: 0;
     overflow: hidden;
    }

    ul li {
     float: left;
     list-style: none;
     margin-right: 1em; 
     position: relative; /* IE fix */
    }

    li a {
     color: white;
     text-decoration: none;
     float: left;
     font-size: 30px;
     background: black;
     padding: 10px;
    }

    li a:hover {
     background: #ff0;
    color:white;
    }

    /* if JS */

    li a span {
        display: block;
        background: white;
        color: maroon;
        padding: 10px;
        position: relative;
        font-weight: bold;
    }

    li a span:last-child {
        background: black;
        color: white;
    }

关于Jquery Animate 菜单向下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15116021/

相关文章:

使用 Grunt 的 CSS 版本号

javascript - 如何使用 MVC razor 语法将行动态添加到 html 表中

java - Joptionpane 未正确输出存储的值。

javascript - 在jquery中排序导航菜单

javascript - 添加带有 "save"或 "no"的警告框

jquery - 返回 JSON 数据以查看 ASP.NET MVC 页面

jQuery mobile - 带和不带视网膜显示的图像尺寸

javascript - 如果没有返回JSON数据则AJAX成功函数

css - 右对齐一个div

css - 不是全宽 css 水平菜单