1. 程式人生 > >JQuery實現背景圖片漸變!

JQuery實現背景圖片漸變!

side start process log key 圖片 ant -type port

    1. <html>
    2. <head>
    3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    4. <title>JQuery實現背景圖片漸變</title>
    5. <script type="text/javascript" src="jquery.js" mce_src="jquery.js"></script>
    6. <script type="text/javascript">
    7. $(document).ready(function(){
    8. $("#sub").css("left",$("#super").offset().left);
    9. $("#sub").css("top", $("#super").offset().top);
    10. $("#super").css("background-image","url(img_01.jpg)");
    11. $("#sub").css("background-image","url(img_02.jpg)");
    12. $(‘#sub‘).css(‘opacity‘, 0);
    13. $("#sub").hover(
    14. function() {
    15. $("#super").stop().animate({opacity: ‘0‘},500);
    16. $("#sub").stop().animate({opacity: ‘1‘},500);
    17. },
    18. function() {
    19. $("#sub").stop().animate({opacity: ‘0‘},500);
    20. $("#super").stop().animate({opacity: ‘1‘},500);
    21. });
    22. }
    23. );
    24. </script>
    25. <style>
    26. #super{
    27. width:900px;
    28. height:400px;
    29. }
    30. #sub {
    31. width:900px;
    32. height:400px;
    33. position:absolute;
    34. }
    35. </style>
    36. </head>
    37. <body>
    38. <div id="super"></div><div id="sub"></div>
    39. </body>
    40. </html>
    41. 簡單註釋:

      背景漸變切換,首先要準備兩個div標簽,主要,這兩個標簽一個是主要div,一個用來掩蓋原來背景的div,因此第二個div的position屬性應該設置為:absolute;

      在Jquery代碼裏面,

      1.設定第二個div與第一個div重合;

      2.分別給兩個div賦上背景圖片,並設第二個div的透明屬性為0,即:完全透明;

      3.在第二個div加上hover函數,分別對兩個div進行漸變操作;

JQuery實現背景圖片漸變!