Thursday, April 22, 2010

Understanding and Building The Script

the first thing we need to do is write the document ready function. As you may or may not already know if you want an event to happen then it should be within a document ready function. This is stated and explained on the official jQuery site.

The document ready function reads like this.

$(document).ready(function(){}

Personally I like to place the French braces on separate lines with the function details inside like this:
$(document).ready(function()
{
properties
methods
events
})

Now we will set the properties for the elements using selectors and classes. We will be animating with opacity so we will start of declaring the value of the opacity property for each element using CSS classes and selectors.

$(".class1").css("opacity","0.7");
$("#box1").css("opacity","0.7");
The class one in our example includes a menu link and a corresponding span text. The box is a selector containing 3 images. The original value of the these elements will be default as .07 of a whole value (which is 1).

Our script should look like this:

$(document).ready(function()
{
$(".class1").css("opacity","0.7");
$("#box1").css("opacity","0.7");
}
)

now that we have the initial properties set we will create a function called by the mouse event "mouseenter" inside of the current document ready function. Start the function like this:

$(".class1").mouseenter(function()
{
}
)
this denotes that when mouseenter occurs on a .class1 element call function. Our browser is handling the event with the document ready function, so no event listeners are required. Though there are other jQuery mouse events in the library I find the mouseenter and mouseout are most effective for this use of animate.

So lets declare methods in this function. We will be using the animate method here.

$("#box1").animate({opacity: "1.0", }, 600)

The elements in the box1 selector will be affected by the animate function, and what will be animated is the opacity value to the value of "1", which is gaining value from it's previous value of .07, this will occur with the animate method. We set the time of milliseconds in which the selector will animate, this is 600 milliseconds. The jQuery UI knows what this property will be, and should be placed at the end of the method.

Lets also animate all items in the class1 when we do this hover over a class1 item. If i had more time for this article i would create a separate class for the element that calls the function, then those being affected by it, but unfortunately my time is limited here and I will be occupied for quite awhile after this. I could have compiled the images in box 1 into a class 2 with the span text, but i wanted to illustrate that we can also affect selectors.

Here we animate all items in class 1.

$(".class1").animate({opacity: "1.0", }, 600)

Now both the selector box 1 and all items in class 1 will gain animate opacity to the value of 1 over 600 milliseconds.

We will also create a mouseout function, but first we might want to call the stop method in our current function.
We can rely on the mouseenter and mouseout for our functionality, but a stop method ensures a clean end to the function and this is particularly relevant when we are using the animate method. There are some problems that can occur is you are using animate but are not including the stop method, such as cued events and animations.. which is a consideration in our full example here. Documentation on the stop method is made available at the jQuery site.


.stop().animate({opacity: 1.0}, 1)

this will stop all animations within the function in one millisecond, and we will also include these boolean conditions [ clearQueue ], [ jumpToEnd ] , they should be placed after the opacity property in the animate method. We don't have to state what the booleans are though, as they will be recognized in their position by the jQuery ui. We will be setting them as true, so the method will read as this.

stop().animate({opacity: 1.0},true, true, 1)

Documentation on the Clear que and jumpToEnd conditions can be found here at the official jQuery site. The basic use of these booleans right now is to end the animations appropriately and avoid a barrage of queued animations. Animation will que if you hover in and our of the class very fast and consecutively, or hover between two items with the animate method.

This is how our animate function for mouseenter should look.

$(document).ready(function()
{
$(".class1").css("opacity","0.7");
$("#box1").css("opacity","0.7");

$(".class1").mouseenter(function()
{
$("#box1").animate({opacity: "1.0", }, 600)
$(".class1").animate({opacity: "1.0", }, 600)
stop().animate({opacity: 1.0},true, true, 1)
}
);
}
);
Mouse out will read the same and will also be instantiated in the document ready function and will read much the same as the mouseenter. We will just change the method from mouseenter to mouseout, and we will be changing back the opacity values.
It should read as this.

$(".class1").mouseout(function()
{
$("#box1").animate({opacity: "0.7", }, 600)
$(".class1").animate({opacity: "0.7", }, 600)
stop().animate({opacity: 0.7}, true, true, 1)
});


and the document ready function as a whole

// JavaScript Document
//custom Jquery feature by Cesar


$(document).ready(function(){
$(".class1").css("opacity","0.7");
$("#box1").css("opacity","0.7");



$(".class1").mouseenter(function(){
$("#box1").animate({opacity: "1.0", }, 600)
$(".class1").animate({opacity: "1.0", }, 600)
stop().animate({opacity: 1.0},true, true, 1)
});

$(".class1").mouseout(function(){
$("#box1").animate({opacity: "0.7", }, 600)
$(".class1").animate({opacity: "0.7", }, 600)
stop().animate({opacity: 0.7}, true, true, 1)
});
});
And for a second set of this function, to affect other elements with this effect we will just change selectors and classes. We will need to create a new document ready function for it as well. I don't think you'll need to see that, but i'll show the script as a whole.

// JavaScript Document
//custom Jquery by Cesar


$(document).ready(function(){
$(".class1").css("opacity","0.7");
$("#box1").css("opacity","0.7");



$(".class1").mouseenter(function(){
$("#box1").animate({opacity: "1.0", }, 600)
$(".class1").animate({opacity: "1.0", }, 600)
stop().animate({opacity: 1.0},true, true, 1)
});

$(".class1").mouseout(function(){
$("#box1").animate({opacity: "0.7", }, 600)
$(".class1").animate({opacity: "0.7", }, 600)
stop().animate({opacity: 0.7}, true, true, 1)
});
});
$(document).ready(function(){
$(".class2").css("opacity","0.7");
$("#box2").css("opacity","0.7");



$(".class2").mouseenter(function(){
$("#box2").animate({opacity: "1.0", }, 600)
$(".class2").animate({opacity: "1.0", }, 600)
stop().animate({opacity: 1.0},true, true, 1)
});

$(".class2").mouseout(function(){
$("#box2").animate({opacity: "0.7", }, 600)
$(".class2").animate({opacity: "0.7", }, 600)
stop().animate({opacity: 0.7}, true, true, 1)
});
});



Setting up the jQ

Now is the time we should throw a link to the jQuery library into the html document head. You can download the latest jquery library from the jQuery site and ftp that.

Or you can link to it externally. I'm linking to it hosted at googlecode. Here is what it should look like in your document head.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQ Example</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<!-- JavaScript - linking to Google -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
</head>
<body>

The next thing we should do is create a JavaScript file and link to it within the html head like this.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQ Example</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<!-- JavaScript - linking to Google -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<!-- JavaScript - the script -->
<script type="text/javascript" src="js/castandcrew.js"></script>
</head>
<body>

In the next post we will look at building the functionality in the new js file

Formatting a Menu and corresponding elements

Now that we have the elements we are going to be using for this article laid out. Lets add formatting for the example. Although unnecessary for functionality This style sheet we will provide a menu and gallery like setup which will better illustrate some of what can be done.

This is my stylesheet.

@charset "utf-8";
/* CSS Document */
body{
background-color:#000;
}

#wrapper
{
width:550px;
height:300px;
margin-top:10px;
padding: 0px;
padding-bottom:10px;
padding-left:10px;
padding-right:10px;
padding-top:10px;
border: 2px solid #363636;
}

#menu
{
display:block;
text-decoration:none;
float:right;
margin-bottom:0px;
margin-top:10px;
height:290px;
border: 1px solid #363636;
width:220px;
}
#menu li
{
list-style:none;
margin-left:20px;
margin-right:20px;
padding-bottom:1.5px;
margin-top:5px;
font-family:Arial, Helvetica, sans-serif;
text-align:center;
}
#menu a {
display: block;
font-size:20px;
color:#000;
background-color:#C00;
margin-top:2px;
}

