110 likes | 198 Views
jQuery Part 1. jQuery. The purpose of jQuery is to make it much easier to use JavaScript It is a lightweight, “write less, do more” JavaScript library jQuery has the following features: HTML/DOM manipulation CSS manipulation HTML event methods Effects and animations AJAX Utilities
E N D
jQuery • The purpose of jQuery is to make it much easier to use JavaScript • It is a lightweight, “write less, do more” JavaScript library • jQuery has the following features: • HTML/DOM manipulation • CSS manipulation • HTML event methods • Effects and animations • AJAX • Utilities • jQuery is supported by all major browers
Adding jQuery • Download jQuery library from jquery.com • v1.10.2 or v2.0.3 • <script src=“jquery-1.10.2.min.js”></script> • Get jQuery from a Content Delivery Network (CDN) • Google and Microsoft host jQuery • <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”></script> • <script src=“http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js”></script>
Document Ready Event • Want to prevent any jQuery code from running before the document is finished loading • Hiding an element before it is created • Getting the size of an image before it is loaded • $(document).ready(function() { // jQuery and JavaScript}); • $(function() { // jQuery and JavaScript});
jQuery Syntax • With jQuery you select (query) HTML elements and perform actions on them • $(selector).action() • $(this).hide() • $(“p”).hide() • $(“.test”).hide() • $(“#test”).hide()
jQuery Event Methods • Common DOM events • $(“p”).click(function() { // jQuery, JavaScript code}
jQuery Effects – Hide and Show • Hide, Show, Toggle • $(selector).hide(speed,callback); • $(selector).show(speed,callback); • $(selector).toggle(speed,callback); • speed is “slow”, “fast” or milliseconds • callback is an optional function that executes when effect completes
jQuery Effects - Fading • fadeIn, fadeOut, fadeToggle, fadeTo • $(selector).fadeIn(speed,callback); • $(selector).fadeOut(speed,callback); • $(selector).fadeToggle(speed,callback); • $(selector).fadeTo(speed,opacity,callback);
jQuery Effects – Sliding • slideDown, slideUp, slideToggle • $(selector).slideDown(speed,callback); • $(selector).slideUp(speed,callback); • $(selector).slideToggle(speed,callback);
jQuery Effects – Animation • The animate() function lets you create custom animations • $(selector).animate({params},speed,callback); • params defines the CSS properties to be animated • speed is “slow”, “fast” or milliseconds • To manipulate the position of an element it must have a css position attribute • jQuery queues animate calls • stop() function stops animation • $(selector).stop(stopAll,goToEnd) • stopAll optional, defaults to false, empties queue • goToEnd optional, defaults to false, complete current animation var div=$(“#foo”); div.animate({left:’100px’},”slow”); div.animate({fontSize:’3em’},”slow”);