Js实现动态的仿百度“元宵节汤圆”特效

 3877

模仿百度首页“元宵节汤圆”动图:(主要运用了js的定时任务:setInterval)

原理:需要一张切图,通过不断定位使得图片就像一帧一帧的图片在播放从而形成了动画

效果图:


Js实现动态的仿百度“元宵节汤圆”特效


切图地址:

  1. https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png


页面代码:

  1. <!DOCTYPE html>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  3. <%
  4.     String ctxPath = request.getContextPath();
  5.     request.setAttribute("ctxpath", ctxPath);//项目根路径
  6. %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <script src="${ctxpath}/js/jquery-1.11.3.min.js"></script>
  11. <%-- <link href="${ctxpath}/css/main.css" rel="stylesheet"> --%>
  12.    <title>首页</title>
  13. <style type="text/css">
  14.  
  15. </style>
  16. <script type="text/javascript"> 
  17. $(function(){ //6210
  18.     // 0 270 540 810 1080 1890 2700 2970 3510 3780 4050 4320 5130 5940 6480 7290
  19.     // 定时任务,每隔100ms执行一次函数
  20.     setInterval("tangyuan()",100);
  21.     setInterval("tangyuan1()",100);
  22.     //setInterval("indexChange()",1);
  23. });
  24.  
  25. //汤圆 正着挖
  26. var count = 0;
  27. function tangyuan(){
  28.     count = count - 270;
  29.     if(count == -8370){
  30.         count = 0;
  31.     }
  32.     var ctxpath = '${ctxpath}';
  33.     //url("https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png")
  34.     //https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png
  35.     $("#tangyuan").css("background","url("+ctxpath+"/img/baidu/baidu_tangyuan.png) "+count+"px"+" 0px no-repeat"); 
  36. }
  37.  
  38. //汤圆 倒着挖
  39. var count1 = -8370;
  40. function tangyuan1(){
  41.     count1 = count1 + 270;
  42.     if(count1 == 0){
  43.         count1 = -8370;
  44.     }
  45.     var ctxpath = '${ctxpath}';
  46.     //url("https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png")
  47.     //https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png
  48.     $("#tangyuan1").css("background","url("+ctxpath+"/img/baidu/baidu_tangyuan.png) "+count1+"px"+" 0px no-repeat"); 
  49. }
  50. </script>
  51. </head>
  52. <body>
  53.     <span style="position: absolute;left: 30%">汤圆我要正着挖(●ˇ∀ˇ●):</span>
  54.     <!-- background: url("https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png") -2970px 0px no-repeat; -->
  55.     <div id="tangyuan" style="position: absolute; top: 0px; left: 50%; cursor: pointer; width: 270px; height: 129px; margin-left: -135px;" title="团团圆圆乐元宵">
  56.     </div>
  57.     <span style="position: absolute;left: 0%">汤圆我要倒着挖o(* ̄︶ ̄*)o:</span>
  58.     <!-- background: url("https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/img/1519708766000_4f5d68844a93b929405a11d217ef0bf5.png") -2970px 0px no-repeat; -->
  59.     <div id="tangyuan1" style="position: absolute; top: 0px; left: 20%; cursor: pointer; width: 270px; height: 129px; margin-left: -135px;" title="团团圆圆乐元宵">
  60.     </body>
  61. </html>



本文网址:https://www.zztuku.com/index.php/detail-11024.html
站长图库 - Js实现动态的仿百度“元宵节汤圆”特效
申明:本文转载于《cnblogs》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