Some basic jQuery examples I created over time to help me learn jQuery. Hopefully you find this helpful as well! I appologize as some of these examples are a work in progress.
Sliding Panel
Used .click method to capture the event and then the .slideToggle method to slide the div into view matching the height of the element. CSS used to hide the div id panel until button event (click) occurs.
.slideToggle( [ duration ], [ callback ] ) - Allows for a callback function that calls once the animation is complete
Accordian
With the click method attached to the H3 Title tags of each section we use the .slideToggle method to display the content. The .eq method is used to specify a specific element within a set of matched elements. In thise case we chose .eq(2). At the same time the .siblings() method is used to get the siblings or the p element and apply the .slideup method which hides the matching elements with a sliding motion.
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Animate Hide method
We use the .click method to fire when the link is clicked. We then use the .parents method which grabs the ancestor/div of the a element. We then use the .animate method to hide the div slowly with an opacity change as it gets hidden. return false at the end to prevent the default link action.
Chain Gang
We are chaining multiple methods here. Watch speedy zip around the screen.
Event Click Handler: (Hides then shows again, threw on a blue border for fun. Also displays url of link clicked) Example Link
$(".link-notify").click(function(event){
alert("You just clicked " + this.href);
event.preventDefault();
.hide("slow").show("slow");
$(this).hide("slow").show("slow").addClass("setBold");
});
The overlay has essentially taken the size of the div.
$("#overlay-test").click( function() {
var position = $(".basic-info").offset(); // Grabs the position
alert("overlay fired");
$("#populate-overlay").css( { position: "absolute", left: position.left, top: position.top } ).show();
} );
$(".close-x").click(function () {
$(this).parent().hide();
});
$(".cancel-overlay").click(function () {
$(this).parent().hide();
});
Highlight and paragraph change
This is an example paragraph one.
This is an example paragraph two.
This is an example paragraph three.
This is an example paragraph four.
$("p.highlight").click(function () {
$(this).toggleClass("highlight").html("I'm am now a funky light switch now! Click me on and off.");
});
$(".paragraph-example").click(function() {
var str = $(".paragraph-example p:first").text();
$(".paragraph-example p:last").html(str).addClass("highlight");
});
.Each Method
Description: Iterate over a jQuery object, executing a function for each matched element. It's designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element. (Source api.jquery.com)
AJAX - Communication between client and server
The server can dynamically shape data based on input from the user/browser/client. Using the .click method the user selects from the list of links. Using the .load method, data (test.php) is loaded from the server and returned into the matched element (id - ajax-list1). We use the .text method to get the contents of the element (in this case the a link).