tbcrawford
tbcrawford

Reputation: 497

Transition left div width over right div and vise versa

Please refer to this jsfiddle: https://jsfiddle.net/b53te5qb/1/

I am attempting to make each of these div widths transition nicely over the other.

Right now it is an instant effect, but I would like for it to transition smoothly. When I attempt the transition it starts to get buggy.

Here is the HTML:

<div class="outer">
    <div class="color left"></div>
    <div class="color right"></div>
</div>

And here is the CSS so far:

.outer {
    position: relative;
    height: 100px;
    width: 200px;
}

.color {
    height: 50px;
    width: 50%;
    float: left;
    transition: width 0.3s linear;
    -webkit-transition: width 0.3s linear;
}

.color:hover {
    width: 100%;
    position: absolute;
}

.left {
    background-color: #ff0;
}

.right {
    background-color: #0ff;
}

I am open to restructuring this however I would need to in order to complete the task. I just provided this as a base example.

Upvotes: 0

Views: 143

Answers (3)

Michael Coker
Michael Coker

Reputation: 53709

If you're just doing this with solid colors, I would transition transform: scaleX(). Using transition with transform will give you better performance.

.outer {
    position: relative;
    height: 100px;
    width: 200px;
}

.color {
    height: 50px;
    width: 50%;
    float: left;
    transition: transform 0.3s linear;
    -webkit-transition: transform 0.3s linear;
    transform-origin: 0 0;
}

.color:hover {
    transform: scaleX(2);
}

.left {
    background-color: #ff0;
}

.right {
    background-color: #0ff;
    transform-origin: 100% 0;
}
<div class="outer">
    <div class="color left"></div>
    <div class="color right"></div>
</div>

Upvotes: 2

Maciej Kwas
Maciej Kwas

Reputation: 6459

Here you go: https://jsfiddle.net/prowseed/b53te5qb/10/

Two techniques, one with flexbox and one with position absolute, pick any :)

.outer {
    position: relative;
    height: 100px;
    width: 666px;
    display:flex;
}

.color {
    flex: 1;
    height: 100%;
    transition: .3s;
}

.color:hover {
    flex-basis:100%;
}

.outer2 {
  margin-top:100px;
  position: relative;
  height: 100px;
  width: 666px;
}

.outer2:hover .color {
  width:0;
}

.outer2 .color {
    position:absolute;
    top:0;
    left:0;
    bottom:0;
    width:50%;
  }

  .outer2 .color + .color {
    left:auto;
    right:0;
  }

  .outer2 .color:hover {
    width:100%;
    z-index:2;
  }

Upvotes: 1

Cherna
Cherna

Reputation: 58

You'll need to position them absolutely in order to avoid them from moving.

https://jsfiddle.net/b53te5qb/6/

I would highly recommend not transitioning the width, much better would be to transition transform: translateX(), since it will be hardware accelerated and much smoother: https://jsfiddle.net/b53te5qb/8/.

It still needs polishing, but the idea is there. (note the overflow: hidden to avoid showing the excess.) Another improvement would be to have two elements on top (50%/50% width) that trigger the hover via javascript, since when the elements move it's difficult to keep the hover on them, or to remove the hover without leaving the .outer component.

Hope it helps.

Upvotes: 1

Related Questions