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 1174
误落风尘
误落风尘 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:09

    I think I've got it.

    .wrapper {
        background:#DDD;
        display:inline-block;
        padding: 10px;
        height: 20px;
        width:auto;
    }
    
    .label {
        display: inline-block;
        width: 1em;
    }
    
    .contents, .contents .inner {
        display:inline-block;
    }
    
    .contents {
        white-space:nowrap;
        margin-left: -1em;
        padding-left: 1em;
    }
    
    .contents .inner {
        background:#c3c;
        width:0%;
        overflow:hidden;
        -webkit-transition: width 1s ease-in-out;
        -moz-transition: width 1s ease-in-out;
        -o-transition: width 1s ease-in-out;
        transition: width 1s ease-in-out;
    }
    
    
    
    .wrapper:hover .contents .inner {
       
        width:100%;
    }
    +
    These are the contents of this div

    Animating to 100% causes it to wrap because the box is bigger than the available width (100% minus the + and the whitespace following it).

    Instead, you can animate an inner element, whose 100% is the total width of .contents.

提交回复
热议问题