Team:Queens Canada/Javascript/Init

From 2012.igem.org

(Difference between revisions)
(Undo revision 160962 by P.tsang (talk))
 
(7 intermediate revisions not shown)
Line 1: Line 1:
-
(function($) {
+
jQuery(document).ready(function($) {
-
+
   
-
/*define easing you can omit this if  
+
    /*define easing you can omit this if
-
you don't want to use the easing plugin*/
+
    you don't want to use the easing plugin*/
 +
jQuery.easing.def = "easeInOutBack";   
-
 
+
    /* create the span tht will be animated across the menu*/
-
/* create the span tht will be animated across the menu*/
+
    /* declare our many variables for easy ref*/
-
/* declare our many variables for easy ref*/
+
        var $span = $('<span class="colourful"></span>');
-
var $span = $('<span class="colourful"></span>');
+
        $span.insertBefore($("#menu ul"));
-
$span.insertBefore($("#menu ul"));
+
       
-
+
        var $menu_link = $('#menu li a'),
-
var $menu_link = $('#menu li a'),
+
        $hovered =  $('#menu a.hovered'),/**/
-
$hovered =  $('#menu a.hovered'),/**/
+
        $hovered_pos = $hovered.position('#menu');/*position of hovered menu item*/
-
$hovered_pos = $hovered.position('#menu');/*position of hovered menu item*/
+
       
-
+
        /* declare our many colors that can confuse a chameleon*/
-
/* declare our many colors that can confuse a chameleon*/
+
        var $colour_arr = ['#fbb92e','#f8d52f','#b4f62f','#54f7a8','#3ff7f3','#3a97fa','#6835f9','#d544f6','#f650ab'];
-
var $colour_arr = ['#fbb92e','#f8d52f','#b4f62f','#54f7a8','#3ff7f3','#3a97fa','#6835f9','#d544f6','#f650ab'];
+
       
-
+
        /*iterate through all menu links and apply colors to border top */
-
/*iterate through all menu links and apply colors to border top */
+
        $menu_link.each(function(index){
-
$menu_link.each(function(index){
+
           
-
+
                    $menu_link.eq(index).css('border-color',$colour_arr[index]);
-
$menu_link.eq(index).css('border-color',$colour_arr[index]);
+
               
-
+
            });  
-
});
+
           
-
+
    /* all the magic happens here*/
-
/* all the magic happens here*/
+
    function init () {
-
function init () {
+
       
-
+
        if($hovered_pos) {
-
if($hovered_pos) {
+
                $span.css('left',$hovered_pos);
-
$span.css('left',$hovered_pos);
+
                var index = 0;
-
var index = 0;
+
                /* search for the selected menu item*/
-
/* search for the selected menu item*/
+
                for(k=0; k<$menu_link.length; k++) {
-
for(i=0; i<$menu_link.length; i++) {
+
                    if($($menu_link[k]).hasClass('hovered')) {
-
if($($menu_link[i]).hasClass('hovered')) {
+
                        index = k;
-
index = i;
+
                    }
-
}
+
                }
-
}
+
                $span.css('background',$colour_arr[index]);
-
$span.css('background',$colour_arr[index]);
+
               
-
+
        }
-
}
+
       
-
+
        /*mouseenter funtion*/
-
/*mouseenter funtion*/
+
        $menu_link.each(
-
$menu_link.each(
+
            function( intIndex ){
-
function( intIndex ){
+
                $(this).on (
-
$(this).on (
+
                    "mouseenter",
-
"mouseenter",
+
                        function(event){
-
function(event){
+
                           
-
+
                            var x = $(this).position('#menu');
-
var x = $(this).position('#menu');
+
                            x = x.left;
-
x = x.left;
+
                           
-
+
                                $span.css('background',$colour_arr[intIndex]);
-
$span.css('background',$colour_arr[intIndex]);
+
                           
-
+
                            $span.stop();
-
$span.stop();
+
                            $span.animate({
-
$span.animate({
+
                               
-
+
                                left: x
-
left: x
+
                              },600);
-
  },600);
+
                        }
-
}
+
                    );
-
);
+
       
-
+
                }
-
}
+
        );
-
);
+
       
-
+
        /* mouseout function*/
-
/* mouseout function*/
+
        $menu_link.each(
-
$menu_link.each(
+
            function( intIndex ){
-
function( intIndex ){
+
                $(this).on (
-
$(this).on (
+
                    "mouseleave",
-
"mouseleave",
+
                        function(event){
-
function(event){
+
                            $span.stop();
-
$span.stop();
+
                        var x = -100;
-
var x = -100;
+
                        if($hovered_pos) {
-
if($hovered_pos) {
+
                            x = $hovered_pos;
-
x = $hovered_pos;
+
                            var index = 0;
-
var index = 0;
+
                            for(l=0; l<$menu_link.length; l++) {
-
for(i=0; i<$menu_link.length; i++) {
+
                                if($($menu_link[l]).hasClass('hovered')) {
-
if($($menu_link[i]).hasClass('hovered')) {
+
                                    index = l;
-
index = i;
+
                                }
-
}
+
                            }
-
}
+
                                $span.css('background',colour_arr[index]);
-
$span.css('background',colour_arr[index]);
+
                           
-
+
                        }
-
}  
+
                       
-
+
                          $span.animate({
-
  $span.animate({
+
                               
-
+
                                left: x
-
left: x
+
                              },600);
-
  },600);
+
                        }
-
}
+
                    );
-
);
+
                }
-
}
+
        );
-
);
+
    }
-
}
+
    /* call init our function*/
-
/* call init our function*/
+
    init();
-
init();
+
});
});

