Jquery implements fade-in and fade-out of background images

Source: Internet
Author: User

The reason why the background image fades in and out: two different images overlap with different layers, and the top layer is temporarily transparent. When the mouse moves up, the top layer changes from transparent to opaque, move the mouse away and vice versa.

Write the following code

Html structure:

The Code is as follows: Copy code

<A id = "logo" href = ""> <span> webpage tutorial </span> </a>


Css code:

The Code is as follows: Copy code

# Logo {
Margin: 0 auto;
Position: relative;/* relative positioning, for the absolute positioning of the following hover */
Background: url(fatkun.png) left top no-repeat;/* set the background image */
Width: 150px;
Height: 40px;/* Note the height here */
Display: block;
Text-indent:-9999px;
}
# Logo. hover {/* prepare for JQ */
Background: url(fatkun.png) left bottom no-repeat;/* the background-position is different from the above */
Position: absolute;/* to overlap two images */
Top: 0;
Left: 0;
Height: 40px;
Width: 150px;
}

Finally, the most important JQuery code appears.

The Code is as follows: Copy code

<Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<Script type = "text/javascript">
$ ("# Logo"). append ("<span class = 'hover '> </span>"); // Add a label to overlap with the gray image.
$ (". Hover" 2.16.css ('opacity ', 0); // Do Not Display
$ (". Hover"). parent (). hover (
Function (){
$ (". Hover"). stop (). animate ({opacity: '1'}, 1000 );
},
Function (){
$ (". Hover"). stop (). animate ({opacity: '0'}, 1000 );
});
</Script>

Complete instance

The Code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Jquery background image gradually displayed </title>
<Style type = "text/css">
Body {
Padding-top: 100px;
Text-align: center;
}
# Logo {
Margin: 0 auto;
Position: relative;/* relative positioning, for the absolute positioning of the following hover */
Background: url(fatkun.png) left top no-repeat;/* set the background image */
Width: 150px;
Height: 40px;/* Note the height here */
Display: block;
Text-indent:-9999px;
}
# Logo. hover {/* prepare for JQ */
Background: url(fatkun.png) left bottom no-repeat;/* the background-position is different from the above */
Position: absolute;/* to overlap two images */
Top: 0;
Left: 0;
Height: 40px;
Width: 150px;
}
</Style>
</Head>

<Body>
<A id = "logo" href = "http: // bKjia. c0m"> <span> bKjia. c0m </span> </a>
BKjia. c0m
<Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<Script type = "text/javascript">
$ ("# Logo"). append ("<span class = 'hover '> </span>"); // Add a label to overlap with the gray image.
$ (". Hover" 2.16.css ('opacity ', 0); // Do Not Display
$ (". Hover"). parent (). hover (
Function (){
$ (". Hover"). stop (). animate ({opacity: '1'}, 1000 );
},
Function (){
$ (". Hover"). stop (). animate ({opacity: '0'}, 1000 );
});
</Script>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.