• Home
  • Services
  • Geek Answers Handbook

    JQuery animation border

    I have this html code

    <nav id="mainNav">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>        
        </ul>
    </nav>
    

    He has a css style of this

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

    And the jQuery code attached to it

    $(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"
        },{ queue: false, duration: 500 }, function() {
        el.css('border-bottom-width', '0px');   
        });
        $(this).children('a').removeClass('aHover');
    });
    });
    

    Now what I want to do is either: fade in the border color to red, or cross it out, or as written in the code, expand the border to max. 5px on hover, then undo the border to 0px.

    Question: as you can see, I'm trying to change the class of the LI element at the end of the animation to make sure that the border goes to the maximum or minimum width, but this does not work, why?

    How would you fade out the color of the frame and out of it?

    +5
    javascript jquery html css
    Callum linington Apr 27 '12 at 17:54
    source share
    1 answer

    Try below

    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"
            }, {
                queue: false,
                duration: 500
            });
            $(this).children('a').removeClass('aHover');
        });
    });
    

    Modified

    • mouseoverevent mouseenteras it is more appropriate in your case
    • Changed mouseenter/mouseleavetohover
    • +=5px, -=5px 5px 0px.
    • .stop(true, true), , .
    +4
    Selvakumar Arumugam 27 . '12 18:02

    More articles:

    • MapTypeStyle in MapKit - ios
    • How to implement a secret menu - android
    • JavaScript: How to get the full function name from the inside? - javascript
    • IE10 - CSS animation not working - css3
    • Zend Framework: this line has been read only - zend-framework
    • Dojo Garbage Collection / Resource Release Methods? - dojo
    • Android: instumentation testing for application widgets - android
    • How to filter a ListView via EditText - android
    • Custom extensible list view with child search filter - android
    • How to calculate and calculate percentages in t-sql? - sql-server

    All Articles

    Geek Answers | 2019