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> |