Skip to content

Commit 8c7db3e

Browse files
committed
Ex5
1 parent d8f8039 commit 8c7db3e

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

Week1/js-exercises/ex5-catWalk.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</head>
88

99
<body>
10+
1011
<img style="position:absolute;" src="http://www.anniemation.com/clip_art/images/cat-walk.gif" />
11-
1212
<script src="ex5-catWalk.js"></script>
1313
</body>
1414

Week1/js-exercises/ex5-catWalk.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,40 @@
1010
5. When the cat reaches the right - hand of the screen, restart them at the left hand side("0px").So they should keep walking from left to right across the screen, forever and ever.
1111
6. When the cat reaches the middle of the screen, replace the img with an image of a cat dancing(use this URL: https: //tenor.com/StFI.gif), keep it dancing for 5 seconds, and then replace the img with the original image and have it continue the walk.
1212
13-
*/
13+
*/
14+
function sleep(ms) {
15+
return new Promise(
16+
resolve => setTimeout(resolve, ms)
17+
);
18+
}
19+
20+
const catImg = document.querySelector('body img');
21+
catImg.style.left = '0';
22+
let point =0;
23+
function catWalk() {
24+
if (point > screen.width){
25+
point = 0 ;
26+
} else if (point == (screen.width / 2) ) {
27+
28+
catImg.src = 'tenor.gif';
29+
sleep(2000);
30+
catImg.src = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
31+
point++;
32+
33+
34+
} else {
35+
catImg.style.left = `${point}px`;
36+
point++;
37+
}
38+
}
39+
40+
setInterval(catWalk, 5);
41+
42+
//another ways
43+
// setInterval(function(){
44+
// if (point > screen.width){
45+
// point = -300 ;
46+
// }
47+
// catImg.style.left = point + 'px';
48+
// point += 10;
49+
// }, 50);

Week1/js-exercises/tenor.gif

621 KB
Loading

0 commit comments

Comments
 (0)