CSS transitions mixing absolute and relative positioning

后端 未结 2 1942
礼貌的吻别
礼貌的吻别 2020-12-09 01:52

Short and sweet version:

Is it possible to combine position: relative and position: absolute with smooth CSS-transitions?

Verbose v

相关标签:
2条回答
  • 2020-12-09 02:41

    No, you can't animate the position property. There are only a number of css properties you can animate, and most of them have numbers or colors as values (With some exceptions). You can see this list in the w3c css transitions especification.

    Anyway, since you can animate top and left properties, you could change your markup a little to achieve the effect, as in this fiddle.

    div.deck-container {
        position: relative;
    }
    div.deck-container li {
        background-color: #fff;
        position: absolute;
        border: 1px solid black;
        padding: 3px;
        display: inline-block;
        -webkit-transition: all 0.5s ease-in-out;
        -moz-transition: all 0.5s ease-in-out;
        -o-transition: all 0.5s ease-in-out;
        -ms-transition: all 0.5s ease-in-out;
        transition: all 0.5s ease-in-out;
    }
    div.deck-container li {
        left: 160px;
        top: 0px;
    }
    div.deck-container li:first-child {
        left: 0px;
        top: 0px;
    }
    div.deck-container li:nth-child(2) {
        left: 40px;
        top: 0px;
    }
    div.deck-container li:nth-child(3) {
        left: 80px;
        top: 0px;
    }
    div.deck-container li:nth-child(4) {
        left: 120px;
        top: 0px;
    }
    div.deck-container.collapsed li {
        left: 12px;
        top: 8px;
    }
    div.deck-container.collapsed li:first-child {
        left: 0;
        top: 0px;
    }
    div.deck-container.collapsed li:nth-child(2) {
        left: 3px;
        top: 2px;
    }
    div.deck-container.collapsed li:nth-child(3) {
        left: 6px;
        top: 4px;
    }
    div.deck-container.collapsed li:nth-child(4) {
        left: 9px;
        top: 6px;
    }
    

    I just set the original position to absolute and positioned those elements. Then, when toggling the class, only topand left attributes change, so the transition works.

    0 讨论(0)
  • 2020-12-09 02:41

    @nikc.org Maybe you could use translate instead:

        div.deck-container {
            position: relative;
        }
        div.deck-container li {
            background-color: #fff;
            position: relative;
            border: 1px solid black;
            padding: 3px;
            display: inline-block;
            -webkit-transition: all 0.5s ease-in-out;
            -moz-transition: all 0.5s ease-in-out;
            -o-transition: all 0.5s ease-in-out;
            -ms-transition: all 0.5s ease-in-out;
            transition: all 0.5s ease-in-out;
        }
    
        div.deck-container.collapsed li:first-child {
          -webkit-transform: translate(0, 0);
             -moz-transform: translate(0, 0);
              -ms-transform: translate(0, 0);
               -o-transform: translate(0, 0);
                  transform: translate(0, 0);
        }
        div.deck-container.collapsed li:nth-child(2) {
            -webkit-transform: translate(-100%, 2px);
             -moz-transform: translate(-100%, 2px);
              -ms-transform: translate(-100%, 2px);
               -o-transform: translate(-100%, 2px);
                  transform: translate(-100%, 2px);
        }
        div.deck-container.collapsed li:nth-child(3) {
            -webkit-transform: translate(-200%, 4px);
             -moz-transform: translate(-200%, 4px);
              -ms-transform: translate(-200%, 4px);
               -o-transform: translate(-200%, 4px);
                  transform: translate(-200%, 4px);
        }
    

    working example

    0 讨论(0)
提交回复
热议问题