1. Code
  2. News

Learn How to Create a jQuery Plugin

Scroll to top
Read Time: 3 min

You might think to yourself, "What is all the fuss with jQuery? You have to download a bunch of plugins to even make the library worth while.". First, that isn't true. Second, the jQuery library was specifically designed for that very purpose. By keeping the core library as small as possible - about 16 kb - users can then apply additional plugins at their own discretion. Today, I'll teach you how to build your first "Center" plugin from scratch. Let's get started!

Our Objective

We want to create a plugin that will dynamically adjust the styling of a specified element in order to keep it vertically and horizontally centered on the page at all times - even when the browser window is resized. Very little is required up front. You only need to make sure that you have the jQuery library downloaded.

The Screencast

Step 1

The first step when creating a plugin is to add a blank Javascript file. Naming conventions state that the file should be named "YourPluginName.jQuery.js". Once you've created this file, make sure that you create a reference to it in your document.

Step 2

Next, paste in the following code.

I go into much greater detail in the video, however, I'd still like to go over a few key points. Any time that you create a plugin, it must be wrapped with:

"Center" should be replaced with whatever your plugin's name is. This lets jQuery know that you're extending its methods. Now, though it does absolutely nothing, we can call our center method with:

Step 3

You must understand how to manually center an image on a page before creating the plugin. First, your element must be positioned absolutely. Otherwise, it obviously won't budge when we alter the "left" and "top" values. Next, the image needs to be shifted 50% of the browser's width to the left. Finally, in order to compensate for the image's width, we need to subtract one half of the image's width.

This will perfectly place the center of the image in the center of the page. It's a bit hard to explain with text. Be sure to watch the video for a greater explanation.

Step 4

Continuing on, we need to create a listener for when the browser window is resized.

"ChangeCss()" is a function that adjusts the left and top values of our image. By calling it again when the window is resized, jQuery will recalulate those values.

You're Done!

If you have any questions, please feel free to leave a comment and I'll make sure that I respond. As always, this might not be "real world ready". What happens if the user has Javascript turned off? And of course, there are some ways to do this using pure CSS - but I digress.

Subscribe to the Weekly Screencasts

  1. Once ITUNES has loaded, clicked the "Advanced Tab"
  2. Choose "Subscribe to Podcast"
  3. Enter ""

That should do it! The screencast will also be searchable on ITUNES in the next twenty four hours.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.