Skip to content

Instantly share code, notes, and snippets.

@h4
Created April 3, 2012 16:02
Show Gist options
  • Save h4/2293181 to your computer and use it in GitHub Desktop.
Save h4/2293181 to your computer and use it in GitHub Desktop.
Итог задания 05.3
<!DOCTYPE html>
<html>
<head>
<title>Анимированный блок</title>
<style>
#box {
height: 50px;
width: 100px;
background: #F00;
position: absolute;
left: 0;
}
</style>
<script>
function moveEl(el, direction) {
if (isNaN(parseInt(el.style.left))) {
el.style.left = 0;
}
if (parseInt(el.style.left) >= screen.availWidth - 100) {
clearInterval(t);
} else {
el.style.left = parseInt(el.style.left) + 10 + "px";
}
}
window.onload = function() {
el = document.getElementById("box");
t = setInterval(function() { moveEl(el); }, 100);
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Анимированный блок</title>
<style>
#box {
height: 50px;
width: 100px;
background: #F00;
position: absolute;
left: 0;
}
</style>
<script>
function moveEl(el, direction) {
if (typeof direction === 'undefined') {
direction = true;
}
if (isNaN(parseInt(el.style.left))) {
el.style.left = 0;
}
if (parseInt(el.style.left) >= screen.availWidth - 100) {
el.style.left = parseInt(el.style.left) - 10 + "px";
clearInterval(t);
t = setInterval(function() {moveEl(el, !direction); }, 100);
} else if (parseInt(el.style.left) < 0) {
el.style.left = parseInt(el.style.left) + 10 + "px";
clearInterval(t);
t = setInterval(function() {moveEl(el, !direction); }, 100);
}
else {
if (direction) {
el.style.left = parseInt(el.style.left) + 10 + "px";
} else {
el.style.left = parseInt(el.style.left) - 10 + "px";
}
}
}
window.onload = function() {
el = document.getElementById("box");
t = setInterval(function() { moveEl(el); }, 100);
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment