Created
May 27, 2015 08:40
-
-
Save marlenesco/a613696dd6ab66c095d1 to your computer and use it in GitHub Desktop.
Class for circular menu using jquery and hammer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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