0

我正在开发一个 css jquery 菜单。我有一个像这样的代码http://jsfiddle.net/A6rUG/并试图动画下来而不是向上。代码是 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', 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 似乎并没有解决问题。有人知道会怎样吗?

4

2 回答 2

0
marginTop : "56px"

但是您必须更改其他代码以将悬停链接BEFORE实际<a>放在 DOM 中,而不是之后。

于 2013-02-27T15:39:07.887 回答
0

这应该可以解决问题:

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;
    }
于 2013-02-27T15:55:41.637 回答