j query - your first steps

14
jQuery: Love at first sight...or site! Bronson Quick sennza | (07) 3040-1545 | [email protected] | http://www.sennza.com.au/ | Twitter: @sennza

Upload: bronson-quick

Post on 15-Jan-2015

1.323 views

Category:

Education


0 download

DESCRIPTION

A basic intro into jQuery talk I prepared for a Meetup.com talk. You can grab the code here: http://www.sennza.com.au/jQuery-intro.zip

TRANSCRIPT

Page 1: J Query - Your First Steps

jQuery:Love at first sight...or site!

Bronson Quick

sennza | (07) 3040-1545 | [email protected] | http://www.sennza.com.au/ | Twitter: @sennza

Page 2: J Query - Your First Steps

What we’ll cover

A brief intro into the jQuery including:

• Adding jQuery to a web page

• Calling jQuery after the DOM has loaded

• Basic selectors

• Basic events & callback functions

• Chaining

rethink | redesign | results Slide 2 of 14

The basics

Page 3: J Query - Your First Steps

Adding jQuery

Before we use get to fool around with the awesomeness that is jQuery we have to tell the browser what we’re talking ‘about!

rethink | redesign | results Slide 3 of 14

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Given the number of pages using jQuery these days there is a good chance that jQuery will be already cached on the users browser.

Speed up the page load time!

See if it’s working:

$('<p>Oh god yes!</p>').appendTo('body');

Give it to me jQuery! Uhuh, Uhuh!

Use Google’s CDN

Locally or from a CDN

Where is your page going to be viewed?

• Locally (Offline)

• Online

• Highly secured network

Once you know the environment then you will know if you have to ship your app/page with jQuery or if you can use a Content Delivery Network (CDN).

Page 4: J Query - Your First Steps

Using jQuery

Before we can add some jQuery sexiness we need to let the browser know that we are going to do something!

rethink | redesign | results Slide 4 of 14

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Given the number of pages using jQuery these days there is a good chance that jQuery will be already cached on the users browser.

Speed up the page load time!

I was used! Use Google’s CDN

$(document).ready(function(){

alert(‘The world is a vampire!’);

});

The long way

$(function(){

alert(‘The world is a vampire!’);

});

The ‘quick’ way

Page 5: J Query - Your First Steps

Selectors

<div id=“yo”>

<p>I like stuff. Do you like stuff?</p>

</div>

rethink | redesign | results Slide 5 of 14

<ul><li class="awesome">Google Search</li><li class="awesome">Google Docs</li><li>Google Chrome</li></ul>

$(function(){$(“.awesome").css({

'backgroundColor':'#652D90','color': '#fff'

});});

ID, classes & elements Get element by class name

$(function(){$("#yo").css({

'backgroundColor':'#652D90','color': '#fff'

});});

Get an element by ID

Page 6: J Query - Your First Steps

Selectors

<h1>I am a heading!</h1>

rethink | redesign | results Slide 6 of 14

<table><tr><td>I'm even!</td></tr><tr><td>I'm odd!</td></tr><tr><td>I'm even!</td></tr><tr><td>I'm odd!</td></tr></table>

$(function(){$("td:even").css({

'backgroundColor':'#652D90','color': '#fff'

});});

ID, classes & elements Pseudo class selectors

$(function(){$(“h1").css({

'backgroundColor':'#652D90','color': '#fff'

});});

This will get all HTML elements on the page so if you had multiple <h1>’s this change would apply to all of them.

Remember:

http://css-tricks.com/pseudo-class-selectors/

A great list of pseudo class selectors

Page 7: J Query - Your First Steps

Events

<h1>I am a heading!</h1>

rethink | redesign | results Slide 7 of 14

.click()

.dblclick()

.focus()

.keypress()

.mouseover()

.hover()

Lets interact with something Some handy events

$(function(){$("h1").click(function(){

$(this).css({

'backgroundColor':'#652D90','color': '#fff'});

});});

http://api.jquery.com/category/events/

The full list of events

Page 8: J Query - Your First Steps

Effects

<h1>I am a heading!</h1>

<a href=“#” title=“Hide heading” id=“hide”>Hide heading</a>

<a href=“#” title=“Show heading” id=“show”>Show heading</a>

rethink | redesign | results Slide 8 of 14

.hide(‘fast’);

.hide(‘slow’);

.hide(4000);

$(function(){$("#hide").click(function(){

$("h1").hide('fast');});$("#show").click(function(){

$("h1").show(‘slow’);});

});

Peek a boo! Fast, slow or milliseconds

$(function(){$(“#hide”).click(function(){

$(“h1”).hide();});$(“#show”).click(function(){

$(“h1”).show();});

});

Page 9: J Query - Your First Steps

Effects

<div id=“box”>I’m a box!</div>

<a href=“#” title=“Slide up” id=“up”>Slide up</a>

<a href=“#” title=“Slide down” id=“down”>Slide down</a>

rethink | redesign | results Slide 9 of 14

$(function(){$(“#up”).click(function(){

$(“#box”).slideUp(‘fast’);

});$(“#down”).click(function(){

$(“#box”).slideDown(‘slow’);

});});

Slide up, slide down, toggle Fast, slow or milliseconds

$(function(){$(“#up”).click(function(){

$(“#box”).slideUp();});$(“#down”).click(function(){

$(“#box”).slideDown();});

});

Page 10: J Query - Your First Steps

Effects

<div id=“box”>I’m a box!</div>

<a href=“#” title=“Fade In” id=“in”>Fade In</a>

<a href=“#” title=“Fade Out” id=“out”>Fade Out</a>

rethink | redesign | results Slide 10 of 14

$(function(){$(“#in”).click(function(){

$(“#box”).fadeIn(‘slow’);

});$(“#out”).click(function(){

$(“#box”).fadeOut(‘fast’);

});});

I’m fading away to nothing! Fast, slow or milliseconds

$(function(){$(“#in”).click(function(){

$(“#box”).fadeIn();});$(“#out”).click(function(){

$(“#box”).fadeOut();});

});

http://api.jquery.com/category/effects/

The full list of effects

Page 11: J Query - Your First Steps

Chaining<div id="box">I'm a box!</div><a href="#" title="Chaining” id="chain">Chained</a>

rethink | redesign | results Slide 11 of 14

Alice in Chains!

$(function(){$("#chain").click(function(){

$("#box").fadeOut('slow').fadeIn(4000).slideUp();});

});

Page 12: J Query - Your First Steps

Links

rethink | redesign | results Slide 12 of 14

Some useful jQuery links

http://jquery.com/

http://api.jquery.com/

http://jqueryui.com/

Page 13: J Query - Your First Steps

Thanks & Questions

rethink | redesign | results Slide 13 of 14

Blatant plug for our WordPress Meetup groupWordPress Brisbane

http://www.meetup.com/WordPress-Brisbane

Page 14: J Query - Your First Steps

[email protected]://www.sennza.com.au/http://www.youtube.com/user/BronsonQuickTwitter: @sennza

rethink | redesign | results