Latest revision as of 03:50, 25 September 2012

jQuery(document).ready(function($) {

   /*define easing you can omit this if
   you don't want to use the easing plugin*/

jQuery.easing.def = "easeInOutBack";

   /* create the span tht will be animated across the menu*/
   /* declare our many variables for easy ref*/
       var $span = $('');
       $span.insertBefore($("#menu ul"));
       
       var $menu_link = $('#menu li a'),
       $hovered =  $('#menu a.hovered'),/**/
       $hovered_pos = $hovered.position('#menu');/*position of hovered menu item*/
       
       /* declare our many colors that can confuse a chameleon*/
       var $colour_arr = ['#fbb92e','#f8d52f','#b4f62f','#54f7a8','#3ff7f3','#3a97fa','#6835f9','#d544f6','#f650ab'];
       
       /*iterate through all menu links and apply colors to border top */
       $menu_link.each(function(index){
           
                   $menu_link.eq(index).css('border-color',$colour_arr[index]);
               
           });    
           
   /* all the magic happens here*/
   function init () {
       
       if($hovered_pos) {
               $span.css('left',$hovered_pos);
               var index = 0;
               /* search for the selected menu item*/
               for(k=0; k<$menu_link.length; k++) {
                   if($($menu_link[k]).hasClass('hovered')) {
                       index = k;
                   }
               }
               $span.css('background',$colour_arr[index]);
               
       }
       
       /*mouseenter funtion*/
       $menu_link.each(
           function( intIndex ){
               $(this).on (
                   "mouseenter",
                       function(event){
                           
                           var x = $(this).position('#menu');
                           x = x.left;
                           
                               $span.css('background',$colour_arr[intIndex]);
                           
                           $span.stop();
                           $span.animate({
                               
                               left: x
                             },600);
                       }
                   );
        
               }
        );
        
       /* mouseout function*/
       $menu_link.each(
           function( intIndex ){
               $(this).on (
                   "mouseleave",
                       function(event){
                           $span.stop();
                       var x = -100;
                       if($hovered_pos) {
                           x = $hovered_pos;
                           var index = 0;
                           for(l=0; l<$menu_link.length; l++) {
                               if($($menu_link[l]).hasClass('hovered')) {
                                   index = l;
                               }
                           }
                               $span.css('background',colour_arr[index]);
                           
                       }
                       
                         $span.animate({
                               
                               left: x
                             },600);
                       }
                   );
               }
        );
   }
   /* call init our function*/
   init();

});