1 / 13

CIS 461 Web Development I

CIS 461 Web Development I. JQuery Part II Jongwook Woo, PhD jwoo5@calstatela.edu California State University, Los Angeles Computer Information System Department. Content. jQuery Events jQuery Effects jQuery Callback. jQuery Events. Example next page: Focus

nibal
Download Presentation

CIS 461 Web Development I

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. CIS 461 Web Development I JQuery Part II Jongwook Woo, PhD jwoo5@calstatela.edu California State University, Los Angeles Computer Information System Department

  2. Content • jQuery Events • jQuery Effects • jQuery Callback

  3. jQuery Events • Example next page: • Focus • Change background color of an input field when it gets focus • Blur • Change background color of an input field when it loses focus (blur)

  4. Focus and blur events <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").focus(function(){ $("input").css("background-color","#FFFFCC"); }); $("input").blur(function(){ $("input").css("background-color","#D6D6FF"); }); }); </script> </head> <body> Enter your name: <input type="text" /> <p>Click in the input field to get focus, and outside the input field to lose focus (blur).</p> </body> </html>

  5. Effects • hide(): How to hide parts of text. • show(): How to show parts of text. • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_slow • slideToggle(): show and hide tags with slide panel effect. • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle • fadeTo(): Demonstrates a simple jQuery fadeTo() method. • animate(): Demonstrates a simple jQuery animate() method.

  6. Hide()/show() <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>If you click on the "Hide" button, I will disappear.</p> <button id="hide">Hide</button> <button id="show">Show</button> </body> </html>

  7. slideToggle() and FadeTo() • $(selector).slideToggle(speed,callback) • The speed parameter can take the following values: • "slow", "fast", "normal", or milliseconds • The callback parameter • is the name of a function to be executed after the function completes. • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle • $(selector).fadeTo(speed,opacity,callback) • The speed parameter can take the following values: • "slow", "fast", "normal", or milliseconds. • The callback parameter • is the name of a function to be executed after the function completes • The opacity parameter in the fadeTo() method • allows fading to a given opacity. • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_fadeto

  8. slideToggle() <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".flip").click(function(){ $(".panel").slideToggle("slow"); }); }); </script> <style type="text/css"> div.panel,p.flip /* look of panel and flip elements */ { margin:0px; padding:5px; text-align:center; background:#e5eecc; border:solid 1px #c3c3c3; }

  9. slideToggle() (cont’d) div.panel /* initial look of panel element: hidden as none display */ { height:120px; display:none; } </style> </head> <body> <div class="panel"> <p>Because time is valuable, we deliver quick and easy learning.</p> <p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p> </div> <p class="flip">Show/Hide Panel</p> </body> </html>

  10. jQuery Custom Animations • $(selector).animate({params},[duration],[easing],[callback]) • Example • animate({width:"70%",opacity:0.4,marginLeft:"0.6in",fontSize:"3em"}); • The key parameter is params. • It defines the CSS properties that will be animated. • Many properties can be animated at the same time: • The second parameter is duration. • It specifies the speed of the animation. • Possible values are • "fast", "slow", "normal", or milliseconds. • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_animation

  11. animate() <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("div").animate({height:300},"slow"); $("div").animate({width:300},"slow"); $("div").animate({height:100},"slow"); $("div").animate({width:100},"slow"); }); }); </script> </head> <body> <button>Start Animation</button> <br/> <br/> <div style="background:#98bf21;height:100px;width:100px;position:relative"> </div> </body> </html>

  12. jQuery CallBack Function • A callback function is executed after the current effect is finished. • Error in animations • JavaScript statements are executed line by line. • However, with effect, the next line of code can be run even though the animation is not finished. • This can create errors. • To prevent this, you can create a callback function. • The callback function will not be called until after the animation is finished. • Typical syntax: $(selector).hide(speed, callback) • $("p").hide(1000,function(){  alert("The paragraph is now hidden");}); • The callback parameter is a function to be executed after the hide effect is completed: • With Callback: • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_slow_right • Without Callback: • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_slow_wrong

  13. CallBack Function <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ /* without callback $("p").hide(1000); alert("The paragraph is now hidden"); */ $("p").hide(1000,function(){ // with callback alert("The paragraph is now hidden"); }); }); }); </script> </head> <body> <button>Hide</button> <p>This is a paragraph with little content.</p> </body> </html>

More Related