I have 3 divs
positioned next to each other, and background div
. We'll call the background div
bg div
. Whenever one of the 3 divs
get selected, the bg div
gets positioned on top of the selected div
. (Basically, something like a segmented controller.)
When I try making the bg div
transition to its new position. I did the following to transition:
transition: left linear .2s;
When you select a div
for the first time, the animation doesn't happen, but the bg div
does move over to its correct position (just without the animation). After the second click and on, it works.
How can I add a transition to the bg div
?
$('.inner').click(function() {
var pos = $(this).position();
$("#back").css('left', pos.left + "px");
});
#wrapper {
width: 600px;
height: 30px;
position: relative;
}
.inner {
display: block;
float: left;
width: calc(100% / 3);
height: 50px;
}
#back {
background-color: yellow;
height: 100%;
z-index: -1;
width: calc(100% / 3);
position: relative;
transition: left linear 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<div class="inner">First Option</div>
<div class="inner">Second Option</div>
<div class="inner">Third Option</div>
<div id="back"></div>
</div>