SlideShare una empresa de Scribd logo
1 de 24
Beginning jQuery (part 2)
Review of the Code School course
“Try jQuery” (parts 4–5)
Review of Levels 4–5
http://try.jquery.com/
Simple Interactions
$('#pushie').click(function() {
// some code here
// more code
});
$('#apple').hover(function() {
// some code here
// more code
});
“Event handlers” are different
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
An event handler always includes .on()
In this example, .confirmation is a DIV or other container.
Inside the container are one or more buttons.
The handler “listens” for a click on any button inside any element
with the class .confirmation
When a click occurs, in this case jQuery travels “up” the DOM to find
.confirmation and then “down” the DOM to find .ticket — then it
reveals (shows) the .ticket element with a sliding motion.
$('.confirmation').on('click', 'button', function() {
// some code here
});
$('button').on('click', function() {
// some code here
});
These two are different:
The first one “listens” only to the .confirmation element(s).
The second one listens to every button element on the whole page.
(The second one is not efficient.)
Event Handlers (2)
Event Handlers: Mouse Events
What are we “listening” for?
• click
• dblclick
• focusin
• focusout
• mousedown
• mouseup
• mouseenter
• mouseleave
• mousemove
• mouseout
• mouseover
Mouse Events
• There is no hover that can be used with
.on() — there was, in an older version of
jQuery, but not anymore.
• So, we use "mouseenter mouseleave" to
achieve the same effect as hover.
• Note: Even though mouseover seems similar
to mouseenter, and mouseout seems similar
to mouseleave, do not use them.
$("#states").on("mouseenter mouseleave",
We will do an exercise (later) that uses this.
Instead of hover
https://github.com/macloo/jquery_exercises
More Event Handlers
Keyboard events
• keydown
• keypress
• keyup
Form events
• blur
• change
• focus
• select
• submit
What about touch and swipe events?
http://jquerymobile.com/
http://jquerymobile.com/
But back to our beginner lessons …
Fading and Sliding!
.fadeIn()
.fadeOut()
.fadeToggle()
.slideIn()
.slideOut()
.slideToggle()
We’ll do two exercises
with these, later.
hideslidefade.html
slideToggle.html
https://github.com/macloo/jquery_exercises
Styling
• Do not stick a lot of CSS styles into your jQuery functions:
$(this).css('background-color', '#252b30');
$(this).css('border-color', '1px solid #967');
• Instead, you should put CSS styles in your stylesheet
(where they belong), and use them like this:
$(this).addClass('highlight');
$(this).removeClass('highlight');
• You can also toggle styles:
$(this).toggleClass('highlight');
.animate()
$('#vacations').on('click', '.vacation', function() {
$(this).toggleClass('highlighted');
$(this).animate( {'top': '-10px'} );
});
Here, jQuery will make the element with the
class .vacation slide up (but not disappear) on
the page, by 10 pixels, when it has been clicked.
Note: The element needs to have position set to
relative in the CSS, or this won’t work.
$('#vacations').on('click', '.vacation', function() {
$(this).toggleClass('highlighted');
if($(this).hasClass('highlighted')) {
$(this).animate({'top': '-10px'});
} else {
$(this).animate({'top': '0px'});
}
});
Here we see an if statement inside the jQuery function. It plays off
the .toggleClass() method, which adds/removes ‘highlighted’
(from Code School lessons).
Reverse the animation
Control the speed
$(this).animate( {'top': '-10px'}, 400 );
$(this).animate( {'top': '-10px'}, 'fast' );
$(this).animate( {'top': '-10px'}, 200 );
$(this).animate( {'top': '-10px'}, 'slow' );
$(this).animate( {'top': '-10px'}, 600 );
Animate more than one thing
$(this).find('.per-night').animate(
{'opacity': '1', 'top':'-14px' } );
Basically, you can animate anything that you can
change or control with CSS.
Because—ahem!—CSS is doing the animation!
However!
• Direct CSS transitions are faster than jQuery:
http://stackoverflow.com/questions/10984771/whats-faster-css3-
transitions-or-jquery-animations
• You have more control with jQuery (but it will
be slower overall). Less code with jQuery.
• So you need to judge: CSS, or jQuery?
http://api.jquery.com/animate/ (Lots of examples here)
• CSS transitions – demos and more:
http://css3.bradshawenterprises.com/transitions/
jQuery vs. CSS
Maybe you didn’t know about CSS3 transitions …
#apple {
width: 100px;
height: 100px;
transition: width 2s, height 2s;
/* Firefox */
-moz-transition: width 2s, height 2s, -moz-transform 2s;
/* Safari and Chrome */
-webkit-transition: width 2s, height 2s, -webkit-
transform 2s;
/* Opera */
-o-transition: width 2s, height 2s, -o-transform 2s;
}
https://github.com/macloo/jquery_exercises
CSS3 transitions need more code
transition: width 2s, height 2s;
/* Firefox */
-moz-transition: width 2s, height 2s,
-moz-transform 2s;
/* Safari and Chrome */
-webkit-transition: width 2s, height 2s,
-webkit-transform 2s;
/* Opera */
-o-transition: width 2s, height 2s,
-o-transform 2s;
jQuery vs. CSS
Again, we’ll do two
exercises to learn
more about
animation in the
browser.
With jQuery:
animate.html
Without jQuery:
animation_with_css.html
https://github.com/macloo/jquery_exercises
Beginning jQuery (part 2)
Review of the Code School course
“Try jQuery” (parts 4–5)

