Writings, notes, and work of a Boston based designer

Results for jQuery

jQuery Timeout Function Jan 22 + 2 comments

written by: ryan coughlin

UPDATE: Edited the code thanks to Adrian Green. Thanks for the comment

jQuery does offer callbacks for most of its functions, but what if you want to delay or timeout a certain effect or function there isn’t a function made for that. This article should help you achieve that.

Old School JavaScript

I have been trying to search for a nice and reusable way to use jQuery to timeout effects or functions. We are familiar with using the traditional JavaScript function setTimeout():

setTimeout (function,timeout);

After putting in some data we can come up with something like this:

setTimeout (showBox(),2500);

Remember that the value 2500 is in MS (milliseconds), we equates to 2.5 seconds. When this function is called, it will execute 2500ms later. I was using this for one of my projects where I wanted two effects/functions not after, but during. I could have used very handy jQuery callback and load in a function, but it would have been executed after the first function is completed.

Let’s Start Delaying Effects – The Code

Lets jump right into the belly of the beast and view the code to call our handy jQuery timeout function. Essentially we have a small jQuery plugin. We define the function as: $.idle()

jQuery.fn.idle = function(time){
	var i = $(this);
	i.queue(function(){
		setTimeout(function(){
			i.dequeue();
		}, time);
	});
};

You see on the first line above we create the idle function: jQuery.fn.idle. We created the variable I to store $(this), which is going to be element that is running the $.idle(); function.

We then use the jQuery queue(); function whose documentation can be found on the jQuery website. Then we attach the $(this); variable we created called: i to it, and create the setTimeout(), which then uses dequeue(); with our variable connected to it.

The last part, where we create the time parameter for the function, so we call the following code and:

$.idle(2500);

time gets replaced with the user input of 2500ms to the function. Say if you want to call a function you can use it in this context instead of the messy old fashion setTimeout(); function.

$('.myelement').fadeIn().idle(2000).fadeOut('slow');

The above simple reads: fade in my element, then wait 2000ms, slowly fade out the same element. BOOM! There it is, the quick n’ dirty method to delaying functions using jQuery.

jQuery Search And Highlight Dec 20 + 2 comments

written by: ryan coughlin

I was sitting in class and wanted to come up with some simple way to add a inline same page ajax search on a website. I came across the jQuery selector :contains(text), check out the documentation for this. Now the to the final product, lets check out the demo. We are going to have our main components for this:

  • The form (1 input and submit)
  • The :contains selector
  • The fadeIn() effect
  • The addClass()

The approach for the code is going to be like

  • User will enter term in form
  • Save data to a variable
  • Place variable in to :contains selector
  • Fade in the highlight class (using fadeIn() and addClass())

(more…)

Combining Selectables And Draggables Using jQuery UI Nov 23 + 2 comments

written by: ryan coughlin

There are two powerful and fun elements that the jQuery UI provides. When both of them work together you will be able to come up with some creative web application uses for them. The two I am going to be covering are:

I am sure you all want to see what the final product will look like, check it out.

(more…)

Two Panel Slick Drop Down Using jQuery Nov 16 + 2 comments

written by: ryan coughlin
UPDATE: I have updated the code and the post here, I have optimized and narrowed down the number of lines, the new demo is up and running.

I use to use something similar to this on my older site to bring down content and a contact form on one side using jQuery, then about a year later I was thinking why not bring it back up, change some styles, and write a post for others to use. The concept is pretty simple.

When you press the selector, it will slide down panel 1, then using window.setTimeout, we will call the panel 2 to come down 100ms later, just enough of a delay where we get a nice effect. Once the panels slide down, the content within are faded in using fadeIn(); To close the panels we call a closePanels() that does the opposite effect when it is sliding down.

(more…)

Using the jQuery UI Slider Nov 4 + 2 comments

written by: ryan coughlin

Since I started working with jQuery and the UI. I have always wanted to work with the UI Slider. I always tried playing with it but I never got it execute an operation on slide. I created a slider to change the opacity of my background on my older temp site here at ryancoughlin.com.

UPDATE: Edit changes to the code with available demo below.javascript:;

UPDATE (9/16/09): Created a new jQuery UI Slider example, check it out.

(more…)