Skip to content

Instantly share code, notes, and snippets.

@marlenesco
Created May 27, 2015 08:40
Show Gist options
  • Save marlenesco/a613696dd6ab66c095d1 to your computer and use it in GitHub Desktop.
Save marlenesco/a613696dd6ab66c095d1 to your computer and use it in GitHub Desktop.
Class for circular menu using jquery and hammer.js
var circular = function (selector) {
var circle = ((typeof selector == "string") ? $(selector) : selector);
if (typeof selector == "string") {
window.setTimeout(function () {
$(selector).parent().addClass('open');
}, 500);
}
var items = circle.children('li');
var angle = Math.PI*2/(items.length);
var radius = 0;
this.dispose = function() {
var self = this;
items.each(function(index) {
$(this).css({
'transform' : 'translate(' + (radius + radius*Math.cos(angle*index)) + 'px, ' + (radius + radius*Math.sin(angle*index)) + 'px) rotate(' + (angle*index) + 'rad)'
});
if ($(this).children('ul').length) {
$('<span>')
.hammer()
.bind('tap', function() {
var liParent = $(this).parent('li');
$(circle).removeClass('open');
if (liParent.hasClass('open')) {
liParent
.removeClass('open')
.closest('ul')
.parent()
.addClass('open');
} else {
$('.open').removeClass('open');
liParent.addClass('open');
}
})
.appendTo($(this));
self.createChild($(this).children('ul'));
}
});
};
this.createChild = function(child) {
var instance = new circular(child);
};
this.init = function(that) {
circle.height(circle.width());
radius = circle.width()/2;
that.dispose();
}(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment