jquery news packages

140
jQuery Developing online news packages

Upload: uc-berkeley-graduate-school-of-journalism

Post on 17-May-2015

677 views

Category:

Education


1 download

DESCRIPTION

jQuery tutorial for class

TRANSCRIPT

Page 1: Jquery News Packages

jQueryDeveloping online news packages

Page 2: Jquery News Packages

What is jQuery?

Page 3: Jquery News Packages

Web pages are made up of three elements

Page 4: Jquery News Packages

Web pages are made up of three elements

• HTML - content

Page 5: Jquery News Packages

Web pages are made up of three elements

• HTML - content• CSS - design

Page 6: Jquery News Packages

Web pages are made up of three elements

• HTML - content• CSS - design• JavaScript - interactivity

Page 7: Jquery News Packages

The problem with JavaScript

• Simple tasks like manipulating a webpage take lots of code.

• Different browsers have slightly different methods of doing things, requiring coders to create fallbacks.

• Writing code, like an animation, could be inefficient or not the the best method of doing something.

Page 8: Jquery News Packages

The problem with JavaScript

• Simple tasks like manipulating a webpage take lots of code.

• Different browsers have slightly different methods of doing things, requiring coders to create fallbacks.

• Writing code, like an animation, could be inefficient or not the the best method of doing something.

jQuery to the rescue!

Page 9: Jquery News Packages

jQuery is a library written in JavaScript

• jQuery is cross-browser compatible.

• Makes performing tasks like updating the page, relatively simple.

• Less code required to introduce interactivity.

Page 10: Jquery News Packages

JavaScript jQuery

Page 11: Jquery News Packages

JavaScript jQuery

