微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – JQuery动画边框

我有这个HTML代码

它具有这种css风格

    #mainNav { display:block; padding:5px; }
    #mainNav ul { display:block; list-style-type:none; width:500px; border-collapse:collapse; }
    #mainNav li { display:inline-block; width:100px; height:66px; text-align:center; padding-top:10px; float:left; background-color:gray; cursor:pointer; border-bottom:0px solid red; }
    #mainNav a { color:white; text-decoration:none; text-transform:capitalize; }
    #mainNav a.aHover { color:red; }

附加到这是JQuery代码

$(document).ready(function() {
$('#mainNav li').mouseover(function() {
    var el = $(this);
    $(this).animate({
    "border-bottom-width":"+=5px"
    },{ queue: false,duration: 500 },function() {
    el.css('border-bottom-width','5px');   
    });
    $(this).children('a').addClass('aHover');
});
$('#mainNav li').mouseout(function() {
    var el = $(this);
    $(this).animate({
    "border-bottom-width":"-=5px"
    },'0px');   
    });
    $(this).children('a').removeClass('aHover');
});
});

现在我想要它做的是,将边框颜色淡入红色并淡出它,或者如代码所示,在悬停时将边框扩展到最大5px然后将边框放回到0px.

问题是,你可以看到我尝试在动画结束时更改LI元素的类,以确保边框达到其最大或最小宽度,但这不起作用,为什么?

你会如何淡化和淡出边框颜色?

最佳答案
试试下面,

DEMO

$(document).ready(function() {
    $('#mainNav li').hover(function() {
        var el = $(this);
        $(this).stop(true,true).animate({
            "border-bottom-width": "5px"
        },{
            queue: false,duration: 500
        });
        $(this).children('a').addClass('aHover');
    },function() {
        var el = $(this);
        $(this).stop(true,true).animate({
            "border-bottom-width": "0px"
        },duration: 500
        });
        $(this).children('a').removeClass('aHover');
    });
});

改变,

> mouSEOver事件到mouseenter,因为它更适合你的情况
>将mouseenter / mouseleave更改为悬停
>更改= 5px,– = 5px到5px和0px.
>添加.stop(true,true)以确保动画完成并清除队列.

原文地址:https://www.jb51.cc/html/425845.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