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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...