Animate an element's width from 0 to 100%, with it and it's wrapper being only as wide as they need to be, without a pre-set width, in CSS3 or jQuery

前端 未结 6 1166
误落风尘
误落风尘 2020-12-01 03:22

http://jsfiddle.net/nicktheandroid/tVHYg/

When hovering .wrapper, it\'s child element .contents should animate from 0px to it\

6条回答
  •  再見小時候
    2020-12-01 04:01

    a late answer, but I think this one works as required in the question :)

    this one uses z-index and position absolute, and avoid the issue that the container element width doesn't grow in transition.

    You can tweak the text's margin and padding to suit your needs, and "+" can be changed to font awesome icons if needed.

    body {
      font-size: 16px;
    }
    
    .container {
      height: 2.5rem;
      position: relative;
      width: auto;
      display: inline-flex;
      align-items: center;
    }
    
    .add {
      font-size: 1.5rem;
      color: #fff;
      cursor: pointer;
      font-size: 1.5rem;
      background: #2794A5;
      border-radius: 20px;
      height: 100%;
      width: 2.5rem;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      z-index: 2;
    }
    
    .text {
      white-space: nowrap;
      position: relative;
      z-index: 1;
      height: 100%;
      width: 0;
      color: #fff;
      overflow: hidden;
      transition: 0.3s all ease;
      background: #2794A5;
      height: 100%;
      display: flex;
      align-items: center;
      border-top-right-radius: 20px;
      border-bottom-right-radius: 20px;
      margin-left: 20px;
      padding-left: 20px;
      cursor: pointer;
    }
    
    .container:hover .text {
      width: 100%;
      padding-right: 20px;
    }
    + Add new client

提交回复
热议问题