Más contenido relacionado

Más de Mindy McAdams

Journalism blogs: An introduction
Journalism blogs: An introduction Journalism blogs: An introduction
Journalism blogs: An introduction Mindy McAdams
 
Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Mindy McAdams
 
Journalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersJournalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersMindy McAdams
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 CanvasMindy McAdams
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOMMindy McAdams
 
HTML and Responsive Design
HTML and Responsive Design HTML and Responsive Design
HTML and Responsive Design Mindy McAdams
 
Design Concepts and Web Design
Design Concepts and Web DesignDesign Concepts and Web Design
Design Concepts and Web DesignMindy McAdams
 
Learning Python - Week 4
Learning Python - Week 4 Learning Python - Week 4
Learning Python - Week 4 Mindy McAdams
 
Learning Python - Week 2
Learning Python - Week 2Learning Python - Week 2
Learning Python - Week 2Mindy McAdams
 
Learning Python - Week 1
Learning Python - Week 1Learning Python - Week 1
Learning Python - Week 1Mindy McAdams
 
Freedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisFreedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisMindy McAdams
 
Networked Information Economy / Benkler
Networked Information Economy / BenklerNetworked Information Economy / Benkler
Networked Information Economy / BenklerMindy McAdams
 
Convergence Culture / Jenkins
Convergence Culture / JenkinsConvergence Culture / Jenkins
Convergence Culture / JenkinsMindy McAdams
 
How to Share Your Digital Stories
How to Share Your Digital StoriesHow to Share Your Digital Stories
How to Share Your Digital StoriesMindy McAdams
 
Global Journalism Research
Global Journalism ResearchGlobal Journalism Research
Global Journalism ResearchMindy McAdams
 
Social media / professional use of Twitter
Social media / professional use of TwitterSocial media / professional use of Twitter
Social media / professional use of TwitterMindy McAdams
 
Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Mindy McAdams
 
Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Mindy McAdams
 

Más de Mindy McAdams (20)

Journalism blogs: An introduction
Journalism blogs: An introduction Journalism blogs: An introduction
Journalism blogs: An introduction
 
Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13Why Your Newsroom Should Be Using WordPress - ONA13
Why Your Newsroom Should Be Using WordPress - ONA13
 
Journalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not NewspapersJournalism's Future: Journalism, Not Newspapers
Journalism's Future: Journalism, Not Newspapers
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
HTML and Responsive Design
HTML and Responsive Design HTML and Responsive Design
HTML and Responsive Design
 
Design Concepts and Web Design
Design Concepts and Web DesignDesign Concepts and Web Design
Design Concepts and Web Design
 
Learning Python - Week 4
Learning Python - Week 4 Learning Python - Week 4
Learning Python - Week 4
 
Learning Python - Week 2
Learning Python - Week 2Learning Python - Week 2
Learning Python - Week 2
 
Learning Python - Week 1
Learning Python - Week 1Learning Python - Week 1
Learning Python - Week 1
 
Learning Python
Learning PythonLearning Python
Learning Python
 
Freedom of Speech - Louis Brandeis
Freedom of Speech - Louis BrandeisFreedom of Speech - Louis Brandeis
Freedom of Speech - Louis Brandeis
 
