Tutorial: How to create your own continuously slide effect plugin with jQuery

The effect i show up till now are all discontinuous effect that can be easily create using CSS and jQuery. Some people may wonder how continuous sliding effect is being achieved. In this tutorial, i will demonstrate the concept and a plugin which i build immediately during this tutorial on how to simultaneously pull out the containers in the easily way. The demo can be view at the bottom of this post.

Concept

The concept for continuously sliding effect is really not that difficult. Imagining you have a photo frame and a long list of photo arranged in a straight row. The list is placed behind the photo frame and you started to pull it accordingly to  display a new photo within the frame. Anything outside the frame is being blocked which restrict the display only within the frame itself. This way you will see a continuously slide effect being displayed.

CSS

From the description above, it is clear that css is required to assist on building the effect (every type of effect cannot escape the demon hand of CSS! RAWR!). And below listed the required CSS rule from the description written above.

  1. float: left -  to flow all containers in a straight row
  2. position: relative - so that the container can be hidden in IE and FF
  3. overflow: hidden -  we want to hide all other element outside the frame
  4. margin-left or left: - this is where we adjust the element so that it get pull to the left or right within the frame

HTML

On the description above, we will have a rough idea how many containers are required to built the effect and plugin. The following list down the approximate container required.

  1. Main Frame
  2. container to contain x element
  3. the x number of elements in the frame
  4. next button
  5. right button

Everything is put it simple so that we have a clear idea how this is being done. The reason why we need the extra container to contain the x element is because the element WILL NOT float side by side if you do not define a length that is big enough for all elements to go side by side.

Coding

This part always get a bit confusing when there are piece and pieces of code flying around the place. But i will try my best to explain what is happening in here.

On the HTML page, i have allocated the following containers as describe above.

<div id='frame'>
	<div id='container'>
		<div class='box' id='box1'></div>
		<div class='box' id='box2'></div>
		<div class='box' id='box3'></div>
		<div class='box' id='box4'></div>
	</div>
<div id='prev'></div>
<div id='next'></div>
</div>

It should be clear that there is 3 main containers which are the frame,next and right button. And within the frame there is another container which contain the 4 element that we will be interested to slide them around. The reason why both navigation button are within the frame so that the button will always be within the container.

On the CSS external script, the below listed out all the necessary elements that we mention on the CSS section.

body{
margin: 0 auto;					/*align the display at the center*/
text-align: center;				/*align the display at the center*/
}

div#frame{
margin: 0 auto;					/*align the frame at the center*/
width: 50em;					/*frame width*/
height: 25em;					/*frame height*/
border: #000 solid 10px;		/*frame border*/
position: relative;				/*position relative so that it gets hide in IE*/
overflow: hidden;				/*hide all overflow element out of the frame*/
}
div#container
{
position: relative;				/*position relative so that it gets hide in IE*/
width: 200em;					/*the width must be big enough so that all elements can align side by side*/
height: 25em;					/*container height*/
}
div.box
{
float: left;					/*float each element side by side*/
width: 50em;
height: 25em;
position:: relative;
}

div#box1
{
background-color: #66A666;
}
div#box2
{
background-color: #E37373;
}
div#box3
{
background-color: #B2B2B2;
}
div#box4
{
background-color: #A51515;
}

div#next
{
width: 3em;
height: 5em;
top: 50%;			/*align it vertically center*/
margin-top: -2.5em;	/*align it vertically center*/
right: 0em;
position: absolute;
background: transparent url(../images/next.png);
}
div#prev
{
width: 3em;
height: 5em;
left: 0em;
top: 50%;			/*align it vertically center*/
margin-top: -2.5em;	/*align it vertically center*/
position: absolute;
background: transparent url(../images/prev.png);
}

If you wonder how the left and right button is aligned on center, you can see the explanation at How to align center on the screen when using position absolute with CSS
i have indicated the comment on the code itself so that it can be self explain instead. After all of these settings, you should be able to get something like this.

sliding container
sliding container

