I have code for googly eyes that is too long. Basically I have variables for each element pupils (one for right -other for left), eyesockets (left, right). I wish I could compress my code so I can call a function animateEye (eye).
function animateEye(eye) { console.log(eye); var pupille = eye.find("div");
For now this is what I have and I have no idea how to continue.
This is the actual code:
var pupilleLinks = $('#pupilleLinks');
var pupilleRechts = $('#pupilleRechts');
// var abstandH = pupille.offset().left- e.pageX + pupille.height()/2;
$(document).on('mousemove', function(e){
var abstandV1 = pupilleLinks.offset().top- e.pageY +pupilleLinks.height()/2;
var pupilleL1 = pupilleLinks.position().top - abstandV1 ; // 10 = Breite Pupille / 2
var augeLinks = $("#augeLinks")
var distanceX=augeLinks.width()-pupilleLinks.width();
var distanceY=augeLinks.height()-pupilleLinks.height();
if(pupilleL1 > distanceY) {
(pupilleL1 = distanceY);
}
if(pupilleL1 < 0) {
(pupilleL1 = 0);
}
var abstandH1 = pupilleLinks.offset().left- e.pageX + pupilleLinks.width()/2;
var pupilleL2 = pupilleLinks.position().left - abstandH1 ;
if (pupilleL2 < 0) {
(pupilleL2 = 0);
}
if(pupilleL2 > distanceX) {
(pupilleL2 =distanceX);
}
pupilleLinks.css({
left: pupilleL2+"px",
top: pupilleL1+"px"
});
var abstandH2 = pupilleRechts.offset().left- e.pageX + pupilleRechts.width()/2;
var pupilleR1 = pupilleRechts.position().left - abstandH2;
var augeRechts = $("#augeRechts")
var distanceX2=augeRechts.width()-pupilleRechts.width();
var distanceY2=augeRechts.height()-pupilleRechts.height();
if(pupilleR1 < 0) {
(pupilleR1 = 0);
}
if(pupilleR1 > distanceX2) {
(pupilleR1 = distanceX2);
}
var abstandV2 = pupilleRechts.offset().top- e.pageY + pupilleRechts.width()/2;
var pupilleR2 = pupilleRechts.position().top - abstandV2;
if(pupilleR2 < 0) {
(pupilleR2 = 0);
}
if(pupilleR2 > distanceY2) {
(pupilleR2 =distanceY2);
}
pupilleRechts.css({
left: pupilleR1+"px",
top: pupilleR2+"px"
});
});
.auge {
position: relative;
display: inline-block;
width: 150px;
height: 150px;
border: 10px solid black;
}
.pupille {
position: absolute;
top: 50%;
left: 50%;
width: 30px;
height: 30px;
background-color: blue;
border-radius: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="augeLinks" class="auge">
<div id="pupilleLinks" class="pupille"></div>
</div>
<div id="augeRechts" class="auge">
<div id="pupilleRechts" class="pupille"></div>
</div>
pupilleLinks
get defined? \$\endgroup\$eye
? Is that a DOM element that you want to contain the "eyes"? DoespupileLinks
ever move from the current position? \$\endgroup\$pupileLinks
andpupileRechts
are never defined in your code. What are they? \$\endgroup\$}
invar abstandV2 = pupilleRechts.offset().top- e.pageY + pupilleRechts.wid}th()/2;
that broke the code — probably a pasting error. It's unusual for us to do this, but I've added a bit of HTML and CSS to make the demo — please adjust it to match your code. Welcome to Code Review. \$\endgroup\$