Search code examples
javascriptjquerysvgtimelinefullpage.js

make SVG as a timeline


i' m trying to make a circular timeline with SVG. i m using a fullpage js so i made a blue line working progressivly on scroll around the circle but now i want that my anchors dash array appears too but i can t find the solution, i want my anchors appear and stay when i scroll down ( like a timeline ) and come back to grey when scroll up

$(document).ready(function() {
			$('#fullpage').fullpage({
				anchors: ['firstPage', 'secondPage', '3rdPage', '4thPage', 'lastPage'],
				menu: '#menu',
				scrollingSpeed: 1000,		
                onLeave: function(index, nextIndex, direction){
                  $('#bluecircle').css('stroke-dashoffset', (1510/4)*(4-(nextIndex-1))); 
				   }
	
				   });
				});
#timeline{
	position:fixed;
	width:500px;
	height:500px;
	top:50%;
	left:50%;
	margin-top:-250px;
	margin-left:-250px;
	pointer-events: all;
	z-index:99;
}


#greycircle, #smallgreytop, #smallgreyleft, #smallgreybottom, #smallgreyright{
	stroke:rgba(204,204,204,1);
}
#bluecircle{
	stroke-dasharray:1510;
	stroke-dashoffset:1510;
	-webkit-transition:all 1s ease;
	transition:all 1s ease;
}

#smallblueleft, #smallbluebottom, #smallblueright{
	  stroke-dasharray:40;
	stroke-dashoffset:40;
		-webkit-transition:all 1s ease;
	transition:all 1s ease;
}

#smallblueleft:hover, #smallbluebottom:hover, #smallblueright:hover{
	stroke-dashoffset:0;
}
 /********** section ************/
 

.fp-section {
    position: relative;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
	text-align:center;
}

.fp-section.fp-table, .fp-slide.fp-table {
    display: table;
    table-layout:fixed;
    width: 100%;
}
.fp-tableCell {
    display: table-cell;
    vertical-align: middle;
    width: 100%;
    height: 100%;
}

.fp-scrollable {
    overflow: scroll;
}
.fp-notransition {
    -webkit-transition: none !important;
    transition: none !important;
}

h2{
	font-size: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/alvarotrigo/fullPage.js/master/jquery.fullPage.js"></script>


 <div id="timeline">
 
			<svg x="0px" y="0px" width="500px" height="500px" viewBox="0 0 500 500">
<circle id="greycircle" fill="none" stroke="#727272" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="248.065" r="239.024"/>

<circle id="smallgreytop" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="8.643" r="4.976"/>

<circle id="smallgreyleft" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="488.875" cy="247.667" r="4.976"/>

<circle id="smallgreybottom" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="486.691" r="4.976"/>

<circle id="smallgreyright" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="10.826" cy="247.667" r="4.976"/>

<circle id="bluecircle" fill="none" stroke="#2C75FF" stroke-width="3" stroke-miterlimit="10" cx="249.85" cy="248.065" r="239.024" transform="rotate(-90 249.85 248.065)"/>

<a data-offset="0" xlink:href="#firstPage"><circle id="smallbluetop" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="8.643" r="4.976"/></a>

<a data-offset="1132.5" xlink:href="#secondPage"><circle id="smallblueright" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="488.875" cy="247.667" r="4.976"/></a>

<a data-offset="755" xlink:href="#3rdPage"><circle id="smallbluebottom" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="486.691" r="4.976"/></a>

<a data-offset="377.5" xlink:href="#4thPage"><circle id="smallblueleft" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="10.826" cy="247.667" r="4.976"/></a>
      </svg>

	</div>
  
  <div id="fullpage">
	<div class="section " id="accueil">
        <h2>first</h2>
   
	</div>
	<div class="section" id="don">
	  <h2>second</h2>

	</div>
	<div class="section" id="tri">
			<h2>3rd</h2>
 
	</div>
	<div class="section" id="recycle">
			<h2>4th</h2>
			</div>
            
            <div class="section" id="vente">
			<h2>last</h2>
 
			</div>
</div>

my code :


Solution

  • Simply add an array which will contain each of your circles' id and change their css in the onleave event after looking for the object returned by the event :

    var smallCircles= ['top','right','bottom','left','top'];
    ...
    onLeave: function(...
    
        if(direction=='up'){
             $('#smallblue'+smallCircles[nextIndex]).css('stroke-dashoffset', '40');
             }
        $('#smallblue'+smallCircles[nextIndex-1]).css('stroke-dashoffset', '0');
    

    var smallCircles= ['top','right','bottom','left','top'];
    $(document).ready(function() {
    			$('#fullpage').fullpage({
    				anchors: ['firstPage', 'secondPage', '3rdPage', '4thPage', 'lastPage'],
    				menu: '#menu',
    				scrollingSpeed: 1000,		
                    onLeave: function(index, nextIndex, direction){
                      $('#bluecircle').css('stroke-dashoffset', (1510/4)*(4-(nextIndex-1))); 
                      if(direction=='up'){$('#smallblue'+smallCircles[nextIndex]).css('stroke-dashoffset', '40');}
                      $('#smallblue'+smallCircles[nextIndex-1]).css('stroke-dashoffset', '0');
    				   }
    	
    				   });
    				});
    #timeline{
    	position:fixed;
    	width:500px;
    	height:500px;
    	top:50%;
    	left:50%;
    	margin-top:-250px;
    	margin-left:-250px;
    	pointer-events: all;
    	z-index:99;
    }
    
    
    #greycircle, #smallgreytop, #smallgreyleft, #smallgreybottom, #smallgreyright{
    	stroke:rgba(204,204,204,1);
    }
    #bluecircle{
    	stroke-dasharray:1510;
    	stroke-dashoffset:1510;
    	-webkit-transition:all 1s ease;
    	transition:all 1s ease;
    }
    
    #smallblueleft, #smallbluebottom, #smallblueright, #smallbluetop{
    	  stroke-dasharray:40;
    	stroke-dashoffset:40;
    		-webkit-transition:all 1s ease;
    	transition:all 1s ease;
    }
    
    #smallblueleft:hover, #smallbluebottom:hover, smallbluetop:hover, #smallblueright:hover{
    	stroke-dashoffset:0;
    }
     /********** section ************/
     
    
    .fp-section {
        position: relative;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    	text-align:center;
    }
    
    .fp-section.fp-table, .fp-slide.fp-table {
        display: table;
        table-layout:fixed;
        width: 100%;
    }
    .fp-tableCell {
        display: table-cell;
        vertical-align: middle;
        width: 100%;
        height: 100%;
    }
    
    .fp-scrollable {
        overflow: scroll;
    }
    .fp-notransition {
        -webkit-transition: none !important;
        transition: none !important;
    }
    
    h2{
    	font-size: 2em;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://raw.githubusercontent.com/alvarotrigo/fullPage.js/master/jquery.fullPage.js"></script>
    
    
     <div id="timeline">
     
    			<svg x="0px" y="0px" width="500px" height="500px" viewBox="0 0 500 500">
    <circle id="greycircle" fill="none" stroke="#727272" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="248.065" r="239.024"/>
    
    <circle id="smallgreytop" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="8.643" r="4.976"/>
    
    <circle id="smallgreyleft" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="488.875" cy="247.667" r="4.976"/>
    
    <circle id="smallgreybottom" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="486.691" r="4.976"/>
    
    <circle id="smallgreyright" fill="#FFFFFF" stroke="#A6A6A6" stroke-width="2" stroke-miterlimit="10" cx="10.826" cy="247.667" r="4.976"/>
    
    <circle id="bluecircle" fill="none" stroke="#2C75FF" stroke-width="3" stroke-miterlimit="10" cx="249.85" cy="248.065" r="239.024" transform="rotate(-90 249.85 248.065)"/>
    
    <a data-offset="0" xlink:href="#firstPage"><circle id="smallbluetop" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="8.643" r="4.976"/></a>
    
    <a data-offset="1132.5" xlink:href="#secondPage"><circle id="smallblueright" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="488.875" cy="247.667" r="4.976"/></a>
    
    <a data-offset="755" xlink:href="#3rdPage"><circle id="smallbluebottom" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="249.85" cy="486.691" r="4.976"/></a>
    
    <a data-offset="377.5" xlink:href="#4thPage"><circle id="smallblueleft" fill="#FFFFFF" stroke="#2C75FF" stroke-width="2" stroke-miterlimit="10" cx="10.826" cy="247.667" r="4.976"/></a>
          </svg>
    
    	</div>
      
      <div id="fullpage">
    	<div class="section " id="accueil">
            <h2>first</h2>
       
    	</div>
    	<div class="section" id="don">
    	  <h2>second</h2>
    
    	</div>
    	<div class="section" id="tri">
    			<h2>3rd</h2>
     
    	</div>
    	<div class="section" id="recycle">
    			<h2>4th</h2>
    			</div>
                
                <div class="section" id="vente">
    			<h2>last</h2>
     
    			</div>
    </div>