1 / 72

By: Bnar Faisal A. Daham

Web Programming Jquery Lectures. By: Bnar Faisal A. Daham. Introduction. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.

richardi
Download Presentation

By: Bnar Faisal A. Daham

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Programming Jquery Lectures By: Bnar Faisal A. Daham

  2. Introduction jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that requires many lines of JavaScript code to accomplish, and wraps it into methods that you can call with a single line of code. jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

  3. Introduction • The jQuery library contains the following features: • HTML/DOM manipulation • CSS manipulation • HTML event methods • Effects and animations • AJAX • Utilities

  4. Why jQuery? • There are a lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable. • Many of the biggest companies on the Web use jQuery, like: • Google • Microsoft • IBM • Netflix

  5. Downloading jQuery • There are two versions of jQuery available for downloading: • Production version - this is for your live website because it has been minified and compressed • Development version - this is for testing and development (uncompressed and readable code) • Both versions can be downloaded from jQuery.com.

  6. Downloading jQuery • The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section): • <head><script src="jquery-3.3.1.min.js"></script></head> • Note that: • The downloaded file in the same directory as the pages where you wish to use it. • we do not have type="text/javascript" inside the <script> tag because this is not required in HTML5. JavaScript is the default scripting language in HTML5 and in all modern browsers.

  7. Alternatives to Downloading If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network). Both Google and Microsoft host jQuery. To use jQuery from Google or Microsoft, use one of the following: Google CDN: <head><script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head>

  8. Alternatives to Downloading If you look at the Google URL in the previous slide we see the version of jQuery is specified in the URL (3.3.1). If you would like to use the latest version of jQuery, you can either remove a number from the end of the version string (for example 3.3), then Google will return the latest version available in the 3.3 series (3.3.0, 3.3.1, etc.), or you can take it up to the whole number (3), and Google will return the latest version available in the 3 series (from 3.1.0 to 3.9.9).

  9. Alternatives to Downloading Microsoft CDN: <head><script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script></head>

  10. jQuery Syntax • The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element(s). • Basic syntax is: $(selector).action() • A $ sign to define/access jQuery • A (selector) to "query (or find)" HTML elements • A jQueryaction() to be performed on the element(s) • Examples: • $(this).hide() - hides the current element. • $("p").hide() - hides all <p> elements. • $(".test").hide() - hides all elements with class="test". • $("#test").hide() - hides the element with id="test".

  11. The Document Ready Event All jQuery methods in our examples, are inside a document ready event: $(document).ready(function() {// jQuery methods go here...}); This is to prevent any jQuery code from running before the document is finished loading (is ready).

  12. The Document Ready Event • It is good practice to wait for the document to be fully loaded and ready, before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section. • Here are some examples of actions that can fail if methods are run before the document is fully loaded: • Trying to hide an element that is not created yet • Trying to get the size of an image that is not loaded yet

  13. The Document Ready Event The jQuery team has also created an even shorter method for the document ready event: $(function() {// jQuery methods go here...});

  14. jQuery Selectors jQuery selectors are one of the most important parts of the jQuery library. jQuery selectors allow you to select and manipulate HTML element(s). With jQuery selectors you can find elements based on their id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All type of selectors in jQuery, start with the dollar sign and parentheses: $().

  15. The element Selector The jQuery element selector selects elements based on their tag names. You can select all <p> elements on a page like this: $("p") Example When a user clicks on a button, all <p> elements will be hidden: $(document).ready(function(){  $("button").click(function(){    $("p").hide();  });});

  16. The#id Selector The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element. To find an element with a specific id, write a hash character, followed by the id of the element: $("#test") Example When a user clicks on a button, the element with id="test" will be hidden: $(document).ready(function(){  $("button").click(function(){    $("#test").hide();  });});

  17. The.class Selector The jQuery class selector finds elements with a specific class. To find elements with a specific class, write a period character, followed by the name of the class: $(".test") Example When a user clicks on a button, the elements with class="test" will be hidden: $(document).ready(function(){  $("button").click(function(){    $(".test").hide();  });});

  18. More Examples of jQuery Selectors

  19. More Examples of jQuery Selectors

  20. Functions In a Separate File If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQuery functions in a separate .jsfile. With the previous jQuery, the functions are added directly into the <head> section. However, sometimes it is preferable to place them in a separate file, and use the src attribute to refer to the .jsfile as shown bellow: <head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script src="my_jquery_functions.js"></script></head>

  21. What are Events? • If your website contains a lot of pages, and you want your jQuery All the different visitors actions that a web page can respond to are called events. • An event represents the precise moment when something happens. • Examples: • Moving a mouse over an element • Selecting a radio button • Clicking on an element • The term "fires/fired"is often used with events. Example: "The keypress event fired the moment you press a key".

  22. Some common DOM events: 

  23. jQuery Syntax For Event Methods In jQuery, most DOM events have an equivalent jQuery method. To assign a click event to all paragraphs on a page, you can do this: $("p").click(); The next step is to define what should happen when the event fires. You must pass a function to the event: $("p").click(function() {  // action goes here!!});

  24. Commonly Used jQuery Event Methods $(document).ready() The $(document).ready() method allows us to execute a function when the document is fully loaded. This event is already explained in the jQuery Syntax.

  25. Commonly Used jQuery Event Methods click() The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element. The following example says: When a click event fires on a <p> element; hide the current <p> element: $("p").click(function() {  $(this).hide();});

  26. Commonly Used jQuery Event Methods mouseenter() The mouseenter() method attaches an event handler function to an HTML element. The function is executed when the mouse pointer enters the HTML element: $("#p1").mouseenter(function() {  alert("You entered p1!");});

  27. Commonly Used jQuery Event Methods mouseleave() The mouseleave() method attaches an event handler function to an HTML element. The function is executed when the mouse pointer leaves the HTML element: $("#p1").mouseleave(function() {  alert("Bye! You now leave p1!");});

  28. Commonly Used jQuery Event Methods focus() The focus() method attaches an event handler function to an HTML form field. The function is executed when the form field gets focus: $("input").focus(function() {  $(this).css("background-color","#cccccc");});

  29. Commonly Used jQuery Event Methods blur() The blur() method attaches an event handler function to an HTML form field. The function is executed when the form field loses focus: $("input").blur(function() {  $(this).css("background-color","#ffffff");});

  30. jQuery Effects: jQuery hide() and show() • With jQuery, you can hide and show HTML elements with the hide() and show() methods. • You can toggle between the hide() and show() methods with the toggle() method. Shown elements are hidden and hidden elements are shown. • Example • $("#hide").click(function(){  $("p").hide();});$("#show").click(function(){  $("p").show();});

  31. jQuery Effects: jQuery hide() and show() • Syntax: • $(selector).hide(speed,callback);$(selector).show(speed,callback); • $(selector).toggle(speed,callback); • The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", or milliseconds. • The optional callback parameter is the name of a function to be executed after hide (or show or toggle) completes. • The following example demonstrates the speed parameter: • Example • $("button").click(function(){  $("p").hide(1000);});

  32. jQuery Effects: Fading • With jQuery you can fade an element in and out of visibility. • jQuery has the following fade methods:

  33. jQuery Effects: Fading • The first three methods in the previous slide use the same syntax: • $(selector).methodName (speed,callback); • while the fourth method uses an extra parameter which is opacity: $(selector).fadeTo(speed,opacity,callback); • The required opacity parameter in the fadeTo() method specifies fading to a given opacity (value between 0 and 1).

  34. jQuery Effects: Fading The following examples demonstrates the fadeIn(), fadeOut(), fadeToggle(), fadeTo()methods with different parameters: $("button").click(function() {  $("#div1").fadeIn();  $("#div2").fadeIn("slow");  $("#div3").fadeIn(3000);}); 2. $("button").click(function() {  $("#div1").fadeOut();  $("#div2").fadeOut("slow");  $("#div3").fadeOut(3000);});

  35. jQuery Effects: Fading 3. $("button").click(function() {  $("#div1").fadeToggle();  $("#div2").fadeToggle("slow");  $("#div3").fadeToggle(3000);}); 4. $("button").click(function() {  $("#div1").fadeTo("slow",0.15);  $("#div2").fadeTo("slow",0.4);  $("#div3").fadeTo("slow",0.7);});

  36. jQuery Effects: jQuery Sliding • With jQuery you can create a sliding effect on elements. • jQuery has the following slide methods: • slideDown() • slideUp() • slideToggle()

  37. jQuery Effects: jQuery Sliding jQueryslideDown() Method The jQueryslideDown() method is used to slide down an element. Syntax: $(selector).slideDown(speed,callback); Example $("#flip").click(function() {  $("#panel").slideDown();});

  38. jQuery Effects: jQuery Sliding jQueryslideUp() Method The jQueryslideUp() method is used to slide up an element. Syntax: $(selector).slideUp(speed,callback); Example $("#flip").click(function() {  $("#panel").slideUp();});

  39. jQuery Effects: jQuery Sliding • jQueryslideToggle() Method • The jQueryslideToggle() method toggles between the slideDown() and slideUp() methods. • If the elements have been slid down, slideToggle() will slide them up. • If the elements have been slid up, slideToggle() will slide them down.

  40. jQuery Effects: jQuery Sliding Syntax: $(selector).slideToggle(speed,callback); Example $("#flip").click(function() {  $("#panel").slideToggle();});

  41. jQuery Effects: jQuery Animations • The jQueryanimate() method is used to create custom animations. • Syntax: • $(selector).animate({params},speed,callback); • The required params parameter defines the CSS properties to be animated. • The following example demonstrates a simple use of the animate() method; it moves a <div> element to the left, until it has reached a left property of 250px: • Example • $("button").click(function() • {  $("div").animate({left:'250px'});}); 

  42. jQuery Effects: jQuery Animations jQuery animate() - Manipulate Multiple Properties Multiple properties can be animated at the same time: Example $("button").click(function(){  $("div").animate({    left:'250px',    opacity:'0.5',    height:'150px',    width:'150px'  });}); 

  43. jQuery Effects: jQuery Animations jQuery animate() - Using Relative Values It is also possible to define relative values (the value is then relative to the element's current value). This is done by putting += or -= in front of the value. Example $("button").click(function(){    $("div").animate({        left: '250px',        height: '+=150px',        width: '+=150px'    });}); 

  44. jQuery Effects: jQuery Animations jQuery animate() - Using Pre-defined Values You can even specify a property's animation value as "show", "hide", or "toggle". Example: $("button").click(function(){    $("div").animate({        height: 'toggle'    });}); 

  45. jQuery Effects: jQuery Animations • jQuery animate() - Uses Queue Functionality • By default, jQuery comes with queue functionality for animations. • This means that if you write multiple animate() calls after each other, jQuery creates an "internal" queue with these method calls. Then it runs the animate calls ONE by ONE. • So, if you want to perform different animations after each other, we take advantage of the queue functionality. • Example: • $("button").click(function(){var div = $("div");div.animate({left: '100px'}, "slow");div.animate({fontSize: '3em'}, "slow");}); 

  46. jQuery Effects: jQuery Stop() • The jQuerystop() method is used to stop an animation or effect before it is finished. • The stop() method works for all jQuery effect functions, including sliding and custom animations. • Syntax: • $(selector).stop(stopAll,goToEnd); • The following example demonstrates the stop() method, with no parameters: • $("#stop").click(function() • {  $("#panel").stop();});

  47. jQuery Effects: jQuery Stop() • The optional stopAll parameter specifies whether also the animation queue should be cleared or not. Default is false, which means that only the active animation will be stopped, allowing any queued animations to be performed afterwards. • The optional goToEnd parameter specifies whether or not to complete the current animation immediately. Default is false. • So, by default, the stop() method kills the current animation being performed on the selected element.

  48. jQuery Effects: jQuery Callback Functions • JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. • To prevent this, you can create a callback function. • A callback function is executed after the current effect is finished. • Typical syntax: $(selector).hide(speed,callback);

  49. jQuery Effects: jQuery Callback Functions The example below has a callback parameter that is a function that will be executed after the hide effect is completed: Example with Callback $("button").click(function() {  $("p").hide("slow",function() {    alert("The paragraph is now hidden");  });});

  50. jQuery Effects: jQuery Callback Functions The example below has no callback parameter, and the alert box will be displayed before the hide effect is completed: Example without Callback $("button").click(function() {  $("p").hide(1000);  alert("The paragraph is now hidden");});

More Related