Skip to content

Instantly share code, notes, and snippets.

@jubstuff
Last active January 4, 2016 03:59
Show Gist options
  • Select an option

  • Save jubstuff/8565062 to your computer and use it in GitHub Desktop.

Select an option

Save jubstuff/8565062 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
#box {
background-color: #369;
height: 100px;
left: 50px;
position: absolute;
top: 50px;
width: 100px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
(function(){
var speed = 10,
moveBox = function(moveBy) {
var el = document.getElementById("box"),
left = el.offsetLeft;
if ( (moveBy > 0 && left > 399) || (moveBy < 0 && left < 51) ) {
clearInterval(timer);
timer = setInterval(function() {
moveBox(moveBy * -1);
}, speed);
}
el.style.left = left + moveBy + 'px';
};
var timer = setInterval(function(){
moveBox(2);
}, speed);
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment