.
.
, , :
- Chrome , .
- Safari . , , float, , .
$("circle").css({
top:Math.round($("circle").offset().top+yvel/16),
left:Math.round($("circle").offset().left+xvel/16)
});
, . Chrome ( ).
- Firefox . margin of 0px.
Firefox, UP, , , - xvel--, xvel++, yvel-- yvel++, , xvel+=2, xvel-=2, yvel+=2 yvel-=2, ++xvel, --xvel, ++yvel --yvel, ( " t , , ). Chrome Safari.
, .
HTML .
css:
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,600' rel='stylesheet' type='text/css'> * {
margin: 0;
}
html, body {
-webkit-text-smoothing: antialiased;
height:100%;
overflow:hidden;
color: #fff;
background-color: #181818;
margin: 0px;
}
circle {
position:absolute;
top:0;
left:0;
width:20px;
height:20px;
border-radius:10px;
background:#fff;
}
JavaScript :
var yvel = 0, xvel = 0;
var up = false,
left = false,
right = false,
down = false;
document.addEventListener("keydown", function (evt) {
if (evt.keyCode == 87 || evt.keyCode == 38) {
up = true;
}
if (evt.keyCode == 65 || evt.keyCode == 37) {
left = true;
}
if (evt.keyCode == 68 || evt.keyCode == 39) {
right = true;
}
if (evt.keyCode == 83 || evt.keyCode == 40) {
down = true;
}
if (evt.keyCode == 8 || evt.keyCode == 80) {
}
});
document.addEventListener("keyup", function (evt) {
if (evt.keyCode == 87 || evt.keyCode == 38) {
up = false;
}
if (evt.keyCode == 65 || evt.keyCode == 37) {
left = false;
}
if (evt.keyCode == 68 || evt.keyCode == 39) {
right = false;
}
if (evt.keyCode == 83 || evt.keyCode == 40) {
down = false;
}
});
function y(obj){
return $(obj).offset().top;
}
function x(obj){
return $(obj).offset().left;
}
setInterval(function(){
if (up) {
--yvel;
--yvel;
} else if (yvel < 0) {
yvel++;
}
if (left) {
--xvel;
--xvel;
} else if (xvel < 0) {
xvel++;
}
if (right) {
++xvel;
++xvel;
} else if (xvel > 0) {
xvel--;
}
if (down) {
++yvel;
++yvel;
} else if (yvel > 0) {
yvel--;
}
var nextposx = $("circle").offset().left+xvel/16;
var nextposy = $("circle").offset().top+yvel/16;
if(nextposy < 0 || nextposy+20 > $('body').height()){
yvel = Math.round(yvel*-0.5);
}
if(nextposx < 0 || nextposx+20 > $('body').width()){
xvel = Math.round(xvel*-0.5);
}
$('circle').css({
top:Math.round($('circle').offset().top+yvel/16),
left:Math.round($('circle').offset().left+xvel/16)
});
},8);
JSFiddle .
, , Firefox , .
, , , , .
, ...
= D.