Skip to main content

jquery/js gliding/sliding navigation item effect



I'm trying to decipher effects like the one seen on http://www.htmldrive.net/items/demo/71/Dynamic-navigation-menu-with-scrolling-color-glide-followed-with-jQuery but I'm not very familiar with jquery/js (unless the code is all spelled out for me)





I haven't been able to find many other examples of this menu effect (I'm probably using the wrong keywords).





Can anyone help me figure out how these are generally created? I'd like to do so on my site (though with a thick underline instead of highlight). Thanks!





edit- I realize I can just use one of these plugins, but I'd really like to understand what's going on/do my own


Comments

  1. Here is a general overview of how you can create a similar effect:

    Create a menu, each menu item is in an individual div. The underline can be an image, absolutely positioned below the first menu item. Each item in the menu has an onmouseover function. That function will change the underline's position (left or right), one pixel at a time. To get it to have a cool slide effect that speeds up or down as it moves, I would recommend using a built-in plugin.

    so the menu items would look something like this:

    <div id="menuitem2" onmouseover="movesliderto('45');">


    The script would be something like this:

    var currentpos= '0'; //initial position

    function movesliderto(newposition){
    if (currentpos<newposition){ //moving the slider to the right
    for (var i=currentpos; i<newposition; i++) {
    document.getElementById('underline').style.left += 1;
    } else { //moving the slider to the left
    for (var i=newposition; i<currentpos; i++) {
    document.getElementById('underline').style.left -= 1;
    }
    }


    and of course, you would want each iteration of the foreach to take a couple of milliseconds, so you would want to put it in a timed function.

    ReplyDelete
  2. If you look at the demo's (taken out of the iframe), you'll see the demo's JavaScript here, accompanied by some CSS here. It looks simple enough.

    The "background" is a separate element in the HTML of the menu:

    <div class="webwidget_menu_glide_sprite"></div>


    The sprite and the menu's <ul> are both absolutely positioned. The <ul> is styled to be above the sprite, and the sprite is animated in response to hover events on the <li>'s in the menu.



    Update:

    To calculate and perform the animation, you have three basic steps:


    Listen to hover events on the <li>s;
    Find the width and position of the "glide" element, based on the item which triggered the hover effect;
    Animate to said width and position.


    In its most basic form, this looks somewhat like this:



    /* 1. Attach the event handler: */
    $('#menu li').on('hover', function() {
    /* 2. Find the position and width: */
    var newPosition = $(this).position();
    var newWidth = $(this).outerWidth(true);
    /* 3. Animate: */
    $('#menu .glide').stop().animate({
    'left': newPosition.left,
    'width': newWidth
    });
    });


    I've put a more complete example online here: http://jsbin.com/unuyov.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.