The method value “toggle” tells the jQuery animation to remember the absolute height of the element so when the next click event happens it knows the original height and returns it back to its normal state.
$(document).ready(function(){
// hide all coaster lists
$( '.park ul' ).hide();
// animate the height of the ul with ease effect
$('.park h3').click(function() {
$(this).next().animate(
{'height':'toggle'}, 'normal', 'easeOutBounce'
);
});
// change cursor to hand when hovered over
$('.park h3').hover(function() {
$(this).css('cursor', 'pointer');
}, function() {
$(this).css('cursor', 'auto');
});
});
Basically, I want to make the text within the h3 centered when it has been clicked and go back to normal when clicked again.
I tried adding a callback function to .animate to add a class called text-center but not sure what the selector should be?
If I use $(‘.park h3’) then that would make ALL the h3’s move to the center when any were clicked and not just the h3 that was clicked.
If I use $(this), then what is “this” referring to?.
Once I have the text of the h3 centering as needed then how would I alter that to animate it centering?
$COLOR=#66cc66 [/COLOR]refers to the current scope within the click method meaning the current jQuery object currently clicked is now referenced to this. Try the following
// animate the height of the ul with ease effect
$('.park h3').click(function() {
$(this)
.css('text-align', ($(this).css('text-align') == 'center' ? 'left' : 'center'));
.next()
.animate({
height: 'toggle'
}, 'normal', 'easeOutBounce');
});
// When someone clicks on the h3 within the "park" class element
$('.park h3').click(function() {
$(this) // do some things relating to that clicked element, which are
// toggle the alignment of its text between center and left
.css('text-align', ($(this).css('text-align') == 'center' ? 'left' : 'center'));
// go to the element immediately after the clicked element
.next()
// animate that element
.animate({
// toggle the height between 0 and its known height
height: 'toggle'
// normal duration, and use a bounce effect at the end of the animation
}, 'normal', 'easeOutBounce');
});
Looking back at the code, the comments really aren’t all that necessary.
Indented javascript in the long run is a better way to go even though it uses more lines of the file it makes it that much easier to for someone else to understand it. A couple of months ago i never coded like this and now i do as the structure is just so much easier to go through rather then everything been on one line and all packed together.
$(document).ready(function(){
// hide all coaster lists
$('.park ul').hide();
// animate the height of the ul with ease effect | move h3 to middle when open | change cursor to hand when hovered
$('.park h3').click(function() {
$(this)
.css('text-align', ($(this).css('text-align') == 'center' ? 'left' : 'center'))
.hover(function() {
$(this).css('cursor', 'pointer');
}, function() {
$(this).css('cursor', 'auto');
})
.next()
.animate({
height: 'toggle'
}, 'slow', 'easeOutBounce');
});
//Move list to left when coaster name is clicked and display photo
$().click(function() {
$('.park li').addClass('toList');
});
});