Networked Information Economy / Benkler
Networked Information Economy / BenklerNetworked Information Economy / Benkler
Networked Information Economy / Benkler
 
Convergence Culture / Jenkins
Convergence Culture / JenkinsConvergence Culture / Jenkins
Convergence Culture / Jenkins
 
How to Share Your Digital Stories
How to Share Your Digital StoriesHow to Share Your Digital Stories
How to Share Your Digital Stories
 
Global Journalism Research
Global Journalism ResearchGlobal Journalism Research
Global Journalism Research
 
Social media / professional use of Twitter
Social media / professional use of TwitterSocial media / professional use of Twitter
Social media / professional use of Twitter
 
Global Journalism
Global JournalismGlobal Journalism
Global Journalism
 
Social Media and Journalists: Part 4
Social Media and Journalists: Part 4Social Media and Journalists: Part 4
Social Media and Journalists: Part 4
 
Social Media and Journalists: Part 1
Social Media and Journalists: Part 1Social Media and Journalists: Part 1
Social Media and Journalists: Part 1
 

Último

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Beginning jQuery part 2

  • 1. Beginning jQuery (part 2) Review of the Code School course “Try jQuery” (parts 4–5)
  • 2. Review of Levels 4–5 http://try.jquery.com/
  • 3. Simple Interactions $('#pushie').click(function() { // some code here // more code }); $('#apple').hover(function() { // some code here // more code });
  • 4. “Event handlers” are different $('.confirmation').on('click', 'button', function() { $(this).closest('.confirmation').find('.ticket').slideDown(); }); An event handler always includes .on() In this example, .confirmation is a DIV or other container. Inside the container are one or more buttons. The handler “listens” for a click on any button inside any element with the class .confirmation When a click occurs, in this case jQuery travels “up” the DOM to find .confirmation and then “down” the DOM to find .ticket — then it reveals (shows) the .ticket element with a sliding motion.
  • 5. $('.confirmation').on('click', 'button', function() { // some code here }); $('button').on('click', function() { // some code here }); These two are different: The first one “listens” only to the .confirmation element(s). The second one listens to every button element on the whole page. (The second one is not efficient.) Event Handlers (2)
  • 6. Event Handlers: Mouse Events What are we “listening” for? • click • dblclick • focusin • focusout • mousedown • mouseup • mouseenter • mouseleave • mousemove • mouseout • mouseover
  • 7. Mouse Events • There is no hover that can be used with .on() — there was, in an older version of jQuery, but not anymore. • So, we use "mouseenter mouseleave" to achieve the same effect as hover. • Note: Even though mouseover seems similar to mouseenter, and mouseout seems similar to mouseleave, do not use them.
  • 8. $("#states").on("mouseenter mouseleave", We will do an exercise (later) that uses this. Instead of hover https://github.com/macloo/jquery_exercises
  • 9. More Event Handlers Keyboard events • keydown • keypress • keyup Form events • blur • change • focus • select • submit
  • 10. What about touch and swipe events?
  • 13. But back to our beginner lessons …
  • 14. Fading and Sliding! .fadeIn() .fadeOut() .fadeToggle() .slideIn() .slideOut() .slideToggle() We’ll do two exercises with these, later. hideslidefade.html slideToggle.html https://github.com/macloo/jquery_exercises
  • 15. Styling • Do not stick a lot of CSS styles into your jQuery functions: $(this).css('background-color', '#252b30'); $(this).css('border-color', '1px solid #967'); • Instead, you should put CSS styles in your stylesheet (where they belong), and use them like this: $(this).addClass('highlight'); $(this).removeClass('highlight'); • You can also toggle styles: $(this).toggleClass('highlight');
  • 16. .animate() $('#vacations').on('click', '.vacation', function() { $(this).toggleClass('highlighted'); $(this).animate( {'top': '-10px'} ); }); Here, jQuery will make the element with the class .vacation slide up (but not disappear) on the page, by 10 pixels, when it has been clicked. Note: The element needs to have position set to relative in the CSS, or this won’t work.
  • 17. $('#vacations').on('click', '.vacation', function() { $(this).toggleClass('highlighted'); if($(this).hasClass('highlighted')) { $(this).animate({'top': '-10px'}); } else { $(this).animate({'top': '0px'}); } }); Here we see an if statement inside the jQuery function. It plays off the .toggleClass() method, which adds/removes ‘highlighted’ (from Code School lessons). Reverse the animation
  • 18. Control the speed $(this).animate( {'top': '-10px'}, 400 ); $(this).animate( {'top': '-10px'}, 'fast' ); $(this).animate( {'top': '-10px'}, 200 ); $(this).animate( {'top': '-10px'}, 'slow' ); $(this).animate( {'top': '-10px'}, 600 );
  • 19. Animate more than one thing $(this).find('.per-night').animate( {'opacity': '1', 'top':'-14px' } ); Basically, you can animate anything that you can change or control with CSS. Because—ahem!—CSS is doing the animation!
  • 20. However! • Direct CSS transitions are faster than jQuery: http://stackoverflow.com/questions/10984771/whats-faster-css3- transitions-or-jquery-animations • You have more control with jQuery (but it will be slower overall). Less code with jQuery. • So you need to judge: CSS, or jQuery? http://api.jquery.com/animate/ (Lots of examples here) • CSS transitions – demos and more: http://css3.bradshawenterprises.com/transitions/
  • 21. jQuery vs. CSS Maybe you didn’t know about CSS3 transitions … #apple { width: 100px; height: 100px; transition: width 2s, height 2s; /* Firefox */ -moz-transition: width 2s, height 2s, -moz-transform 2s; /* Safari and Chrome */ -webkit-transition: width 2s, height 2s, -webkit- transform 2s; /* Opera */ -o-transition: width 2s, height 2s, -o-transform 2s; } https://github.com/macloo/jquery_exercises
  • 22. CSS3 transitions need more code transition: width 2s, height 2s; /* Firefox */ -moz-transition: width 2s, height 2s, -moz-transform 2s; /* Safari and Chrome */ -webkit-transition: width 2s, height 2s, -webkit-transform 2s; /* Opera */ -o-transition: width 2s, height 2s, -o-transform 2s;
  • 23. jQuery vs. CSS Again, we’ll do two exercises to learn more about animation in the browser. With jQuery: animate.html Without jQuery: animation_with_css.html https://github.com/macloo/jquery_exercises
  • 24. Beginning jQuery (part 2) Review of the Code School course “Try jQuery” (parts 4–5)

Notas del editor

  1. CONTACT ----- http://mindymcadams.com/ ----- Lecture by Mindy McAdams, University of Florida. Updated March 2014.
  2. SOURCE http://try.jquery.com/
  3. This is the simplest kind of interaction – you bind the function directly to one single element on the page. .hover()Description: Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements. http://api.jquery.com/hover/ .click()Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element. http://api.jquery.com/click/ Will not work if #target is added later.$("#target").click(function() {alert("Handler for .click() called.");});http://api.jquery.com/click/
  4. (this) refers to the button, in this case.
  5. Mouse events
  6. http://api.jquery.com/mouseleave/ mouseout and mouseleave do not work the same way. It seems mouseleave is preferable.mouseover and mouseenter have the same issues, and mouseenter is preferable.Support for using only mouseenter and mouseleave:http://www.bennadel.com/blog/1805-jQuery-Events-MouseOver-MouseOut-vs-MouseEnter-MouseLeave.htm In jQuery, hover actually combines both events into one (same source).So, use hover. BUT NOT WITH .on() —Deprecated as of jQuery 1.8: The name "hover" used as a shorthand for the string "mouseentermouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions. http://api.jquery.com/on/
  7. https://github.com/macloo/jquery_exercises
  8. Before, we saw a long list of MOUSE events. They only respond to mouse actions. These respond to keypresses and – the form events – to actions we take within an HTML form.
  9. Mobile has different EVENTS
  10. SOURCE http://jquerymobile.com/
  11. SOURCE http://jquerymobile.com/
  12. Return to what was covered in Code School
  13. https://github.com/macloo/jquery_exercises
  14. See “Using CSS animations”: https://developer.mozilla.org/en-US/docs/CSS/Tutorials/Using_CSS_animationsFor more complex animations, see JavaScript timer functions (can use with jQuery): https://developer.mozilla.org/en-US/docs/JavaScript/Timers jQuery documentation: http://api.jquery.com/animate/
  15. https://github.com/macloo/jquery_exercises Folder: css_animation
  16. https://github.com/macloo/jquery_exercises Folder: css_animation
  17. CONTACT ----- http://mindymcadams.com/ ----- Lecture by Mindy McAdams, University of Florida. Updated March 2014.