Search code examples
jqueryjquery-selectorsjquery-animatetraversaljquery-traversing

How to show sub-menu in a circular navigation menu on click of an element in jQuery


This question follows on from a previous question which can be found here: Need to fadeIn each circle 1 after the other with jQuery

Basically now when the 'Climate Change and energy' circle is clicked, I need to hide all the other circles apart from the clicked circle and show a sub set or other circles that follow the same path as the original circles. But the new circles start from the next point on from the clicked circle.

I have attached a picture of what needs to be shown when you click on the circle, which can be seen here: http://cl.ly/EO95

Here is the code that I attempted it with:

HTML:

<div class="circle_holder seventh">
                <div class="circle pie">
                    <a href="#"><span>climate change and energy</span></a>
                </div>
                <div class="inner_circles hidden">
                    <div class="circle_holder eighth">
                        <div class="circle pie">
                            <a href="#"><span>energy efficiency</span></a>
                        </div>
                    </div>
                    <div class="circle_holder first">
                        <div class="circle pie">
                            <a href="#"><span>renewable energy</span></a>
                        </div>
                    </div>
                    <div class="circle_holder second">
                        <div class="circle pie">
                            <a href="#"><span>carbon finance</span></a>
                        </div>
                    </div>
                    <div class="circle_holder third">
                        <div class="circle pie">
                            <a href="#"><span>climate adaptation</span></a>
                        </div>
                    </div
                    <div class="circle_holder fourth">
                        <div class="circle pie">
                            <a href="#"><span>ghg footprint assessment</span></a>
                        </div>
                    </div
                </div>
            </div>

JS:

if ($.browser.msie || $.browser.version < 9) {
    var circle = $('.circles .circle_holder > .circle');
    $(circle).animate({
        height: 168,
        width: 168,
        left: '0',
        top: '0'
    }, 1000);
    if (window.PIE) {
        $('.pie').each(function() {
            PIE.attach(this);
        });
    }
}

$('.circles .circle_holder > .circle').each(function(i){
    var time = 300 * (i + 1);
    setTimeout(function(){
        $('.circle').eq(i).addClass('open').animate({opacity: '1'}, i);
        $('.circle a span').animate({opacity: '1'}, 4000);
    }, time);
});

setTimeout(function() {
   $('.circle').addClass('circles_loaded');
}, 3700);

$('.circles > .circle_holder > .circle').click( function(){
    $('.inner_circles').removeClass('hidden', function() {
        console.log($('.inner_circles').parent().hide());
        $('.inner_circles').find().parent('.circle_holder').hide();

        $('.inner_circles .circle').each(function(i){
            var time = 300 * (i + 1);
            setTimeout(function(){
                $('.inner_circles .circle').eq(i).addClass('open').animate({opacity: '1'}, i);
                $('.inner_circles .circle a span').animate({opacity: '1'}, 4000);
            }, time);
        });
    });
});

Here is a link to a jsFiddle containing all the HTML/CSS & JS: http://jsfiddle.net/thomasbritton/wV867/

If anyone could help me out with this I would very much appreciate it as have been tearing my hair out over this for hours.

I am on skype if anyone would like to talk through it if it might help things.

Thanks


Solution

  • Tried to reach you on skype but you did not accepted invite.
    Anyway: click on climate change and energy.

    DEMO jsBin

    Sorry for ruining your CSS, complete JS, ... :)

    but I tought this could help. Take a look at the CSS : you don't have to setup manually the elements around the bg circle. Just try to follow the concept and redo the colors I left. Good luck!

    var $outerC = $('.circle').not('.inner_circles > div > .circle');
    
    var cX = 120; // center - distance from left
    var cY = 130; // center - distance from top
    var rad = 190; // SET .box DISTANCE (Radius from center);
    var boxN = $outerC.length;
    
    function circus(){
      $outerC.each(function(i,e){
        
          thisWc = $(e).outerWidth(true)/2;
          thisHc = $(e).outerHeight(true)/2;   
          var angle = (360/boxN)*i;
          var X = Math.round(cX+rad* Math.sin(angle*Math.PI/180))-2;
          var Y = Math.round(cY+rad*-Math.cos(angle*Math.PI/180))-2;  
          $(e).css({left:(X-thisWc), top:(Y-thisHc)});
        
      });
    }
    circus();
    
    function fader(){
      $outerC.each(function(i,e){
              var time = 300*i;
              setTimeout(function(){
                  $(e).addClass('open').animate({opacity: '1'}, time);
                  $('.circle a span').animate({opacity: '1'}, 4000);
              }, time);
      });
    }
    fader();
    
    $outerC.click(function(e){
        e.preventDefault();
      
        $outerC.fadeTo(300,0); // hide old ones
        $outerC = $(this).next('.hidden').find('.circle');   // set new
        boxN = $outerC.length;
        
        $('.hidden').show();
        $outerC.show();
        
        fader();
        circus(); 
      
    }); 
    

    You should find another, more programmable way to 'center' your texts inside your circles.