JQuery Unit-3
JQuery Unit-3
Unit-3
3.1 Showing/Hiding Elements
• This effect allows you to show or hide an HTML element.
• .show(): Displays the hidden element.
• .hide(): Hides the element.
Example
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="box" style="width:100px; height:100px; background:orange;"></div>
<button id="show">Show</button>
<button id="hide">Hide</button>
<script>
$(document).ready(function () {
$("#show").click(function () { $("#box").show(); });
$("#hide").click(function () { $("#box").hide(); });
});
</script>
</body>
</html>
3.2 Sliding Elements
• In jQuery, you can create custom animations using the animate() method. This method
allows you to define multiple CSS properties and their target values, as well as control the
duration and easing of the animation.
• Syntax of animate()
$(selector).animate(properties, duration, easing, complete);
• properties: An object specifying the CSS properties to animate and their target values.
• duration: The speed of the animation ("slow", "fast", or a value in milliseconds like 1000).
• easing (optional): Specifies the easing function for the animation (e.g., "swing" or "linear").
• complete (optional): A callback function to execute after the animation is complete.
Example
<html lang="en"> <script>
<head> $(document).ready(function() {
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> $("#animateBtn").click(function() {
<style> $("#box").animate(
#box { {
width: 100px; width: "200px", // Increase width to 200px
height: 100px; height: "200px", // Increase height to 200px
background-color: lightblue; left: "100px", // Move 100px to the right
position: relative; opacity: 0.5 // Set opacity to 50%
margin: 20px; },
} 1000, // Duration: 1 second
button { "swing", // Easing function: swing
margin: 10px; function() { // Callback function
padding: 10px 20px; alert("Animation Complete!");
font-size: 16px; }
} );
</style> });
</head> });
<body> </script>
<button id="animateBtn">Animate</button> </body>
<div id="box"></div> </html>
Working with events.
• Events in jQuery are actions or occurrences that happen in the
browser, such as clicks, mouse movements, keyboard inputs, form
submissions, and more.
• jQuery simplifies working with events by providing intuitive methods
to handle them.
Commonly Used Event Methods in jQuery
Mouse Events:
• click(): Triggered when an element is clicked.
• dblclick(): Triggered when an element is double-clicked.
• mouseenter() and mouseleave(): Triggered when the mouse enters or
leaves an element.
• hover(): A shorthand for handling both mouseenter and mouseleave.
Keyboard Events:
• keypress(): Triggered when a key is pressed.
• keydown(): Triggered when a key is pressed down.
• keyup(): Triggered when a key is released.
Form Events:
• submit(): Triggered when a form is submitted.
• change(): Triggered when the value of an input, select, or textarea
changes.
• focus() and blur(): Triggered when an input gains or loses focus.
Document/Window Events:
• ready(): Triggered when the DOM is fully loaded.
• resize(): Triggered when the window is resized.
• scroll(): Triggered when the user scrolls.