short intro to jquery and modernizr

22
Short into to JQuery and Modernizr Jussi Pohjolainen Tampere University of Applied Sciences

Upload: jussi-pohjolainen

Post on 19-May-2015

8.485 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Short intro to JQuery and Modernizr

Short  into  to  JQuery  and  Modernizr  Jussi  Pohjolainen  

Tampere  University  of  Applied  Sciences  

Page 2: Short intro to JQuery and Modernizr

JQuery?  

•  Mo?va?on  – Simple  things  may  require  lot  of  coding  – Common  browsers  are  different  and  implementa?on  varies  

•  Solu?on,  use  a  framework  –  jQuery  is  a  fast  and  concise  JavaScript  Library  that  simplifies  HTML  document  traversing,  event  handling,  anima?ng,  and  Ajax  interac?ons  for  rapid  web  development.    

 

Page 3: Short intro to JQuery and Modernizr

How?  

•  Download  JQuery  file  (hOp://jquery.com/)  – hOp://code.jquery.com/jquery-­‐1.7.1.min.js  

•  Make  your  xhtml  page  and  reference  to  the  file  in  script  block  

•  Make  your  code  and  use  JQuery  func?ons!  

Page 4: Short intro to JQuery and Modernizr

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

//<![CDATA[

// When document is ready to be manipulated

jQuery(document).ready( pageReadyToBeManipulated );

function pageReadyToBeManipulated() {

// If link is clicked

jQuery("a").click( linkClick );

}

function linkClick(event) {

alert("Thanks for visiting!");

// Prevent the default action

event.preventDefault();

}

//]]>

</script>

Page 5: Short intro to JQuery and Modernizr

Some  Basic  Syntax  

•  JQuery  can  be  used  in  two  ways:  –  JQuery()  – Or  – $()  

•  $  is  an  alias  to  JQuery()!  $  more  commonly  used  

Page 6: Short intro to JQuery and Modernizr

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

//<![CDATA[

// When document is ready to be manipulated

$(document).ready( pageReadyToBeManipulated );

function pageReadyToBeManipulated() {

// If link is clicked

$("a").click( linkClick );

}

function linkClick(event) {

alert("Thanks for visiting!");

// Prevent the default action

event.preventDefault();

}

//]]>

</script>

Page 7: Short intro to JQuery and Modernizr

// USING ANONYMOUS FUNCTIONS

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

//<![CDATA[

$(document).ready(function(){

$("a").click(function(event){

alert("Thanks for visiting!");

event.preventDefault();

});

});

//]]>

</script>

Page 8: Short intro to JQuery and Modernizr

// EVEN SHORTER SYNTAX, FORGET THE DOCUMENT PARAMETER

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

//<![CDATA[

$().ready(function(){

$("a").click(function(event){

alert("Thanks for visiting!");

event.preventDefault();

});

});

//]]>

</script>

Page 9: Short intro to JQuery and Modernizr

GeOers  in  the  Tradi?onal  Way  

•  getElementsById

•  getElementsByTagName

•  getAttribute

Page 10: Short intro to JQuery and Modernizr

JQuery  and  Selectors  

•  Select  all  h1  elements  – $(“h1”)  

•  Select  the  first  one  – $(“h1”)[0]  

•  Add  contents  – $(“h1”)[0].innerHTML  =  “hello!”;  

•  Lot  of  different  selectors  – hOp://api.jquery.com/category/selectors/  

Page 11: Short intro to JQuery and Modernizr

Crea?ng  Elements  in  Tradi?onal  Way  

•  createElement •  createTextNode •  setAttribute •  appendChild •  removeChild

Page 12: Short intro to JQuery and Modernizr

JQuery  Insert   $().ready(function(){

$("a").click(function(event){

// Insert the new element after element with id here

$("<p>New Element</p>").insertAfter("#here");

event.preventDefault();

});

});

Page 13: Short intro to JQuery and Modernizr

Manipula?on  Func?ons  

•  .addClass()  •  .afer()  •  .append()  •  .css()  •  …  •  See:  hOp://api.jquery.com/category/manipula?on/  

Page 14: Short intro to JQuery and Modernizr

Some  Effects:  Lot  of  Anim  Func?ons  

$('#clickme').click(function() {

$('#book').slideUp('slow', function() {

// Animation complete.

});

});

Page 15: Short intro to JQuery and Modernizr

MODERNIZR  

Page 16: Short intro to JQuery and Modernizr

Modernizr  

•  Small  JS  library  for  detec?ng  browser  features  •  Replaces  highly  unreliable  user  agent  sniffing  •  Feature  detec?on  for  each  browser  what  the  browser  can  or  cannot  do  

•  Tests  over  40  features  – Creates  Modernizr  object  containing  the  results  

Page 17: Short intro to JQuery and Modernizr

How  

•  Download  and  install  modernizr.js  •  Add  – <html  class=“no-­‐js”>  

•  Modernizr  replaces  this  – <html  class=“canvas  canvastext  geoloca?on..”>  

•  Classes  for  every  feature  it  detects!  

Page 18: Short intro to JQuery and Modernizr
Page 19: Short intro to JQuery and Modernizr

Adding  CSS  

Page 20: Short intro to JQuery and Modernizr

Result  

Page 21: Short intro to JQuery and Modernizr

HTML5  and  Video  

Page 22: Short intro to JQuery and Modernizr

Solu?on