Skip to content

Instantly share code, notes, and snippets.

@shameen
Created March 14, 2011 21:44
Show Gist options
  • Save shameen/869947 to your computer and use it in GitHub Desktop.
Save shameen/869947 to your computer and use it in GitHub Desktop.
I might turn this into a new repo, but for now it's here.
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">//<!--
/* Shameen's Context Menu (referred to as 'ccontext')
* First created 14th March 2011
*
* Usage:
* -The global array 'cc' stores the links (not yet linkable though)
* -Have a ul with the id 'context' such as:
* --<ul id="context" class="context" style="display:none;position:absolute;></ul>
* -Whatever you use to call ccontext() must have an id of
* the index it should have in the array. Eg:
* --<button onclick="ccontext(1)">This should show sub1.1,sub1.2,sub1.3</button>
*
* Oh, and for now, it's coded to appear 200px to the right of whatever called it,
* because I made it for propahjobstudios.co.uk
* Visit http://dl.dropbox.com/u/698380/PJS/v5/index.html to see this in action!
*/
//Context Menu Contents
var cc = [];
cc[1] = [["sub1.1"],["sub1.2"],["sub1.3"]];
cc[2] = [["sub2.1"],["sub2.2"],["sub2.3"]];
cc[3] = [["sub3.1"],["sub3.2"],["sub3.3"]];
cc[4] = [["sub4.1"],["sub4.2"],["sub4.3"]];
cc[5] = [["sub5.1"],["sub5.2"],["sub5.3"]];
var t;
function ccontextBye(instant) {
if (instant) {
//instantly hide
$('#context').stop().animate({top:0,opacity:0},'fast',function(){$('#context').hide();});
}
else {
//set a short delay before hiding
t = setTimeout(function() {
$('#context').stop().animate({top:0,opacity:0},'fast',function(){$('#context').hide();});
},250);
}
}
function ccontext(id) {
//set position
var o = $('#buttons #'+id).offset();
//hide context if already open there
if ($('#context').offset().top === o.top) {
ccontextBye(true);return;
}
$('#context').css('left',o.left+200+'px');
//fill up context
var s = '<ul>';
var i = 0;
for (i=0;i<cc[id].length;i=i+1) {
s += '<li><a href="#">' + cc[id][i] + '</a></li>';
}
s += '</ul>';
$('#context').html(s);
//show
$('#context').show();
$('#context').stop().animate({top:o.top,left:o.left+200,opacity:1},'fast');
}
$(document).ready(function() {
$('#buttons div').live('click', function() {var id=$(this).attr('id');ccontext(id);});
$('#context').live('mouseleave', function() {ccontextBye(false);});
$('.context li a').live('click', function() {/*todo*/ccontextBye(true);});
});
//--></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment