Der Versuch, ein DIV innerhalb eines Containers DIV vor und zurück zu bewegen. Im Moment rutscht es weiter nach rechts statt zurückzusetzen und nach links zurückzugehen.JQUERY: Div innerhalb eines DIVs hin und her bewegen
Idealerweise sollte er auf die enthaltene DIV-Box einstellbar sein. Code unten:
\t var RIGHT = 1;
\t var LEFT = 0;
\t var bookDirection = RIGHT;
\t var containerwidth = $("#container").width();
\t var objectwidth = $("#book").width();
\t var displacement = 150;
\t var maxwidth = containerwidth - objectwidth/2;
\t var minwidth = 0 + objectwidth/2;
\t setInterval(function(){
\t \t var currentposition = $("#book").position().left;
\t \t $("#output").text("currentposition: " + currentposition + " > Displacement: " + displacement + " >Max width: " + maxwidth);
\t \t $("#output").text("summed: " + (currentposition + displacement));
\t \t $("#output2").text("max is: " + maxwidth);
\t \t //console.log("Book direction is: " +bookDirection);
\t \t if(currentposition + displacement <= maxwidth && bookDirection == RIGHT)
\t \t {
\t \t \t console.log('hit if');
\t \t \t $("#book").animate({
\t \t \t \t left: "+=" + displacement, \t
\t \t \t }, 1000, function(){
\t \t \t // Animation complete. \t
\t \t \t });//inner function call - anim complete
\t \t \t bookDirection == RIGHT
\t \t }
\t \t else if(currentposition + displacement >= minwidth && bookDirection == LEFT)
\t \t { \t console.log('hit else if');
\t \t \t console.log('direction : ' + bookDirection + 'displacement: ' + displacement);
\t \t \t bookDirection = LEFT;
\t \t \t $("#book").animate({
\t \t \t \t left: "-=" + displacement, \t
\t \t \t }, 1000, function(){
\t \t \t // Animation complete. \t
\t \t \t });//inner function call - anim complete
\t \t }
\t \t else if(currentposition + displacement > maxwidth && bookDirection == RIGHT){
\t \t \t console.log('here');
\t \t \t $("#book").stop();
\t \t \t bookDirection = LEFT; \t
\t \t \t console.log(bookDirection);
\t \t }// end IF
\t }, 0); // setinterval
body{
\t color: red;
}
#container{
\t width: 1000px;
\t height: 200px;
\t border: solid 2px black;
}
#book{
\t position: relative;
\t left: 10px;
\t background-color: black;
\t width :100px;
\t height :100px; \t
}
\t <head>
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
\t \t <script src="day044.js"></script>
\t \t <link rel="stylesheet" type="text/css" href="day044.css">
\t </head>
\t <body>
\t \t <div id="clickme">
\t \t \t Click here
\t \t </div>
\t \t <div id="container"></container>
\t \t <img id="book">
\t \t <div id="output"></div>
\t \t <div id="output2"></div>
\t \t <div id="output3"></div>
\t </body>