-
Notifications
You must be signed in to change notification settings - Fork 35
/
animate.game.html
175 lines (164 loc) · 5.86 KB
/
animate.game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Работа с произвольными очередями</title>
<link rel="profile" href="https://gmpg.org/xfn/11"/>
<link rel="shortcut icon" href="https://anton.shevchuk.name/favicon.ico"/>
<link rel="stylesheet" href="css/styles.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.easing.js"></script>
<script type="text/javascript" src="js/code.js"></script>
<script>
$(function () {
let logX = $("#log-x")
let logY = $("#log-y")
let $player = $("#player")
logX.text($player.css("left"))
logY.text($player.css("top"))
let stepX = function (now, fx) {
if (fx.prop === "left" && fx.start > fx.end && now <= 6) {
$(this).stop().css("left", 0); // Stop the animation if "left" is less than or equal to 0
} else if (fx.prop === "left" && fx.start < fx.end && now > 660) {
$(this).stop().css("left", 660);
}
logX.text(Math.floor(now) + fx.unit)
}
let stepY = function (now, fx) {
if (fx.prop === "top" && fx.start > fx.end && now < 6) {
$(this).stop().css("top", 0); // Stop the animation if "top" is less than or equal to 0
} else if (fx.prop === "top" && fx.start < fx.end && now > 424) {
$(this).stop().css("top", 420);
}
logY.text(Math.floor(now) + fx.unit)
}
setInterval(function() {
$player.animate(
{ "top": "424" },
{ duration: 500, easing: "easeOutBounce", queue: "y", step: stepY }
).dequeue("y")
}, 500)
$(document).keydown(function (e) {
// 37 - `←` | 68 - `d` | left
// 38 - `↑` | 82 - `r` | up
// 39 - `→` | 71 - `g` | right
// 40 - `↓` | 70 - `f` | down
switch (e.keyCode) {
case 37:
case 68:
$player.stop("x", true)
$player.css("transform", "initial")
$player.animate({ "left": "-=100" }, { queue: "x", step: stepX }).dequeue("x")
break
case 38:
case 82:
$player.stop("y", true)
$player.animate({ "top": "-=100" }, { queue: "y", step: stepY }).dequeue("y")
break
case 39:
case 71:
$player.stop("x", true)
$player.css("transform", "scaleX(-1)")
$player.animate({ "left": "+=100" }, { queue: "x", step: stepX }).dequeue("x")
break
case 40:
case 70:
$player.stop("y", true)
$player.animate({ "top": "+=100" }, { queue: "y", step: stepY }).dequeue("y")
break
}
return false;
})
})
</script>
<style>
article {
position: relative;
}
article.screen {
height: 600px;
background: url("images/mario-bg.png");
}
#player {
position:absolute;
width:100px;
height: 100px;
top:424px;
right:24px;
display:none;
background: url("images/mario.svg") no-repeat;
}
</style>
</head>
<body>
<header>
<h1>Произвольная очередь анимации</h1>
<h2>Используя стрелочки на клавиатуре перемещайте элемент (← и →, ↑ и ↓)</h2>
</header>
<main id="content">
<article>
<p>
X: <span id="log-x">0px</span>
Y: <span id="log-y">0px</span>
</p>
</article>
<article class="screen">
<div id="player">
</div>
</article>
</main>
<footer>
© <a href="https://anton.shevchuk.name/jquery-book/">jQuery for beginners</a>
</footer>
<aside>
<nav>
<a href="animate.queue.html" title="go prev" rel="prev">Prev</a>
<a href="index.html#40" title="back to Index" rel="index">Index</a>
<a href="#" title="reload" onclick="window.location.reload();return false">Reload</a>
</nav>
<hr/>
<div>
<code>$(<span>'#player'</span>).show()</code>
<button type="button">Start Game</button>
<code contenteditable="true">$(<span>"#player"</span>)
.css(<span>"transform"</span>, <span>"initial"</span>)
.animate({
<span>"left"</span>: <span>"-=100"</span>
}, {
<span>"queue"</span>: <span>"x"</span>,
<span>"duration"</span>: <i>2000</i>
}).dequeue("x")</code>
<button type="button">← Left</button>
<code contenteditable="true">$(<span>"#player"</span>)
.css(<span>"transform"</span>, <span>"scaleX(-1)"</span>)
.animate({
<span>"left"</span>:<span>"+=100"</span>
}, {
<span>"queue"</span>: <span>"x"</span>,
<span>"duration"</span>: <i>2000</i>
}).dequeue("x")</code>
<button type="button">Right →</button>
<code contenteditable="true">$(<span>"#player"</span>).animate({
<span>"top"</span>:<span>"-=250"</span>
}, {
<span>"queue"</span>: <span>"y"</span>,
<span>"duration"</span>: <i>2000</i>
}).dequeue("y")</code>
<button type="button">↑ Up</button>
<code contenteditable="true">$(<span>"#player"</span>).animate({
<span>"top"</span>:<span>"+=250"</span>
}, {
<span>"queue"</span>: <span>"y"</span>,
<span>"duration"</span>: <i>2000</i>
}).dequeue("y")</code>
<button type="button">↓ Down</button>
<code contenteditable="true">$(<span>"#player"</span>).stop(<span>"x"</span>, true);
$(<span>"#player"</span>).stop(<span>"y"</span>, true);</code>
<button type="button">Stop</button>
<code>$(<span>"#player"</span>).hide()</code>
<button type="button">End Game</button>
</div>
</aside>
</body>
</html>