1. 程式人生 > >jquery 滾動到指定位置觸發動畫

jquery 滾動到指定位置觸發動畫

jQuery 滾動到指定位置觸發動畫

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>sas</title>
  6. <scripttype="text/javascript"src="js/jquery.js"></script>
  7. <styletype="text/css">
  8. *{ margin:0px; padding:0px;}  
  9. .gs{  
  10. position:relative;  
  11. top:800px;   
  12. background-color:#099;   
  13. left:150px; width:80px;   
  14. }  
  15. .xz{  
  16. animation:roate 2s;  
  17. animation-fill-mode:forwards;  
  18. animation-direction:alternate;  
  19.     }  
  20. @keyframes roate{  
  21.     from { transform:rotate(0deg);  
  22.     width:100px;  
  23.     height:100px; }  
  24.     to{transform:rotate(360deg);  
  25.     width:200px;  
  26.     height:200px;  
  27.     }}  
  28. .xs{ width:50px; float: left; height:30px; background-color: #F90; position:fixed; left:700px; top:0px;}  
  29. </style>
  30. </head>
  31. <body>
  32. <scripttype="text/javascript">
  33. function gdjz(div,cssname,offset){  
  34.     var a,b,c,d;  
  35.     d=$(div).offset().top;  
  36.     a=eval(d + offset);  
  37.     b=$(window).scrollTop();   
  38.     c=$(window).height();  
  39.     if(b+c>a){  
  40.         $((div)).addClass((cssname));  
  41.         }  
  42.     }  
  43. $(document).ready(function(e) {  
  44. $(window).scroll(function(){  
  45.     gdjz("#dh",'xz',100);  
  46.     }  
  47. /*var a,b,c;  
  48. a=$("#dh").offset().top;//元素相對於視窗的距離  
  49. b=$(window).scrollTop(); //監控視窗已滾動的距離;  
  50. c=$(document).height();//整個文件的高度  
  51. d=$(window).height();//瀏覽器視窗的高度*/  
  52. /*if(d+b>a+100){  
  53.     $("#dh").addClass("xz");  
  54.     }  
  55. */  
  56.     );  
  57. });  
  58. </script>
  59. <divstyle="height:1800px; background-color:#999; width:500px; float:left;">
  60. <divid="dh"class="gs">觸發動畫</div>
  61. </div>
  62. <divclass="xs"></div>
  63. </body>
  64. </html>