Presentation is loading. Please wait.

Presentation is loading. Please wait.

JQuery CS 268. What is jQuery? From their web site:

Similar presentations


Presentation on theme: "JQuery CS 268. What is jQuery? From their web site:"— Presentation transcript:

1 jQuery CS 268

2 What is jQuery? From their web site: http://jquery.com/ http://jquery.com/

3 Is jQuery the only one? (No) From: http://javascriptlibraries.com/http://javascriptlibraries.com/

4 jQuery architecture Finds objects in the DOM/HTML using CSS style syntax. Core of jQuery is a group of functions designed to create/edit/delete the array of elements Also provides many methods for editing the basic components of elements like.css and.attr

5 What can jQuery do? http://docs.jquery.com/Main_Page Demos (from jQuery's site): http://jqueryui.com/demos/ http://jqueryui.com/demos/ Tutorials (from jQuery's site): http://docs.jquery.com/Tutorials http://docs.jquery.com/Tutorials

6 What is the jQuery dollar sign $ ? It is an identifier. You can think of it as a function name. You can use jQuery in place of the $ sign  but since most of us are lazy we use the $  For example these are equivalent lines of code: jQuery("#divTest1").text("Hello, world!"); $("#divTest1").text("Hello, world!");

7 What is the jQuery dollar sign $ ? The $ (or jQuery) function returns objects Therefore you can use. (dot) notation to access additional methods/functions applicable to the returned object. jQuery("#divTest1").text("Hello, world!"); $("#divTest1").text("Hello, world!");

8 jQuery allows easy access to Elements # used to reference elements with an id attribute. (dot or period) is used to reference elements with class attributes Omit # and. and you are searching for all elements of the same type, like div or table or img or... jQuery("#divTest1").text("Hello, world!"); $("#divTest1").text("Hello, world!");

9 jQuery ready event jQuery assumes you often want to execute code after a page loads. JavaScript can do this if called from a body tag's onload event  But this requires all images to finish loading jQuery's ready event is executed after all page elements (tags) are loaded - BUT before images are completely down loaded.  Allows quicker code execution

10 jQuery tutorials usually start here With the.ready event JQuery function documentReady() { $("#divTest1").text("Hello, world!"); } $(document).ready(documentReady); Calls documentReady after elements/tags are loaded

11 jQuery ready event shortcut The jQuery ready event is used often – so the jQuery developers decided to allow shorthand notation for it  Send an "anonymous" function as a parameter to it  An anonymous function does not have a name // use an anonymous function $(document).ready( function() { $("#divTest2").text("Hello, world!"); } );

12 jQuery ready event shortcut The code is often formatted like this (same code as in previous slide // use an anonymous function $(document).ready(function() { $("#divTest2").text("Hello, world!"); });

13 Even shorter notation for same thing Omit the (document).ready – default assumes this is there.  The jQuery crowd likes shortcuts! $(function() { $("#divTest2").text("Hello, world!"); });

14 Fade In

15 Fade In several boxes

16 Toggle fade in and out

17 Slide box up (till disappears)

18 Slide box up and down

19 Slide box down

20 Adding event handlers using jQuery Easier than slide 133's approaches shown in previous JS slideshow

21 Hiding text

22 Picture Galleries the jQuery Way! Using jQuery simplies the "custom" gallery you created in assignment1 Combines jQuery event handling with jQuery effects

23 The Gallery's HTML File Picture Gallery San Juan Mountains Colorado Close

24 JS File Using jQuery for Gallery $(function() { $("#popupphoto").click(function() { $("#popupphoto").fadeOut(1000); }); $(".image").mouseover(function() { $(this).css("border", "3px solid #9F0"); }); $(".image").mouseout(function() { $(this).css("border", "3px solid #000000"); }); // continued on next slide Assign event handlers here rather than in the HTML file

25 JS File Using jQuery for Gallery // continued from previous slide $(".image").click(function() { var path = $(this).attr("src"); path = getLargePhotoPath(path); $("#imgphoto").attr("src", path); $("#popupphoto").slideDown(1000); $("#filename").text(getFilename(path)); }); getLargePhotoPath and getFilename are short string handling functions to get the path to the large photos from the current thumbnail's path and to return the filename to display in the pop up photo this refers to clicked image object

26 Still not quite done... Previous gallery example issue:  After displaying large photo If close large photo first (has a fade out effect) clicking thumbnail allows the slide down effect (this is also good) Click another thumbnail without closing the larger photo  Next image is displayed without the slide down effect (not good – want to retain the fade out and slide in effects) Solution adds a bit more code...

27 Solution Modify the thumbnail image's click event $(".image").click(function() {  Check to see if the larger pop up photo is displayed: if($("#popupphoto").is(":visible")) { If it is, add code to fade out the large photo before sliding in the new photo  Not quite as easy as it might look .fadeOut and.slideDown allow following lines of code to run while the fade or slide is happening.  Need a way to ensure the fadeOut finishes before starting the slideDown!

28 Solution (continued).fadeOut and.slideDown have an optional second parameter  Second parameter is a function called after the fade or slide is finished.  Use an anonymous function to slide in the new photo after the fade is finished! $("#popupphoto").fadeOut(1000, function() { // code to slide in };); Using the name of a normal function as the second parameter seems like it should work but it doesn’t. The function gets called immediately without waiting for the effect to complete. Use an anonymous function.

29 var showFileName = true; // = true to show the filename in the popup photo, = false to hide it $(function() { // I'm not showing all the event handlers from slides 25 and 26 // I'm only showing how to modify the $(".image").click(function() { code // but the other code is still needed here. $(".image").click(function() { var path = getLargePhotoPath($(this).attr("src")); if($("#popupphoto").is(":visible") == true) { fadeThenSlideIn(path); } else { slideIn(path); } }); function fadeThenSlideIn(path) { $("#popupphoto").fadeOut(1000, function() { $("#imgphoto").attr("src", path); $("#popupphoto").slideDown(1000); if(showFileName == true) { $("#filename").text(getFilename(path)); } }); } function slideIn(path) { $("#imgphoto").attr("src", path); $("#popupphoto").slideDown(1000); if(showFileName == true) { $("#filename").text(getFilename(path)); }

30 AJAX AJAX – Asynchronous JavaScript and XML  XML – eXtensible Markup Language markup language for encoding data opening and closing tags used to identify data semantics like html but you get to define the tags and their meanings  AJAX allows updating portions of a web page with additional detail without reloading "all" of the web page.

31 http://www.w3schools.com/ajax/ajax_intro.asp AJAX

32 Initial web page has one or more sections within it with content derived from other web pages  HTTPRequest retrieves this data Makes Drill Down (Master Detail) pages easy to implement

33 jQuery AJAX Simplifies AJAX and creates cross browser compatible code

34 HTML fragment – not a complete page

35 Dynamically generated content

36 Another jQuery shortcut The first code example does the same thing the second one is doing (with less code) $("#orderInfo").html(ajax_load).load(loadUrl).css("display", "none").css("top", 40).fadeIn(500); $("#orderInfo").html(ajax_load).load(loadUrl); $("#orderInfo").html(ajax_load).css("display", "none"); $("#orderInfo").html(ajax_load).css("top", 40); $("#orderInfo").html(ajax_load).fadeIn(500); Same as:


Download ppt "JQuery CS 268. What is jQuery? From their web site:"

Similar presentations


Ads by Google