【问题标题】:Jquery Animate menu downJquery Animate 菜单向下
【发布时间】:2013-02-27 15:35:53
【问题描述】:

我正在开发一个 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 似乎并不能解决问题。有人知道会怎样吗?

【问题讨论】:

  • 很高兴它有帮助 - 点击勾选将被感激地接受! =)
  • 完成,再次感谢 :)

标签: jquery css menu jquery-animate rollover


【解决方案1】:

这应该可以解决问题:

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;
    }

【讨论】:

【解决方案2】:
marginTop : "56px"

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

【讨论】:

  • 谢谢。我已经更新了 marginTop 的小提琴。它最初是 marginTop : '-' + spanHeight。为了便于更新,我只是将其更改为数字值。我必须在哪里更改 DOM 中 之前的悬停链接?
猜你喜欢
  • 1970-01-01
  • 2015-11-12
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多