jQuery paul.tartarian

Introduction

Lors du développement d'un site web, l'utilisation du langage JavaScript devient pratique. Ce langage offre des options qui facilitent les choses à faire. Le problème est que pour utiliser ces options et aboutir à ce qui est demandé, on est obligé de passer par beaucoup de lignes de codes. C'est pourquoi on s'est décidé d'écrire une librairie contenant des fonctionnalités très utilisées dans JavaScript qui permet de ne pas écrire un grand morceau de code à chaque fois on veut avoir ces fonctionnalités. Cette librairie s'appelle jQuery.

Définition

jQuery est une librairie Javascript dont son but principale est d'écrire moins pour faire plus. Cette librairie permet de manipuler le DOM et CSS, de gérer les évènements et animations facilement, et enrichissent la page HTML avec des méthodes qui paraient difficiles à maintenir. jQuery est utilisé sur des sites fameux(google, facebook, amazon...) .

Utilisation

La librairie jQuery n'est qu'un fichier .js qui peut être téléchargé à partir du WEB (par exemple http://jquery.com/download/). Il existe 2 versions de ce fichier:

  • Version comprimée: Le code n'est pas lisible (tabulation n'est pas existante de façon propre), mais le fichier est de taille minime. C'est cette version qui est la plus utilisée.
  • Version non comprimée : Le code est bien lisible. Le fichier est de grande taille par rapport à la version comprimée.

Pour utiliser jQuery il faut lier la page HTML au fichier js(par exemple : < script type="text/javascript" src="js/jquery.min.js"></script> )

 

Code jQuery avec explication

