写的js文件不运行

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="demo.css">

</head>
<body>
   <div id="rabbit"></div>
   <!--<script src="demo.js"></script>-->
   <script>
       /**
        * Created by 22935 on 2016/11/10.
        */
       var imgUrl='rabbit-big.png';
       var ele=document.getElementById("rabbit");
       var positions=['0 -854','-174 -852','-349 -852','-524 -852','-698 -852','-873 -848'];
       animation(ele,positions,imgUrl);

       function animation(ele,positions,imgUrl) {
           ele.style.backgroundImage="url("+imgUrl+")";
           ele.style.backgroundRepeat="no-repeat";
           alert("haha");
           var index=0;
           function run() {
               var position=positions[index].split(' ');
               ele.style.backgroundPosition=position[0]+"px "+position[1]+"px";
               index++;
               if(index>=positions.length){
                   index=0;
               }

               setTimeout(run,80);
           }
           run();

       }
   </script>
</body>
</html>
阅读 2.5k
2 个回答

运行了,还运行的很好,我把position改为positions=['0 1','2 3','4 5','6 7','8 9','10 11'];就能看到图片了,貌似你数字太大了

和数字大小没关系啊,我运行了下,你的代码没错啊。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题