Author Topic: JQuery Functions  (Read 2117 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
JQuery Functions
« on: April 04, 2012, 12:23:33 PM »
The web isn't being very helpful on this, so here goes:  I have the following which works fine:

Code: [Select]
       var $profile = $("#usermenu"), pos, width;

       var showProfileMenu = function(){
                //get the position of the placeholder element
                pos   = $(this).offset();
                width = $(this).width();
                height = $(this).height();
                $profile.css({ "left": (pos.left - 5) + "px", "top":(pos.top + height + 11) + "px" }).prependTo("#user").show();
        }
        var hideProfileMenu = function() {
            $profile.hide();
        }             

        $("#user").hover(showProfileMenu, hideProfileMenu);

But now I have a series of these so I want to make the function more generic.  So I tried the following, but it isn't working:
Code: [Select]
var $recordfilter = $("#subfmenu"), pos, width;
 function showFilterMenu(menu, id) {
            pos   = $("#"+id).offset();
            width = $("#"+id).width();
            height = $("#"+id).height();
            menu.css({ "left": (pos.left - 5) + "px", "top":(pos.top + height + 11) + "px" }).prependTo("#"+id).show();
        }
       
        function hideFilterMenu(menu) {
            menu.hide();
        }

$("#recordfilter").hover(showFilterMenu($recordfilter, "recordfilter"), hideFilterMenu($recordfilter));

It isn't even doing anything on the hover event and there aren't any JS errors, so I don't know what I'm doing wrong.  Help?

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: JQuery Functions
« Reply #1 on: April 04, 2012, 12:33:18 PM »
this might be grasping at straws, but I've had problems calling functions with parameters in bound jquery functions.

instead of:
$("#recordfilter").hover(showFilterMenu($recordfilter, "recordfilter"), hideFilterMenu($recordfilter));

try:
$("#recordfilter").hover( function(){ showFilterMenu($recordfilter, "recordfilter") }, function(){ hideFilterMenu($recordfilter) });

it might not work either but can't hurt to try
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: JQuery Functions
« Reply #2 on: April 04, 2012, 12:48:30 PM »
Found another site that said to do this right after I posted here (typical):

Code: [Select]
$("#recordfilter").mouseover(function() {
            showFilterMenu($recordfilter, "recordfilter");
        });
       
        $("#recordfilter").mouseout(function() {
            hideFilterMenu($recordfilter);
        });

Which is basically what you have.  It works but my offsets are fucked up for some reason.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: JQuery Functions
« Reply #3 on: April 04, 2012, 12:57:46 PM »
I'd make it a plugin and use something like

Code: [Select]
(function($)
{
$.fn.filtermenu = function (options)
{
var settings = $.extend({
menu: '#subfmenu',
}, options);

this.each(function()
{
var $menu = $(settings.menu);

$(this).hover(function()
{
var pos = $(this).offset();
var width = $(this).width();
var height = $(this).height();
$menu.offset({
left: pos.left - 5,
top: pos.top + 11,
});
},
function ()
{
$menu.hide();
}
);
});

return this;
}
})(jQuery);

Use it like
Code: [Select]
$("#recordfilter").filtermenu();
$("#recordfilter").filtermenu({menu: '#someotherid'});

For the offsets make sure the menu is absolutely positioned and attach it to the body.

Edit: Above is untested

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: JQuery Functions
« Reply #4 on: April 04, 2012, 02:15:06 PM »
Eh... that seems like a lot of work.  ;)  Plus it's working now ;)

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: JQuery Functions
« Reply #5 on: April 04, 2012, 02:54:15 PM »
Front loaded work and then dead simple usage.