#content
{
width: 320px;
height:290px;
border: 1px solid #363636;
margin-top:10px;
margin-bottom:10px;
}

#box1
{
position: relative;
height:140px;
width:306;
float:left;
padding-top:5px;
}
#box2
{
position: relative;
height:140px;
width:306;
padding-top:5px;
}

a
{
height:auto;
font-family:"Arial", Helvetica, sans-serif;
text-decoration:none;
color:#C00;
}


In the next post we will get down to using jQuery.

Getting Started

Getting Started
To begin, lets create a menu with which we to call the function, and some corresponding elements. We will use selectors and classes for this example.

I will try to keep code clean for this article, and this is most likely not the most elegant way to use chain events with animate, but this is what I did to achieve this.

The markup I will be using will look like this:

<body>

<div id="wrapper">
<div id="menu">
<li>
<a class="run" href="1" title=""&gtClass 1</a>
<a class="run2" href="2" title=""&gtClass 2</a>
</li>
</div>
<div id="images">

<div id="box1">
<a href="1">
<img src="../../assets/castandcrew/img/01.jpg" alt="aetuts" border="0" class="latest_img" />
<img src="../../assets/castandcrew/img/02.jpg" alt="aetuts" border="0" class="latest_img" />
<img src="../../assets/castandcrew/img/03.jpg" alt="aetuts" border="0" class="latest_img" />
<span class="class1span"&gt3d models of <em&gtspan text 1</em></span>

</div>
<div id="box2">
<a href="2">
<img src="../../assets/castandcrew/img/01.jpg" alt="aetuts" border="0" class="latest_img" />
<img src="../../assets/castandcrew/img/02.jpg" alt="aetuts" border="0" class="latest_img" />
<img src="../../assets/castandcrew/img/03.jpg" alt="aetuts" border="0" class="latest_img" />
<span class="class2span"&gt3d models of <em&gtspan text 2</em></span>
</div>
</div>
</div>
</body>
</html>

Note: I've replaced the closing tag > in the markup with & gts so that the code can be posted here. You will want to run a find and replace if you are using the markup. In the next post I will provide a simple style sheet formatting the markup.

Using Jquery To Animate classes with chain events

Animating elements is a convention often used to add interactivity to a site. You implement animate so that as a user hovers over an image in a web site the image grows in dimension indicating functionality, or you might want to add flare animating the color property of a hypertext when it's been clicked.

But what if you want multiple items to animate when a user hovers over one element?

This article is intended to describe a basic, yet powerful, chain technique which will allow an element to trigger the .animate method to affect multiple items on a page.

The finished product.