Results 1 to 6 of 6

Thread: jQuery Gooey Menu: Change selected item dynamically

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default jQuery Gooey Menu: Change selected item dynamically

    1) Script Title: jQuery Gooey Menu

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex1/gooeymenu.htm

    3) Describe problem:
    I'm unable to dynamically change which menu item is selected. I added the following to the code:
    Code:
    $menulinks.click(function() {
        $selectedlink = this;
    })
    It does select the active item, but whenever I mouseover another item, that item which was mouseover'd becomes the new active item.

    I tried:
    Code:
    $menulinks.click(function() {
        $menulinks.removeClass('selected');
        $selecteditem = $(this).addClass('selected');
    })
    It doesn't do anything.

    Finally, in my last desperate attempt to make this work, I tried calling gooeymenu.setup again with the new selectitem defined.
    Code:
    $menulinks.click(function() {
                    var allItems = this.parentNode.getElementsByTagName('a');
                    
                    for(i=0; i<allItems.length; i++) {
                        if(allItems[i] == this) {
                            gooeymenu.setup({id:'gooeymenu1', selectitem: i});
                        }
                    }
    })
    It doesn't do anything. I would like to have it so that when an item is clicked, it stays active.

    Thanks for looking into this.
    -Josh

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Bump.

    Any ideas? :/

  3. #3
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Instead of:

    Code:
    $menulinks.click(function() {
        $selectedlink = this;
    })
    Try doing this instead:

    Code:
    $menulinks.click(function() {
        $selectedlink = $(this);
    })
    The $selectedlink variable expects a jQuery object.
    DD Admin

  4. The Following User Says Thank You to ddadmin For This Useful Post:

    JShor (07-21-2011)

  5. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Worked perfectly! Thanks a bunch!

  6. #5
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi! I'm a novice and I don't know how and where you put your code to get something working... I would like also to change dynamically the selected item in the menu staying in the same page.
    Could you please give me more explanations or a full example?
    Thank you very much for your support...

  7. #6
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Could you please give me more explanations or a full example?
    Assuming you're using notepad to edit the .js file, you can place it immediately after
    Code:
    $selectedlink=$target //set current mouseover element to selected element})
    so:
    Code:
    $selectedlink=$target //set current mouseover element to selected element})$menulinks.click(function(){$selectedlink = $(this);
    })
    I am no expert myself, so I do not know if this is the best placement, however it does work.

    The full modified .js file would then be as follows:
    Code:
    /* jQuery Gooey Menu
    * Created: April 7th, 2011 by DynamicDrive.com. This notice must stay intact for usage 
    * Author: Dynamic Drive at http://www.dynamicdrive.com/
    * Visit http://www.dynamicdrive.com/ for full source code
    */
    
    jQuery.noConflict()
    
    jQuery.extend(jQuery.easing, {easeOutBack:function(x, t, b, c, d, s){ //see http://gsgd.co.uk/sandbox/jquery/easing/
    		if (s == undefined) s = 1.70158;
    		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
    	}
    })
    
    
    var gooeymenu={
    	effectmarkup: '<li class="active"></li>',
    
    	setup:function(usersetting){
    		jQuery(function($){ //on document.ready
    			function snapback(dur){
    				if ($selectedlink.length>0)
    					$effectref.dequeue().animate({left:$selectedlink.position().left, width:$selectedlink.outerWidth()}, dur, setting.fx)
    			}
    			var setting=jQuery.extend({fx:'easeOutBack', fxtime:500, snapdelay:300}, usersetting)
    			var $menu=$('#'+setting.id).find('li:eq(0)').parents('ul:eq(0)') //select main menu UL
    			var $menulinks=$menu.find('li a')
    			var $effectref=$(gooeymenu.effectmarkup).css({top:$menulinks.eq(0).position().top, height:$menulinks.eq(0).outerHeight(), zIndex:-1}).appendTo($menu) //add trailing effect LI to the menu UL
    			$effectref.css({left:-$menu.offset().left-$effectref.outerWidth()-5}) //position effect LI behind the left edge of the window
    			if (typeof setting.selectitem!="undefined"){ //if setting.selectitem defined
    				var $selectedlink=$menulinks.removeClass('selected').eq(setting.selectitem).addClass('selected')
    			}
    			else{
    				var $selectedlink=$menulinks.filter('.selected:eq(0)') //find item with class="selected" manually defined
    			}
    			setting.defaultselectedBool=$selectedlink.length
    			$menulinks.mouseover(function(){
    				clearTimeout(setting.snapbacktimer)
    				var $target=$(this)
    //alert($target.position().left+" "+$target.get(0).offsetLeft)
    
    				$effectref.dequeue().animate({left:$target.position().left, width:$target.outerWidth()}, setting.fxtime, setting.fx)
    				if (setting.defaultselectedBool==0) //if there is no default selected menu item
    					$selectedlink=$target //set current mouseover element to selected element
    			})
    			$menulinks.click(function() {
    				$selectedlink = $(this);
    			})
    			if ($selectedlink.length>0){
    				snapback(0)
    				$menu.mouseleave(function(){
    					setting.snapbacktimer=setTimeout(function(){
    						snapback(setting.fxtime)
    					}, setting.snapdelay)
    				})
    			}
    				$(window).bind('resize', function(){
    					snapback(setting.fxtime)
    				})
    		})
    	}
    }
    Thanks to JShor and ddadmin for this fix, it's just what I needed.
    Last edited by Shreger; 12-16-2011 at 10:14 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •