views:

97

answers:

1

So I have a hover function that is supposed to move an arrow under the corrent the link that someone hovers over. The problem is, the arrow never returns to the current link when the person stops hovering over the link. However, if I set the left properly with a constant value it works. Anyone have any ideas?

Here is the js:

$('#navbar li').hover(function(event) {
    var currentId = '#' + $('body').attr('id') + '_link';
    var xCordCurrent = $(currentId).offset().left - ($('#arrow').width() / 2);
    var xCordHover = $(event.target).offset().left + ($(event.target).width() / 2) - ($('#arrow').width() / 2);
        $('#arrow').animate(
        { left: xCordHover }, {
            duration: 'slow',
            easing: 'easeOutBack'
        })  
    }, function(event) {
        $('#arrow').animate(
        { left: xCordCurrent }, {
            duration: 'slow',
            easing: 'easeOutBack'
        })
    });

And here is the html:

<div id="arrow"></div>
<ul id="navbar">
    <li id="home_link"><a href="/">home</a></li>
    <li id="portfolio_link"><a href="portfolio.php">portfolio</a></li>
    <li id="resume_link"><a href="resume.php">resume</a></li>
    <li id="photos_link"><a href="photography.php">photos</a></li>
    <li id="blog_link"><a href="blog/">blog</a></li>
</ul>
A: 

The following complete example worked for me:

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(function() {
                var current = $('#' + $('body').attr('id') + '_link');
                var oldLeft = current.position().left + (current.width() / 2) - (arrow.width() / 2);
                var arrow = $('#arrow').css('left', oldLeft);
                $('#navbar li').hover(function() {
                    var h = $(this);
                    var newLeft = h.position().left + (h.width() / 2) - (arrow.width() / 2);
                    arrow.stop().animate({ left: newLeft }, {
                        duration: 'slow',
                        //easing: 'easeOutBack'
                    });
                }, function() {
                    arrow.stop().animate({ left: oldLeft }, {
                        duration: 'slow',
                        //easing: 'easeOutBack'
                    });
                });
            });
        </script>
        <style type="text/css">
            #navbar { margin: 0; padding: 0; overflow: auto; }
            #navbar li { list-style: none; margin: 0; padding: 0; float: left; }
            #navbar li a { margin: 1em; }
            #arrow { background-color: #f00; height: 3px; width: 10px; left: 0; position: absolute; }
        </style>
    </head>
    <body id="resume">
        <div id="arrow"></div>
        <ul id="navbar">
            <li id="home_link"><a href="/">home</a></li>
            <li id="portfolio_link"><a href="portfolio.php">portfolio</a></li>
            <li id="resume_link"><a href="resume.php">resume</a></li>
            <li id="photos_link"><a href="photography.php">photos</a></li>
            <li id="blog_link"><a href="blog/">blog</a></li>
        </ul>
    </body>
</html>

Notice how your easing method of easeOutBack is commented out. As per the jQuery API the only valid easing methods are linear and swing. I hope this at least gives you some insight to your problem.

Jake Wharton