Chapter-7 (Adding Visual Effects To Web Pages)
Chapter-7 (Adding Visual Effects To Web Pages)
-----------------------------------------------------------
jQuery:
---------
2. The purpose of jQuery is to make it much easier to use JavaScript on your website.
3. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish,
and wraps them into methods that you can call with a single line of code.
--------------------------------------------------------------------------------
------------------------------------------
There are several ways to start using jQuery on your web site.
<head>
<script src="jquery-3.7.1.min.js"></script>
</head>
--------------------------------------------------------------------------------
------------------------------
The execution of jQuery code should occur only after the Web page has been fully loaded.
To ensure that a Web page is fully loaded before the jQuery code is executed on it,
jQuery provides the document.ready() function.
This function contains the jQuery code to be executed on HTML elements after the
<SCRIPT >
$(document).ready(function(){
// jquery code...
});
</SCRIPT>
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Ex.1
----
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
</body>
</html>
-----------
Ex.2
-------
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("p").hide();
});
$("#b2").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
-----------------------
----------------------------
jQuery uses selectors to select and manipulate an HTML element or a group of elements. You can
select an HTML element by:
-------------------------
Ex.
----
$("h1")
In the preceding code snippet, the <H1> elements are referred to by the name, h1.
Ex.
----
To select an element with a specific ID by using jQuery, you can use the following syntax:
$("#Element_ID")
Ex.
----
To select elements with a specific class, you can use the following syntax:
$(".Element_class")
Ex.
----
$("[src]")
In the preceding code snippet, all the elements with the src attribute are selected.
Using attribute name:
--------------------------
The HTML elements can also be selected by using the name of an attribute.
Syntax:
---------
$("[attribute-name]")
-------------------------
jQuery provides CSS selectors to modify the CSS properties of an HTML element or document.
Syntax:
---------
eg.
$("div").css("color","green");
---------------------------------------------------------------
$("div").css("color":"green", "font-size":"200%");
----------------------------------------------------------------
Ex.
----
the following code for modifying the inner content of HTML elements:
<!DOCTYPE HTML><HTML>
<HEAD>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#para").prepend(" <B>jQuery </B>");
$("#newhead").remove();
});
});
</SCRIPT>
</HEAD>
<BODY>
<DIV align="center">
</DIV>
</BODY>
</HTML>
--------------
**In the preceding code, when a user clicks the Click here button, the content, jQuery, is appended
before the <P> tag having the ID, para, by using the prepend() function.
Similarly, the content, append() function, is appended after the same <P> tag by using the append()
function.
In addition, a new <H3> tag is inserted after the <P> tag having the ID, para, by using the after()
function.
Finally, the heading having the ID, newhead, is removed by using the remove() function.
----------------------------------------------------------------------------------------------------------
------------------------------
<!DOCTYPE HTML><HTML>
<HEAD>
<SCRIPT type="text/javascript" src="jquery.js"></SCRIPT>
<SCRIPT type="text/javascript">
$(document).ready(function(){
});
});
</SCRIPT>
</HEAD>
<BODY>
<HR>
</BODY>
</HTML>
---------------------------
-------------------------------------------
With the help of jQuery, amazing visual effects can be applied to the elements of a Web document.
Some of the predefined jQuery effects that can be used to add visual appeal to a Web page are:
1. Hide
2. Show
3. Toggle
4. Slide
5. Fade
6. Animate
----------------------------
Ex.1:(hide)
-----
<!DOCTYPE HTML><HTML>
<HEAD>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
});
});
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
-------------------------------------
Ex.2:(show)
-----
<!DOCTYPE HTML><HTML><HEAD>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$(".view").click(function(){
$("h3").show(2000);
});
$(".conceal").click(function(){
$("h3").hide();
});
});
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
--------------
Ex.3:(Toggle)
-----
The toggle () function can be used to switch between the show and hide effects of an element.
This event can be used to hide or show the element, alternatively, when an event occurs.
<!DOCTYPE HTML><HTML>
<HEAD>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$(".view").click(function(){
$("h3").toggle('fast');
});
});
</SCRIPT>
</HEAD>
<BODY>
<HR>
</BODY>
</HTML>
------------------
Ex.4:(Slide)
-----
<!DOCTYPE HTML><HTML>
<HEAD>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$(".flipbut").click(function(){
$(".thought").slideToggle();
});
});
</SCRIPT>
<STYLE type="text/css">
margin:0px;
padding:5px;
text-align:center;
background:beige;
border:solid 1px;
div.thought
height:120px;
display:none;
</STYLE>
</HEAD>
<BODY>
<BR><BR>
</DIV>
</BODY>
</HTML>----------------
***In the preceding code, when the Show/Hide the thought of the day button is clicked,
slide down on the <DIV> element along with the thought class.
------------------------------------------------------------------------------
Ex.5. (Fade)
--------------
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
</head>
<body>
<br>
<br>
</body>
</html>
------------------------------------------------------------------------
Ex.6. Animate:
-----------------
<!DOCTYPE HTML>
<HTML>
<HEAD>
<SCRIPT src="jquery.js">
</SCRIPT>
<SCRIPT>
$(document).ready(function(){
$("div").mouseenter(function(){
$("div").animate({
center:'250px',
opacity:'0.5',
height:'250px',
width:'250px'
});
});
$("div").mouseleave(function(){
$("div").animate({
opacity:'1',
height:'100px',
width:'100px'
});
});
});
</SCRIPT>
</HEAD>
<BODY>
<DIV style="background:#98bf21;height:100px;width:100px;position:absolute;">
</DIV>
</BODY>
</HTML>