Ich habe die folgende jQuery-Code für ein Beispielprogramm I wurde aufunten und rechts Verhalten auf jQuery
Arbeits$('#left').click(function(){
$('.box').animate({
left: "-=40px",
}, function(){/* End of Animation*/});
});
$('#right').click(function(){
$('.box').animate({
right: "-=40px",
}, function(){ /* End of Animation*/});
});
$('#up').click(function(){
$('.box').animate({
top: "-=40px",
}, function(){/* End of Animation*/});
});
$('#down').click(function(){
$('.box').animate({
bottom: "-=40px",
}, function(){ /* End of Animation*/});
});
Hier ist der HTML-Code für sie
<div class="box">a box</div>
<div id="navArrows">
<button id="left" class="navigationArrow">←</button>
<button id="up" class="navigationArrow">↑</button>
<button id="right" class="navigationArrow">→</button>
<button id="down" class="navigationArrow">↓</button>
</div>
Und die CSS
#navArrows {
position: relative;
width: 150px;
height: 150px;
margin: 100px auto 0;
background: #333;
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
padding: 20px;
}
.navigationArrow {
width: 50px;
height: 50px;
line-height: 50px;
padding: 0;
margin: 0;
position: absolute;
top: 20px;
left: 20px;
background: white;
color: #222;
}
#up {
left: 50%;
margin-left: -25px;
}
#left, #right {
top: 50%;
margin-top: -25px;
}
#right {
right: 20px;
left: inherit;
}
#down {
bottom: 20px;
top: inherit;
left: 50%;
margin-left: -25px;
}
.box {
height: 100px;
width: 100px;
background: #a7f;
color: white;
border: solid 4px #a1f;
line-height: 100px;
margin: 100px auto 0;
opacity: 0.5;
position: relative;
}
Nun, was dies tun soll, ist das Verschieben der div.box
basierend auf der Schaltfläche, die angekreuzt ist. Um es semantisch zu machen, habe ich die entsprechenden Positionen für jeden Knopf-Tick benutzt. Aber irgendwie. Es fällt mir schwer, den Code zu verstehen.
Der Code funktioniert jedoch, wenn ich folgendes verwenden:
für Rechts- und Links-Tasten
{left: "-=40px"} and {left: "+=40px"} or {right: "-=40px"} and {right: "+=40px"}
für Up- und Down-Tasten
{top: "-=40px"} and {top: "+=40px"} or {bottom: "-=40px"} and {bottom: "+=40px"}
Einsicht auf diese geschätzt wird.