001.//waits until the whole page is loaded to do an action
002.$(document).ready(function(){
003.$("#welcome").fadeIn('slow');
004.$('#test_find').find('strong').addClass('big');
005. 
006.//----------------------------------------------------------------------- 
007.//Showing the password
009.$('#showpassword_text').showPassword();
010.//----------------------------------------------------------------------- 
011.//showing text while hovering over it using a PLUGIN
012.$('#hover_text').hoverText(true);
013.//----------------------------------------------------------------------- 
014.//Drag an object using ui(user intferace)
015.$('#drag_me').draggable();
016.$('#drag_me_x').draggable({axis: 'x'});
017.$('#drag_me_y').draggable({axis: 'y'});
018.// we can use as containment : parent, window, [x1, y1, x2, y2]...
019.$('#drag_me_containment_document').draggable({containment: 'document'});
020.$('#drag_me_containment_surrounding').draggable({containment: 'parent'});
021.$('#drag_me_pointer_cursor').draggable({cursor: 'pointer'});
022.$('#drag_me_opacity_change').draggable({opacity: 0.60});
023.$('#drag_me_grid').draggable({grid: [40,40]});
024.$('#drag_me_revert').draggable({revert: true});
025.$('#drop_me').draggable({revert: true});
026.//----------------------------------------------------------------------- 
027.// tolerance: fit = when the whole "drop object" is in #drop_here than trigger droppable
028.//            intersect=default
029.//            pointer=when the mouse enters the #drop_here
030.//            touch= when the "drop object" touches the #drop_here
031.// accept: name the object (id or class) that can this drop box accept.
032.$('#drop_here').droppable(
033.{ hoverClass: 'border'
034., tolerance: 'fit'
035., accept: '#drop_me'
036., over: function(){
037.$('#drop_here').text('something hovered over here');
038.}
039.,out: function(){
040.$('#drop_here').text('');
041.}
042.,drop: function(){
043.$('#drop_here').text('Something dropped!');
044.}
045.}
046.);
047.//----------------------------------------------------------------------- 
048.// Allow to sort a list of items by dragging
049.$('#sortable_names').sortable();
050.//----------------------------------------------------------------------- 
051.// Change the size of an object
052.$('#box_resizable').resizable(
053./*containment: 'parent'
054., */animate: true
055., ghost: true
056., animateDuration: 'fast'
057.}
058.);
059.//----------------------------------------------------------------------- 
060.// The mouseover event changes the event from onclick to when the user hovers over the accordian widget
061.// the collapsible option allows a header in the div to be minimized otherwise the user will always be able to see a content of the div at all time
062.// The active option allows to define a particular header to be opened as the web-page is ready(like putting active:0). the active false means no header is opened.
063.$('#content_accordion').accordion({
064.fillspace: true
065., icons: {'header''ui-icon-plus''headerSelected''ui-icon-minus'
066.//, event: 'mouseover'
067., collapsible: true
068., active: false
069.});
070.//----------------------------------------------------------------------- 
071.// Displays a date  calendar
072.$('#date_picker').datepicker({
073.dateFormat: 'dd/mm/yy'
074., minDate: 0
075., maxDate: '+1m + 2d'
076., showButtonPanel: true
077., showAnim: 'bounce'
078.});
079.//----------------------------------------------------------------------- 
080.// Displays a dialog
081.$('#save_dialog').click(function(){
082.$('#dialog').attr('title','Saved').text('Settings were saved').dialog({
083.buttons: {'OK': function(){
084.$(this).dialog('close');
085.}
086.'NOT OK': function(){
087.alert('NOT OK');}
088.}
089., closeOnEscape: true         
090.});
091.});
092.//----------------------------------------------------------------------- 
093.});// End of document.ready
094. 
095.$('#upload').click(function(){
096.var val = 0;
097.var interval= setInterval(function(){
098.val=val+1;
099.// set a progress bar by percentage (just the like the upload download bar)
100.$('#progress_bar').progressbar({value: val});
101.$('#percent').text(val+'%');
102.if (val == 100) {
103.clearInterval(interval);
104.}
105.}, 50);
106.});
107.//----------------------------------------------------------------------- 
108.//Select all the paragraphs in HTML and put an event (click) on them
109.$("p").click(function(){
110.});
111.//-----------------------------------------------------------------------
112.// toggle is a function that hides and show objects
113.$("#show_hide").click(function(){
114.var current_value = $(this).attr("value");
115.$("#selectors_view").toggle("fast");
116.console.log($("#selectors_view"));
117. 
118.if (current_value=="Hide"){
119.$("#show_hide").attr("value","Show");
120.}else{
121.$("#show_hide").attr("value","Hide");
122.}
123.});
124.//----------------------------------------------------------------------- 
125.$('#change_text').click(function(){
126.$('#text1').css('background-color','black').css('color','red');
127.});
128.//-----------------------------------------------------------------------
129.function change_size(P_element, P_size){
130.var current_size = parseInt(P_element.css('font-size'));
131.if (P_size == 'smaller'){
132.P_element.css('font-size',current_size - 2 'px');
133.}else if(P_size == 'bigger') {
134.P_element.css('font-size',current_size + 2 +'px');
135.}
136.}
137.$('#smaller').click(function(){
138.change_size($('div#smaller_bigger > p'),'smaller')});
139. 
140.$('#bigger').click(function(){
141.change_size($('div#smaller_bigger p'),'bigger')});
142.//-----------------------------------------------------------------------
143.// hover is an event that is triggered when the user hovers over the object by mouse
144.$('#videos').hover(function(){
145.$('#menu_feedback').html('Free video tutorials');
146.});
147. 
148.$('#code').hover(function(){
149.$('#menu_feedback').html('Free high quality mode');
150.});
151. 
152.$('#forum').hover(function(){
153.$('#menu_feedback').html('A help forum');
154.});
155.//-----------------------------------------------------------------------
156.// focusin and focus out are events that trigger when the user enters/exits the textbox
157.$('#name').focusin(function(){
158.$('#span_name').html('Enter your name!');
159.});
160. 
161.$('#name').focusout(function(){
162.$('#span_name').html('');
163.});
164. 
165.$('#age').focusin(function(){
166.$('#span_age').html('Enter your current age!!!!!');
167.});
168. 
169.$('#age').focusout(function(){
170.$('#span_age').html('');
171.});
172. 
173.//-----------------------------------------------------------------------
174.var text_attr=$('#link').attr('target');
175.$('#attr').text(text_attr);

Références

http://thenewboston.org/list.php?cat=32

http://www.w3schools.com/jquery/default.asp

http://jquery.com/

Sous-pages (1) : Présentation
Comments