function  fadeOut(elm,  interval)  {      for  (var  i=10;  i>0;  i-­‐-­‐)  {          var  opacity  =  i/10;          setTimeout(  function(opacity)  {              elm.setStyle("-­‐moz-­‐opacity",opacity);              elm.setStyle("opacity",opacity);              elm.setStyle("filter","alpha(opacity="  +  (opacity*100).toString()  );              //set  alpha  values  for  various  browsers          },  interval;      }  }  !fadeOut(getElementById('box'),  100);

Page 12: Jquery News Packages

JavaScript jQuery

function  fadeOut(elm,  interval)  {      for  (var  i=10;  i>0;  i-­‐-­‐)  {          var  opacity  =  i/10;          setTimeout(  function(opacity)  {              elm.setStyle("-­‐moz-­‐opacity",opacity);              elm.setStyle("opacity",opacity);              elm.setStyle("filter","alpha(opacity="  +  (opacity*100).toString()  );              //set  alpha  values  for  various  browsers          },  interval;      }  }  !fadeOut(getElementById('box'),  100);

$('#box').fadeOut();

Page 13: Jquery News Packages

Where do I get jQuery?

jquery.com

OR Link directly to the "Content Delivery Network"

(CDN)

http://code.jquery.com/jquery.min.js

Page 14: Jquery News Packages

Where do I put jQuery?

<head>  !      <title>Some  page  title</title>        <style>              #container{                      width:600px;              }        </style>        <script  src="jquery.min.js"></script>        <script>              //your  jquery  commands  go  here        </script>  !</head>

Page 15: Jquery News Packages

Where do I put jQuery?

<head>  !      <title>Some  page  title</title>        <style>              #container{                      width:600px;              }        </style>        <script  src="jquery.min.js"></script>        <script>              //your  jquery  commands  go  here        </script>  !</head>

jQuery Library

Page 16: Jquery News Packages

Where do I put jQuery?

<head>  !      <title>Some  page  title</title>        <style>              #container{                      width:600px;              }        </style>        <script  src="jquery.min.js"></script>        <script>              //your  jquery  commands  go  here        </script>  !</head>

jQuery Library

Your jQuery commands

Page 17: Jquery News Packages

Where do I put jQuery?

<head>  !      <title>Some  page  title</title>        <style>              #container{                      width:600px;              }        </style>        <script  src="jquery.min.js"></script>        <script>              //your  jquery  commands  go  here        </script>  !</head>

jQuery Library

Your jQuery commands

CSS code (before/after)

Page 18: Jquery News Packages

Review

Page 19: Jquery News Packages

What is jQuery?

Page 20: Jquery News Packages

Where on my web page do I put jQuery?

Page 21: Jquery News Packages

Understanding syntax

Page 22: Jquery News Packages

Understanding the syntax

$

Page 23: Jquery News Packages

Understanding the syntax

$This means 'jQuery'

Page 24: Jquery News Packages

What do you want to manipulate?

$(".somebox")$("#video")$("div")

Page 25: Jquery News Packages

What do you want to manipulate?

$(".somebox")$("#video")$("div")

Page 26: Jquery News Packages

What do you want to manipulate?

$(".somebox")$("#video")$("div")

Page 27: Jquery News Packages

What do you want to manipulate?

$(".somebox")$("#video")$("div")

Page 28: Jquery News Packages

What do you want to manipulate?

$(".somebox")$("#video")$("div")

Note the quotes and parentheses

Page 29: Jquery News Packages

jQuery commands

$("#container").doSomething();

Page 30: Jquery News Packages

jQuery commands

$("#container").doSomething();

Page 31: Jquery News Packages

jQuery commands

$("#container").doSomething();

Page 32: Jquery News Packages

jQuery commands

$("#container").doSomething();

Page 33: Jquery News Packages

jQuery commands

$("#container")$("#container")$("#container")

Page 34: Jquery News Packages

jQuery commands

$("#container")$("#container")$("#container")

.fadeOut();

Page 35: Jquery News Packages

jQuery commands

$("#container")$("#container")$("#container")

.fadeOut();

.hide();

Page 36: Jquery News Packages

jQuery commands

$("#container")$("#container")$("#container")

.fadeOut();

.hide();

.slideDown();

Page 37: Jquery News Packages

Parts of jQuery command

$("#container").fadeOut();

Page 38: Jquery News Packages

Parts of jQuery command

$("#container").fadeOut();

jQuery

Page 39: Jquery News Packages

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

Page 40: Jquery News Packages

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

Page 41: Jquery News Packages

css selector

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

Page 42: Jquery News Packages

css selector

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

dot

Page 43: Jquery News Packages

commandcss selector

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

dot

Page 44: Jquery News Packages

commandcss selector

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

dot parentheses

Page 45: Jquery News Packages

commandcss selector

Parts of jQuery command

$("#container").fadeOut();

parentheses

jQuery

quotes

dot parentheses

semicolon

Page 46: Jquery News Packages

Review

Page 47: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

Page 48: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$

Page 49: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$(

Page 50: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("

Page 51: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container

Page 52: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container"

Page 53: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container")

Page 54: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container").

Page 55: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container").fadeOut

Page 56: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container").fadeOut()

Page 57: Jquery News Packages

Describe the jQuery command to fade out a div element with the id

name "container"

$("#container").fadeOut();

Page 58: Jquery News Packages

jQuery in action<div  id="box"></div>

Page 59: Jquery News Packages

jQuery in action

$("#box").fadeOut();

<div  id="box"></div>

Page 60: Jquery News Packages

jQuery in action<div  id="box"></div>

Page 61: Jquery News Packages

jQuery in action<div  id="box"></div>

$("#box").slideUp();

Page 62: Jquery News Packages

jQuery in action<div  id="box"></div>

Page 63: Jquery News Packages

jQuery in action<div  id="box"></div>

$("#box").hide();

Page 64: Jquery News Packages

jQuery in action<div  id="box"></div>

Page 65: Jquery News Packages

jQuery in action

Hello World

<div  id="box"></div>

$("#box").text("Hello  World");

Page 66: Jquery News Packages

Parameters

Page 67: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut();

Page 68: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut();

parentheses

Page 69: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut("slow");

Page 70: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut("slow");

parameter

Page 71: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut(3000);

Page 72: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut(3000);

parameter

Page 73: Jquery News Packages

Parameters in jQuery

$("#container").text("Hello  World");

All text has to be in quotes, unless it is some special command recognized by JavaScript.

Page 74: Jquery News Packages

Parameters in jQuery

$("#container").text("Hello  World");

quotes

All text has to be in quotes, unless it is some special command recognized by JavaScript.

Page 75: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut(200);

Will fade out element with id #container in 200 milliseconds

Page 76: Jquery News Packages

Parameters in jQuery

$("#container").fadeOut(200);

Will fade out element with id #container in 200 milliseconds

Page 77: Jquery News Packages

Review

Page 78: Jquery News Packages

Why is there a parenthesis at the end of every jQuery command?

$("#someid").command();

wtf?

Page 79: Jquery News Packages

Why is there a parenthesis at the end of every jQuery command?

$("#someid").command();

wtf?

To sometimes include additional information about how the command should operate

oh.

Page 80: Jquery News Packages

What does the following jQuery command do?

$("#title").fadeOut(4000);

Page 81: Jquery News Packages

What does the following jQuery command do?

$("#title").fadeOut(4000);

Fades out some HTML element with an id attribute "title" over the course of four seconds

Page 82: Jquery News Packages

What's wrong with the following jQuery command?

$("#footer").text(This  is  copyrighted);

Page 83: Jquery News Packages

What's wrong with the following jQuery command?

$("#footer").text(This  is  copyrighted);

When the parameter is text, it NEEDS to have quotes or it won't work.

$("#footer").text("This  is  copyrighted");

Page 84: Jquery News Packages

Multiple Parameters

Page 85: Jquery News Packages

Multiple Parameters

$("#container").css("background",  "red");

Page 86: Jquery News Packages

2nd parameter

Multiple Parameters

$("#container").css("background",  "red");

1st parameter

Page 87: Jquery News Packages

2nd parameter

Multiple Parameters

$("#container").css("background",  "red");

1st parameter

comma

Page 88: Jquery News Packages

Multiple Parameters

$("#container").fadeTo(5000,  0.5);

Page 89: Jquery News Packages

Multiple Parameters

$("#container").fadeTo(5000,  0.5);

Page 90: Jquery News Packages

Multiple Parameters

$("#container").fadeTo(5000,  0.5);

comma

Page 91: Jquery News Packages

Review

Page 92: Jquery News Packages

What character symbol do I use to separate multiple parameters?

Page 93: Jquery News Packages

What character symbol do I use to separate multiple parameters?

A comma

$("#container").css("background",  "red");

Page 94: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

Page 95: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$

Page 96: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$(

Page 97: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box"

Page 98: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box")

Page 99: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").

Page 100: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css

Page 101: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css(

Page 102: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css("color"

Page 103: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css("color",

Page 104: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css("color","blue"

Page 105: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css("color","blue")

Page 106: Jquery News Packages

How would I write a command to change the CSS of #box so that the

text color is blue?

$("#box").css( ;"color","blue")

Page 107: Jquery News Packages

Functions

Page 108: Jquery News Packages

What are functions?

• Functions are simply blocks of code that are in a waiting queue to be run.

• Think of it as a way of saying "do the following…" but only when activated.

• Functions only run at certain times. In jQuery, a function is usually runs when a user clicks a button, or performs some type of action.

Page 109: Jquery News Packages

jQuery functions

$("#button").on("click"…

Page 110: Jquery News Packages

jQuery functions

$("#button").on("click"…

wait for something to happen

Page 111: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){});

Page 112: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){});

Page 113: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){});

parenthesis, which we won't ever use in functions

Page 114: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){});

parenthesis, which we won't ever use in functions

We put code inside the curly brackets to be called only after the event click happens.

Page 115: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){});

Page 116: Jquery News Packages

jQuery functions

$("#button").on("click",  function(){

});

Code only runs after!button is clicked.

$("#box").fadeOut();

Page 117: Jquery News Packages

Anatomy of a function

function  (){}

Page 118: Jquery News Packages

Anatomy of a function

function  (){

}

Code goes here

Page 119: Jquery News Packages

Review

Page 120: Jquery News Packages

What's a function?

Page 121: Jquery News Packages

What's a function?

A block of code which is only run at a certain time, like after an event, such as when a user

clicks on a button.

Page 122: Jquery News Packages

Describe all the parts of a function from beginning to end.

Page 123: Jquery News Packages

Describe all the parts of a function from beginning to end.

function

Page 124: Jquery News Packages

Describe all the parts of a function from beginning to end.

function()

Page 125: Jquery News Packages

Describe all the parts of a function from beginning to end.

{}function()

Page 126: Jquery News Packages

Where does the code go that should execute when this function is run?

{}function()

Page 127: Jquery News Packages

Where does the code go that should execute when this function is run?

{}function()

Right here

Page 128: Jquery News Packages

What does the following do in jQuery?

$("#button").on("click",  function(){  !   $("#title").fadeOut("fast");  !});

Page 129: Jquery News Packages

What does the following do in jQuery?

$("#button").on("click",  function(){  !   $("#title").fadeOut("fast");  !});

When the HTML element with the id attribute "button" is clicked, an element "title" will fade out quickly.

Page 130: Jquery News Packages

Animation

Page 131: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

Just to expose you to this command (don't worry about memorizing it)

Page 132: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

Just to expose you to this command (don't worry about memorizing it)

Page 133: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

Just to expose you to this command (don't worry about memorizing it)

Page 134: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

css property value

Just to expose you to this command (don't worry about memorizing it)

Page 135: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

curly brackets

css property value

Just to expose you to this command (don't worry about memorizing it)

Page 136: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

curly brackets

colon

css property value

Just to expose you to this command (don't worry about memorizing it)

Page 137: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

curly brackets

colon

milliseconds

css property value

Just to expose you to this command (don't worry about memorizing it)

Page 138: Jquery News Packages

animate method

$("#box").animate({'left':'50px'},  3000);

jquery selector

jquery command

curly brackets

colon

milliseconds

css property value

Just to expose you to this command (don't worry about memorizing it)

Page 139: Jquery News Packages

jQuery "ready"

• jQuery can't run until the entire webpage is fully loaded.

• Because web pages run from the top to bottom, we need to place a trigger to prevent jQuery from running until the web page is fully loaded.

Page 140: Jquery News Packages

All jQuery code goes into a "ready" block

$(document).ready(function(){  !!!});