VaioWay
VaioWay

Reputation: 402

jQuery position element from the mouse point (and more)

I'm writing a jQuery plugin for adding context menus to elements. Now, I need to fix the position of the context menu. If there is not enough space to show the entire context menu from the mouse bottom, then it should show from the mouse top. I tried some things but not works like I want.

Here is the code on jsFinddle: http://jsfiddle.net/evandroprogram/pRPQq/

Thank you.

Upvotes: 2

Views: 153

Answers (1)

joevallender
joevallender

Reputation: 4293

Here you go mate, I've forked and updated your fiddle here

http://jsfiddle.net/joevallender/j5Vy9/

The code change is this

var screenBottom = $(window).scrollTop() + $(window).height();
var menuHeight = _contextMenu.height();
var menuBottom = menuHeight + options.positionY;
if(menuBottom > screenBottom) {
    _contextMenu.css({
        top: "-=" + menuHeight
    })
}

Placed just after you set _contextMenu.css()

EDIT Just tested again and that isn't pixel perfect but it does work and should give you a decent clue if you want to tweak it :)

Upvotes: 1

Related Questions