SlideShare a Scribd company logo
1 of 19
Writing jQuery Plugins
           Casey West
      casey@geeknest.com
      http://caseywest.com
Get Setup
(function($) {

 // Put your plugin here.

})(jQuery);
Extend jQuery
$.extend({
  doImportantWork: function(){

      console.log(
        “I’m working hard!”);

  }
});
Use It
$.doImportantWork();
Extend jQuery Selector
 $.fn.extend({

   rise: function(options) {
   },

   fall: function(options) {
   }

 });
Use It
$(‘#quote’).rise();
$(‘#quote’).fall();

$(‘.callouts’).rise();
$(‘.callouts’).fall();
Get Ready for
       Selectors
(function($) {
  $(document).ready(function(){

   // We’re ready to spice it up!

  });
})(jQuery);
Click It
$(document).ready(function() {
  var $rise = $(‘.callout a.rise’);
  $rise.click(function(e) {
    e.preventDefault();
    $(this).parents(‘.callout’)
           .rise();
  });

});
Get Options
$(‘.callout’).rise({
  elevation: 10
});
Handling Options
$.fn.extend({
  rise: function(options) {
    var defaults = {
      elevation: 10,
      speed:     0.4,

        onComplete: null
      };
      var o = $.extend(defaults, options);
  }
});
Plugging It In
return $(this).each(function(){
  var $elem = $(this);

  $elem.css({
    backgroundColor: ‘#ffc’
  });
});
Plugging It In
return $(this).each(function(){
  var $elem = $(this),
      scale = 1 + (o.elevation/100),
      blur = o.elevation * 4,
      alpha = (blur * 2) / 100;
  $elem.css({
    '-webkit-transform': 'scale(' + scale + ')',
    '-webkit-transition': 'all ' + o.speed + 's',
    '-webkit-box-shadow': '0px 0px ' + blur
                         +'px rgba(0,0,0,' + alpha + ')'
  });

  if (o.onComplete) o.onComplete($elem);
});
Turn It On (HTML)
<h1>I Like Hovercrafts</h1>

<div class="callout" id="important">
  <p>You need to know about this!</p>
</div>
<p>
  Listen, you should definitely
  <a href="#important" class="rise">
     lift it up!</a>
</p>
Turn It On (JavaScript)
$('a.rise').click(function(e) {
  e.preventDefault();
  e.stopImmediatePropagation();

 var callout =
   $(this).attr('href');

  $(callout).rise();
});
Customize It
$(callout).rise({
  elevation: 10,
  speed: 1
});
It’s On. Do Stuff
onComplete: function ($elem) {
  $elem.prepend(
     $('<a></a>').hide().fadeIn().addClass('fall')
     .click(function(){
        $elem.fall({
          onComplete: function($elem) {
            $elem.find('.fall').fadeOut().remove();
          }
        }); // END      $elem.fall()
     })      // END   $(‘a.fall’).click()
  );         // END $elem.prepend()
}
Action Packed
See supporting source code...
Questions?
Thanks!

More Related Content

What's hot (18)

Pimp your site with jQuery!
Pimp your site with jQuery!Pimp your site with jQuery!
Pimp your site with jQuery!
 
How to Listen
How to ListenHow to Listen
How to Listen
 
Cervantes
CervantesCervantes
Cervantes
 
Add tag shortcode
Add tag shortcodeAdd tag shortcode
Add tag shortcode
 
การบันทึกและออกจากโปรแกรมMspowerpoint
การบันทึกและออกจากโปรแกรมMspowerpointการบันทึกและออกจากโปรแกรมMspowerpoint
การบันทึกและออกจากโปรแกรมMspowerpoint
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
 
Desafios de Performance Web - BrazilJS
Desafios de Performance Web - BrazilJSDesafios de Performance Web - BrazilJS
Desafios de Performance Web - BrazilJS
 
Historia berribide hasta 40 años
Historia berribide hasta 40 añosHistoria berribide hasta 40 años
Historia berribide hasta 40 años
 
Presentación JxNet - Software de Gestión Jurídica Corporativa
Presentación JxNet - Software de Gestión Jurídica CorporativaPresentación JxNet - Software de Gestión Jurídica Corporativa
Presentación JxNet - Software de Gestión Jurídica Corporativa
 
Session4
Session4Session4
Session4
 
JavascriptMVC
JavascriptMVCJavascriptMVC
JavascriptMVC
 
08 cercles et sphères
08 cercles et sphères08 cercles et sphères
08 cercles et sphères
 
jQuery プラグインの作り方
jQuery プラグインの作り方jQuery プラグインの作り方
jQuery プラグインの作り方
 
Jquery overview 2013
Jquery overview 2013 Jquery overview 2013
Jquery overview 2013
 
Performance Web além do carregamento
Performance Web além do carregamentoPerformance Web além do carregamento
Performance Web além do carregamento
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
 
Separate ways journey
Separate ways journeySeparate ways journey
Separate ways journey
 
Seminario business angel the hive iban (1)
Seminario business angel the hive   iban (1)Seminario business angel the hive   iban (1)
Seminario business angel the hive iban (1)
 

Viewers also liked

Memphis php 01 22-13 - laravel basics
Memphis php 01 22-13 - laravel basicsMemphis php 01 22-13 - laravel basics
Memphis php 01 22-13 - laravel basicsJoe Ferguson
 
OWASP AppSec USA 2011 - Dismantling Web Malware
OWASP AppSec USA 2011 - Dismantling Web MalwareOWASP AppSec USA 2011 - Dismantling Web Malware
OWASP AppSec USA 2011 - Dismantling Web MalwareAditya K Sood
 
jQuery Stack Overflow DevDays DC 2009
jQuery Stack Overflow DevDays DC 2009jQuery Stack Overflow DevDays DC 2009
jQuery Stack Overflow DevDays DC 2009Richard D. Worth
 
Zero to Hero, a jQuery Primer
Zero to Hero, a jQuery PrimerZero to Hero, a jQuery Primer
Zero to Hero, a jQuery PrimerMatthew Buchanan
 
Knolx j query-form-validation-slides
Knolx j query-form-validation-slidesKnolx j query-form-validation-slides
Knolx j query-form-validation-slidesKnoldus Inc.
 
Web Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris UriarteWeb Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris Uriartewebhostingguy
 
Php Security Workshop
Php Security WorkshopPhp Security Workshop
Php Security WorkshopAung Khant
 
Scalable Internet Servers and Load Balancing
Scalable Internet Servers and Load BalancingScalable Internet Servers and Load Balancing
Scalable Internet Servers and Load BalancingInformation Technology
 
UpsilonPiEpsilon-UniversityOfBridgeport-May1997
UpsilonPiEpsilon-UniversityOfBridgeport-May1997UpsilonPiEpsilon-UniversityOfBridgeport-May1997
UpsilonPiEpsilon-UniversityOfBridgeport-May1997Muthuselvam RS
 
CSS3 and jQuery
CSS3 and jQueryCSS3 and jQuery
CSS3 and jQuerypsophy
 
Anatomy of Fraud (2010 & 2013)
Anatomy of Fraud (2010 & 2013)Anatomy of Fraud (2010 & 2013)
Anatomy of Fraud (2010 & 2013)Jerry Ocampo
 
Safety LAMP: data security & agile languages
Safety LAMP: data security & agile languagesSafety LAMP: data security & agile languages
Safety LAMP: data security & agile languagesPostgreSQL Experts, Inc.
 

Viewers also liked (20)

RHCSA
RHCSARHCSA
RHCSA
 
Memphis php 01 22-13 - laravel basics
Memphis php 01 22-13 - laravel basicsMemphis php 01 22-13 - laravel basics
Memphis php 01 22-13 - laravel basics
 
A brief look inside UML
A brief look inside UMLA brief look inside UML
A brief look inside UML
 
OWASP AppSec USA 2011 - Dismantling Web Malware
OWASP AppSec USA 2011 - Dismantling Web MalwareOWASP AppSec USA 2011 - Dismantling Web Malware
OWASP AppSec USA 2011 - Dismantling Web Malware
 
Web Security
Web SecurityWeb Security
Web Security
 
Cinematic UX Design
Cinematic UX DesignCinematic UX Design
Cinematic UX Design
 
jQuery Stack Overflow DevDays DC 2009
jQuery Stack Overflow DevDays DC 2009jQuery Stack Overflow DevDays DC 2009
jQuery Stack Overflow DevDays DC 2009
 
Zero to Hero, a jQuery Primer
Zero to Hero, a jQuery PrimerZero to Hero, a jQuery Primer
Zero to Hero, a jQuery Primer
 
Knolx j query-form-validation-slides
Knolx j query-form-validation-slidesKnolx j query-form-validation-slides
Knolx j query-form-validation-slides
 
Web Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris UriarteWeb Technology – Web Server Setup : Chris Uriarte
Web Technology – Web Server Setup : Chris Uriarte
 
jQuery
jQueryjQuery
jQuery
 
Php Security Workshop
Php Security WorkshopPhp Security Workshop
Php Security Workshop
 
Scalable Internet Servers and Load Balancing
Scalable Internet Servers and Load BalancingScalable Internet Servers and Load Balancing
Scalable Internet Servers and Load Balancing
 
UpsilonPiEpsilon-UniversityOfBridgeport-May1997
UpsilonPiEpsilon-UniversityOfBridgeport-May1997UpsilonPiEpsilon-UniversityOfBridgeport-May1997
UpsilonPiEpsilon-UniversityOfBridgeport-May1997
 
CSS3 and jQuery
CSS3 and jQueryCSS3 and jQuery
CSS3 and jQuery
 
Cross platform php
Cross platform phpCross platform php
Cross platform php
 
Anatomy of Fraud (2010 & 2013)
Anatomy of Fraud (2010 & 2013)Anatomy of Fraud (2010 & 2013)
Anatomy of Fraud (2010 & 2013)
 
Apache Web Server Setup 2
Apache Web Server Setup 2Apache Web Server Setup 2
Apache Web Server Setup 2
 
Safety LAMP: data security & agile languages
Safety LAMP: data security & agile languagesSafety LAMP: data security & agile languages
Safety LAMP: data security & agile languages
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 

jQuery Plugins Intro