menucloud.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (function($) {
  2. $.fn.menucloud = function(settings)
  3. {
  4. settings = $.extend({}, $.fn.menucloud.defaults, settings);
  5. $('ul', this).css({
  6. 'position':'relative',
  7. 'width':'100%',
  8. 'height':'100%',
  9. 'padding':0
  10. })
  11. $('li', this).css({'top':"-100px", 'left':'-100px'}).each(function(i,e){
  12. var $li = $(e);
  13. $li.css({
  14. // 'border':'1px solid red',
  15. 'position':'absolute'
  16. });
  17. var $prt = $li.parent('ul.menu');
  18. // $.log('li', $li);
  19. // $.log('$this | width = '+$li.width()+' | height = '+$li.height());
  20. // $.log('$prt | width = '+$prt.width()+' | height = '+$prt.height());
  21. function _testPos(t){
  22. var isok = true;
  23. $li.siblings().each(function(j, sib){
  24. $sib = $(sib);
  25. // var pos = $sib.position();
  26. // $.log('pos',pos);
  27. var css_sib_top = $sib.css('top');
  28. // $.log('css_sib_top.match("/em/") = '+css_sib_top.search('em'));
  29. var sib_top = css_sib_top.search('em') >= 0 ? parseFloat(css_sib_top)/settings.q : parseFloat(css_sib_top);
  30. // $.log('sib_top = '+sib_top);
  31. // $.log('t = '+t+' | sib_top = '+sib_top);
  32. // $.log('Math.abs(top-sib_top) = '+Math.abs(top-sib_top));
  33. if(Math.abs(t-sib_top) < 35){
  34. isok = false;
  35. return false;
  36. };
  37. });
  38. return isok;
  39. };
  40. var isPosOK = false;
  41. var limit = 0;
  42. while(!isPosOK && limit < 10){
  43. var top = (Math.round(Math.random()*(($prt.height()-settings.margin.t-settings.margin.b)-$li.height()))+settings.margin.t);
  44. var left = (Math.round(Math.random()*(($prt.width()-settings.margin.l-settings.margin.r)-$li.width()))+settings.margin.l);
  45. isPosOK = _testPos(top);
  46. // $.log('isPosOK = '+isPosOK);
  47. limit = limit+1;
  48. }
  49. $li.css({
  50. 'top':(top*settings.q)+'em',
  51. 'left':(left*settings.q)+'em'
  52. });
  53. // $.log('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ');
  54. });
  55. return this;
  56. }
  57. $.fn.menucloud.defaults = {
  58. q:0.063,
  59. margin : {t:0,r:0,b:0,l:0}
  60. };
  61. })(jQuery);