Look pretty decent right? Now come the real code in jQuery. So we want to pull to the next element  to the right to simulate continuously sliding, we will attach an event handler to both next and previous button. Using the animate() function in jQuery, this will be a piece of cake.

$(function(){
var i = 0;
	$(this).click(function()
	{
		if(this.id==op.prevImageID.replace("#",""))
		{
			i++;
			if(i > 0)
			i=(0-(op.noOfContainer-1))
		}
		else if(this.id==op.nextImageID.replace("#",""))
		{
			i--;
			if(i<(0-(op.noOfContainer-1)))
			i=0;

		}
		$("div#container").animate({marginLeft: i*op.containerW+"em"},op.duration);
		
	});

The first sentence in the code indicate that if previous button is pressed, we add the counter and check whether it is more than the available container. If the counter has go pass the available containers, we will push it to the last container available. We did the same for the next button too! but in descending order. Finally, we animate the scrolling according to the the given counter with the size of each element.

Plugin

I really do not know whether there is a need for such plugin since its really quite simple. But i will still provide it like i said previously. So with the explanation above, i will refractor the code a bit so that it is flexible enough to become a plugin.

Parameter

  • prevImageID: previous button image id
  • nextImageID: next button image id
  • noOfContainer: number of container
  • containerW: width of container
  • duration: duration of animation

in order to change the display, just change the css code in ini.css. No hardcoded code in the javascript itself. There can be more flexibility in the future of this plugin which i will place in more feature if i get the time. If you would like to support me, you can always throw in some bucks for the maintance of this plugin.

[donate]

File

  • Current version: 1.0
  • Last Update: 09/8/2009
  • Licensing: MIT
  • Download
  • demo

jQuery Plugin: RAWR! Image Effect Gallery (RIEG)

RAWR image effect gallery(RIEG) is a flexible and fully customize image gallery which can be adjusted to fill your need. It is design so that it is simple to use without much complexity on the usage of the plugin. Currently, RIEG plugin provides 7 fixed and a custom effect with different combination of easing which can be configured within the plugin using George easing plugin. RIEG is currently released on alpha testing and i will try to update it in the future with more effect and feature.

Information

  • Current version: 1.0 (Alpha)
  • Licensing: MIT

Browser Tested

  • Firefox 3.0.7
  • IE 7.0.5730.13
  • Opera 9.64
  • Google Chrome
  • Safari

Dependencies

  • lightbox by Warren Krewenki (optional),
  • Easing plugin by George McGinley Smith
  • jQuery 1.31

Documentation

Here listed the entire variable you can use to customize RIEG. There are some example which should be easy enough to understand how to use this plugin.

1.0 Example

All you need is a HTML tag which should be placed on the bottom of your HTML file.

<div id="rawr">
<img id="gallery" src="images/start.jpg"/></div>
</div>

import all the dependency library,plugin and stylesheet

<script type="text/javascript" src="js/library.js"></script>
<script type="text/javascript" src="js/jquery.RIEG.js"></script>
<script type="text/javascript" src="js/easing.js"></script>

Finally, initialize RIEG. (click on plain text for better view)

$("#gallery").RIEG({
url: [
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/h/hanging-laundry-1203294-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/m/madidi-trees-646088-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/j/jellyfish-swarm-palau-131157-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/s/shark-kingman-reef-1166392-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/h/havasu-creek-waterall-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/p/polar-bears-svalbard-1205962-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/p/pictured-rocks-lakeshore-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/hibiscus-petals-758680-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/swirling-aurora-754878-sw.jpg",
"http://gallery.photo.net/photo/6922516-md.jpg",
"http://gallery.photo.net/photo/6823111-md.jpg",
"http://gallery.photo.net/photo/7360351-md.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/v/volcano-winter-521567-sw.jpg",
"http://www.studio7designs.com/_media/images/photography_portfolio_01.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/m/mangroves-bali-1047803-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/arctic-rainbow-nicklen-673279-sw.jpg",
"http://science.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Science/Images/Content/sedimentary-slope-a2wd32-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/green-landscape-haas-1093772-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/suruga-bay-doubilet-101053-sw.jpg",
"http://photo.net/photo/pcd0787/san-galgano-19.4.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/cherry-blossom-tree-monument-720322-sw.jpg",
]
});

This is the simplest way to initialize RIEG without adjusting the default setting. You can adjust the setting according such as this one which change the screen size and effect. (click on plain text for better view)

$("#gallery").RIEG({
url: [
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/h/hanging-laundry-1203294-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/m/madidi-trees-646088-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/j/jellyfish-swarm-palau-131157-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/s/shark-kingman-reef-1166392-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/h/havasu-creek-waterall-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/p/polar-bears-svalbard-1205962-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/p/pictured-rocks-lakeshore-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/hibiscus-petals-758680-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/swirling-aurora-754878-sw.jpg",
"http://gallery.photo.net/photo/6922516-md.jpg",
"http://gallery.photo.net/photo/6823111-md.jpg",
"http://gallery.photo.net/photo/7360351-md.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/v/volcano-winter-521567-sw.jpg",
"http://www.studio7designs.com/_media/images/photography_portfolio_01.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/m/mangroves-bali-1047803-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/arctic-rainbow-nicklen-673279-sw.jpg",
"http://science.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Science/Images/Content/sedimentary-slope-a2wd32-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/green-landscape-haas-1093772-sw.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/suruga-bay-doubilet-101053-sw.jpg",
"http://photo.net/photo/pcd0787/san-galgano-19.4.jpg",
"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/Content/cherry-blossom-tree-monument-720322-sw.jpg",
],
effect: "zoom",
inEasing:"linear",
outEasing: "linear",
duration: 500,
screenW: screen.width/2,
screenH: screen.height/2.2

});

2.0 Section Description

description file on each section

3.0 RIEG API

api in Excel SpreadSheet

Variable name Type Default Value Limitation Description
url array string none IMAGE PATH ONLY array of string used in RIEG
loadingURL string http://www.sofa-framework.org/design/loading_wh.gif IMAGE PATH ONLY loading image used in RIEG
flipWidth int 0 SCREEN WIDTH width length of flip effect
fadeIn decimal 1 1 fade in opacity amount
fadeOut decimal 0 1 fade out opacity amount
zoomW int 10 SCREEN WIDTH zoom width
zoomH int 10 SCREEN HEIGHT zoom height
effect string slideRight slideRight
slideLeft
slideUp
slideDown
custom
flip
zoomfade
This is case sensitive, only the following limitation are allow
inEasing string easeInBounce SECTION 3.0 This is case sensitive, only the following limitation are allow
outEasing string easeOutBounce SECTION 3.0 This is case sensitive, only the following limitation are allow
customLeft int 0 INTEGER ONLY css left
customTop int 0 INTEGER ONLY css left
customWidth int 0 INTEGER ONLY css width
customHeight int 0 INTEGER ONLY css height
duration int 1000 INTEGER ONLY duration of animation effect
screenH int 400 INTEGER ONLY image screen height, can use mathematical calculation
screenW int 600 INTEGER ONLY image screen width, can use mathematical calculation
thumbnailH int 30 INTEGER ONLY thumbnail height, can use mathematical calculation
thumbnailW int 30 INTEGER ONLY thumbnail width, can use mathematical calculation
rawrgallery string #rawr STRING ONLY css id value
galleryContainer string #rawr-gallery-container STRING ONLY css id value
navigation string #rawr-images STRING ONLY css id value
navRight string #rawr-right STRING ONLY css id value
navLeft string #rawr-left STRING ONLY css id value
mainNav string #rawr-nav STRING ONLY css id value
mainControl string #rawr-control STRING ONLY css id value
controlPrev string #rawr-prev STRING ONLY css id value
controlNext string #rawr-next STRING ONLY css id value
controlNav string #rawr-nav-visibility STRING ONLY css id value
controlSlideShow string #rawr-slideshow STRING ONLY css id value
controlPrevDesc string Previous STRING ONLY description fo previous icon
controlNextDesc string Next STRING ONLY description for next icon
controlNavDesc string Disable Navigation Bar STRING ONLY description for navigation icon
controlSlideShowDesc string Start/Stop Slideshow STRING ONLY description for slideshow icon
navEasing string linear SECTION 3.0 easing functionality of navigation bar
navSpeed int 1500 INTEGER ONLY speed of animation of the navigation bar
nav_border string #787878 solid 1px STRING ONLY CSS declaration for border
gborderSize int 10 INTEGER ONLY the frame size of the gallery
gborderColor string #F0F0F0 STRING ONLY the color of the gallery border, white in default
navLeftButton string images/left.png IMAGE PATH ONLY The navigation left arrow button
navRightButon string images/right.png IMAGE PATH ONLY The navigation right arrow button
nav_screenH int 50 INTEGER ONLY the navigation screen height, can use mathatical calculation
nav_screenW int 500 INTEGER ONLY the navigation screen width, can use mathatical calculation
elementHeight int 20 INTEGER ONLY controller icon height on the top right hand corner
elementWidth int 20 INTEGER ONLY controller icon width on the top right hand corner
controlWidth int 60 INTEGER ONLY controller container width
controlHeight int 20 INTEGER ONLY controller container height
controlSlideShowInPath string images/play.png IMAGE PATH ONLY controller icon play
controlSlideShowOutPath string images/stop.png IMAGE PATH ONLY controller icon stop
controlPrevPath string images/prev.png IMAGE PATH ONLY controller icon previous
controlNextPath string images/next.png IMAGE PATH ONLY controller icon next
navigationShow string visible visible/hidden to enable visibility of the navigation bar located on the bottom of RIEG
autoSlideShow boolean FALSE TRUE/FALSE the slideshow will not be on in default
slideShowSpeed int 4000 INTEGER ONLY the speed of the slide show
popOut boolean TRUE TRUE/FALSE whether to add on click function on the image to display the original size image
imageNext boolean FALSE TRUE/FALSE whether to add in the event on the image so that when clicked it will go to the next image

4.0 Easing capability

This is taken from George McGinley Smith easing plugin. It is case sensitive.

  • swing
  • linear
  • easeInQuad
  • easeOutQuad
  • easeInOutQuad
  • easeInCubic
  • easeOutCubic
  • easeInOutCubic
  • easeInQuart
  • easeOutQuart
  • easeInOutQuart
  • easeInQuint
  • easeOutQuint
  • easeInOutQuint
  • easeInSine
  • easeOutSine
  • easeInOutSine
  • easeInExpo
  • easeOutExpo
  • easeInOutExpo
  • easeInCirc
  • easeOutCirc
  • easeInOutCirc
  • easeInElastic
  • easeOutElastic
  • easeInOutElastic
  • easeInBack
  • easeOutBack
  • easeInOutBack
  • easeInBounce
  • easeOutBounce
  • easeInOutBounce

Support

  • Please post questions to the jQuery discussion list, putting [validate] into the subject of your post, making it easier to spot it and respond quickly. Keep your question short and succinct and provide code when possible; a test page makes it much more likely that you get an useful answer in no time. Alternatively, you can also post your question on the update post or email me at [email protected].
  • Please post bug reports and other contributions (enhancements, features, etc.) to [email protected] and i will try to get back to you asap. You can also try post it on jQuery plugin page.

Donation

[donate]

Update

  • Updated test site to include fade effect.

File

Tutorial: How to stop caching with jQuery and javascript

There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.

jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,

$.ajax({
url: 'test.html',
cache: false,
success: function(html){
$('#results').append(html);
}
});

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,

$.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.

Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,

var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
return $(img).load(function()
{
alert('completed!');
});

This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.

HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

This will prevent the whole page from being cached by the browser as well.

Tutorial: How to Create your own jQuery Flip effect plugin

It is always annoying to look at codes that are created in a plugin or website  that perform millions of complicated actions without comments to find the one effects contain in the plugin.

In this tutorial, i will demonstrate an easy to create  jQuery flip effect with minimum amount of complication so that you will be able to inherit this effect into your plugin.

Before we start, i believe the below resources will be useful to create your own simple flip effect program plugin

Once you read the above tutorial, you should have an idea how jQuery flip actual work. The key point to flip is speed. And in order to create a jQuery flip effect plugin, we will apply the same concept on all other types of effect that is being used in other tutorial. Of course, the demo guy will be the ghost again. Opps. You can view the demo page below,

demo-rawrflip

Explanation

In order for the flip to work. There must be a swap between two images or containers. If there is only one element, we don't really need the flip effect on it.  The code for the demo above can be seen via view source or the one that i'm going to show below,

$(function(){

var alt = 0;
$('#flipMe').click(function(){
alt%2 == 0?
$(this)
.css('background-color', '#000')
.animate({height:50+'px', width: 0+'px', marginLeft: 25+'px'},140)
.css('background-color', '#555')
.animate({height:50+'px', width: 50+'px', marginLeft: 0+'px'},240):
$(this)
.css('background-color', '#555')
.animate({height:50+'px', width: 0+'px', marginLeft: 25+'px'},140)
.css('background-color', '#000')
.animate({height:50+'px', width: 50+'px', marginLeft: 0+'px'},240);
alt++;
})
});

i attached an event click to the image tag so that whenever user click on the image it will flip. There is a bit of maths in here so that we can determine user click by using modulus. The important part for flipping to occurs is shown below,

.animate({height:50+'px', width: 0+'px', marginLeft: 25+'px'},140)

whereby it compress the image at the center and flip it out from the center again which creates a flip effect when its quick enough. If the speed of the animate function is set to a larger value such as 2000, you will notice the flip effect became a zoom effect instead. Throw this into a plugin code and you get yourself a very simple jQuery flip effect plugin!

Tutorial: jQuery effect – how to shrink/zoom at the center of a container

jQuery provides a basic effect of sliding in and out. However, in other to create other form of effect we will have to apply animate function provided by jQuery. This tutorial will demonstrate of using animate function in jQuery to perform a shrink function at the center of an element  that you often see in Gallery and other types of plugin.

It is not difficult to create a method to demonstrate a shrink in and out effect with animate function. The concept used here is to opposite the natural direction of the sliding jQuery provides in order to zoom in the image shows below.

image to be zoom
image to be zoom

Yes! Its RAWR! cute ghost, i was lazy to find a proper picture so i just grab him down to play with.  Now, in order to shrink him properly, we will have to use the plugin i created just for him. I will also explain what does this simple plugin does. You can have a look at the demo below,

demo-rawrzoom

PS: There is another method which is  a bit more complex than this one which you can see from here

So, how does the plugin work? Simple! it's just pure animate function from jquery with css. Illustrator the code below.

$(selector).stop(true,false)
.animate({
height: options.zoomSize+'px',
width: options.zoomSize+'px',
marginTop: ((oriH-options.zoomSize)/2)+'px',
marginLeft: ((oriW-options.zoomSize)/2)+'px'
}, options.duration)

What it does here is to shrink it to the size you define and move opposite accordingly. Let's assume height: and width: move upwards and right respectively while it shrink. So marginTop: and marginLeft: will move downwards and left respectively so that our eyes will illustrate the shrink on the center of the box! hopes this help!

Usage

An example on how to use this plugin from the demo site. The parameter is optional.

$('#zoomMe').zoom(
{
zoomWidth: 10,
zoomHeight: 10,
duration: 3000
}
);

or

$('#zoomMe').zoom();

Parameter

The optional parameter for this plugin is as follow,

  • zoomwidth: the width you wish it to be minimize default its 10
  • zoomheight: the height you wish it to be minimize defaults its 10
  • duration: time for zoom to be complete defaults its 5000

You can download the plugin from