Chapter 1 gave you the basic understanding about what is jquery and its syntax, lets check out some jquery functions which will produce effects on your screen.
Remember the example.js we have asked you to include in Chapter 1 for no reason? We will have to work on that in this chapter. Open this exmaple.js in your text editor and paste the following code in it and save it.
The first function you will notice is myCallback. This function will be called after every jquery effect. I am calling this function after every jquery effect just to let you know how call back function works and when it is getting called.
Next, you will see btnClick() function. This function is called on the onclick event of the submit button. This function gets the selected value from the effects dropdown and calls the appropriate jquery effects function.
Try playing with these functions and their parameters. Also notice the .delay() function used in myCallback().
Chapter 2 ends here. We will see some advance topics in coming chapters.
Remember the example.js we have asked you to include in Chapter 1 for no reason? We will have to work on that in this chapter. Open this exmaple.js in your text editor and paste the following code in it and save it.
function myCallback(msg){
$("#spCallback").html("I was called after " + msg + " effect!").hide("fast").delay(800).fadeIn(400);
}
function btnClick(){
var effect = $("#ddEffects").val();
switch(effect){
case "hide" :
jqHide();
break;
case "show" :
jqShow();
break;
case "toggle" :
jqToggle();
break;
case "slidedown" :
jqSlideDown();
break;
case "slideup" :
jqSlideUp();
break;
case "slidetoggle" :
jqSlideToggle();
break;
case "fadeout" :
jqFadeOut();
break;
case "fadein" :
jqFadeIn();
break;
case "fadeto" :
jqFadeTo();
break;
case "animate" :
jqAnimate();
break;
}
}
function jqHide(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").hide('normal',myCallback('Hide'));
});
}
function jqShow(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").show('normal',myCallback('Show'));
});
}
function jqToggle(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").toggle('normal',myCallback('Toggle'));
});
}
function jqSlideDown(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideDown('normal',myCallback('SlideDown'));
});
}
function jqSlideUp(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideUp('normal',myCallback('SlideUp'));
});
}
function jqSlideToggle(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideToggle('normal',myCallback('SlideToggle'));
});
}
function jqFadeOut(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").fadeOut('normal',myCallback('FadeOut'));
});
}
function jqFadeIn(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").fadeIn('normal',myCallback('FadeIn'));
});
}
function jqFadeTo(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Opacity, allows fading to a given opacity. Values can be between 0 to 1 ex - 0.25, 0.50, 0.75, etc
//@param 3 - Name of the callback function
$("div#myPara").fadeTo('normal',0.3,myCallback('FadeTo'));
});
}
function jqAnimate(){
$(document).ready(function(){
//@param 1 - styles (height, width, wordspacing, etc)
//@param 2 - speed (slow, normal, fast, milliseconds)
//@param 3 - easing (swing, linear)
//@param 4 - Name of the callback function
$("div#myPara").animate({wordSpacing:"15px"},"slow","linear",myCallback('Animate'));
});
}
$("#spCallback").html("I was called after " + msg + " effect!").hide("fast").delay(800).fadeIn(400);
}
function btnClick(){
var effect = $("#ddEffects").val();
switch(effect){
case "hide" :
jqHide();
break;
case "show" :
jqShow();
break;
case "toggle" :
jqToggle();
break;
case "slidedown" :
jqSlideDown();
break;
case "slideup" :
jqSlideUp();
break;
case "slidetoggle" :
jqSlideToggle();
break;
case "fadeout" :
jqFadeOut();
break;
case "fadein" :
jqFadeIn();
break;
case "fadeto" :
jqFadeTo();
break;
case "animate" :
jqAnimate();
break;
}
}
function jqHide(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").hide('normal',myCallback('Hide'));
});
}
function jqShow(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").show('normal',myCallback('Show'));
});
}
function jqToggle(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").toggle('normal',myCallback('Toggle'));
});
}
function jqSlideDown(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideDown('normal',myCallback('SlideDown'));
});
}
function jqSlideUp(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideUp('normal',myCallback('SlideUp'));
});
}
function jqSlideToggle(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").slideToggle('normal',myCallback('SlideToggle'));
});
}
function jqFadeOut(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").fadeOut('normal',myCallback('FadeOut'));
});
}
function jqFadeIn(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Name of the callback function
$("div#myPara").fadeIn('normal',myCallback('FadeIn'));
});
}
function jqFadeTo(){
$(document).ready(function(){
//@param 1 - slow, normal, fast, milliseconds
//@param 2 - Opacity, allows fading to a given opacity. Values can be between 0 to 1 ex - 0.25, 0.50, 0.75, etc
//@param 3 - Name of the callback function
$("div#myPara").fadeTo('normal',0.3,myCallback('FadeTo'));
});
}
function jqAnimate(){
$(document).ready(function(){
//@param 1 - styles (height, width, wordspacing, etc)
//@param 2 - speed (slow, normal, fast, milliseconds)
//@param 3 - easing (swing, linear)
//@param 4 - Name of the callback function
$("div#myPara").animate({wordSpacing:"15px"},"slow","linear",myCallback('Animate'));
});
}
The first function you will notice is myCallback. This function will be called after every jquery effect. I am calling this function after every jquery effect just to let you know how call back function works and when it is getting called.
Next, you will see btnClick() function. This function is called on the onclick event of the submit button. This function gets the selected value from the effects dropdown and calls the appropriate jquery effects function.
Try playing with these functions and their parameters. Also notice the .delay() function used in myCallback().
Chapter 2 ends here. We will see some advance topics in coming chapters.