Qiwu - Animation - Notes

Keywords: Javascript

Transform transform

  • Move, rotate, scale elements horizontally
  • transform does not affect the layout of other elements
  • Value: none|
//html

    <ul>
        <li class="item-1"><span>Initial state</span></li>
        <li class="item-2"><span>translation</span></li>
        <li class="item-3"><span>rotate</span></li>
        <li class="item-4"><span>zoom</span></li>
    </ul>
//css

    ul {
        font-size: 14px;
        line-height: 4em;
    }

    li {
        display: block;
        text-align: center;
        margin: 1em;
    }

    li span {
        display: block;
        width: 6em;
        background: coral;
        transition: all .3s;
    }

    .item-2:hover span {
        transform: translate(100px, 0);
    }

    .item-3:hover span {
        transform: rotate(45deg);
    }

    .item-4:hover span {
        transform: scale(2, 0.5);
    }

Transform list (note that their order has an impact)

//html

    <div class="box-1">box-1</div>
    <div class="box-2">box-2</div>
//css

html,body {
    font-size: 16px;
}
div {
    line-height: 6em;
    width: 6em;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -3em 0 0 -3em;
}

.box-1 {
    transform: rotate(-45deg) translate(100px, 0);
    background: coral;
}

.box-2 {
    transform: translate(100px, 0) rotate(-45deg);
    background: lightblue;
}

Specify an axis transform

  • translateX
  • translateY
  • scaleX
  • scaleY

transform-origin

//html
    <ul>
        <li class="item-1"><span>Center rotation</span></li>
        <li class="item-2"><span>Top left corner rotation</span></li>
        <li></li>
    </ul>
//css

ul {
            font-size: 14px;
            line-height: 4em;
        }
        li {
            display: block;
            text-align: center;
            margin: 1em;
        }
        li span {
            display: block;
            width: 6em;
            background: coral;
            transition: all .3s ease;
        }

       li:hover span {
        transform: rotate(45deg);
       }
       .item-2:hover span {
        transform-origin: 0 0 ;
       }

3D Transform

perspective

  • Specifies the distance between the human eye and the Z plane for 3D rendering
  • Does not affect the rendering of the element itself
  • Affects only the 3D effect of the child elements (which is important)
//html
    <div class="container-2d">
        <p> 2D Transform</p>
    </div>
    <div class="container-3d">
        <p>3D transform</p>
    </div>
//css
div {
        padding: 1em;
        border: 1px solid lightgray;
    }
    p {
        transform: rotateY(45deg);
        margin: 0;
        line-height: 4;
        width: 8em;
        text-align: center;
        background: lightgray;
    }
    .container-3d {
        perspective: 10em;
    }
  • translate3d()
  • translateZ()
  • rotate3d()
  • rotateX()
  • rotateY()

Transition transition

  • Specifies how to navigate from one style state to another
  • Meaning of animation: tell the user what happened

Designated transition

  • What attributes need transition when they change
  • How long does the transition last
  • What is the speed change
  • Is there a delay

transition

  • transition-property
  • transition-duration
  • transition-timing-function
  • transition-delay
//html
    <div class="box"></div>
//css

.box {
        transition: all 2s ease-in;
        width: 200px;
        height: 200px;
        margin: 1em auto;
        border-width: 50px;
        border-style: solid;
        border-top-color: #f35;
        border-right-color: #fa0;
        border-bottom-color:#649;
        border-left-color: #269; 
    }
    .box:hover {
        width: 0;
        height: 0;
    }
//Note that the animation here is one second delayed

//html
  <div class="box"></div>   
//css
    .box {
        height: 100px;
        width: 100px;
        background: coral;
        transition: width 1s ease 1s, height 1s ease;
    }
    .box:hover {
        width: 200px;
        height: 200px;
    }

http://cubic-bezier.com/#.17,.67,.83,.67

//html

    <nav>
        <a href="#">Home</a>
        <a href="#">HTML</a>
        <a href="#">CSS</a>
        <a href="#">JavaScript</a>
        <a href="#">HTTP</a>
    </nav>
//css
nav {
    width: 100%;
    display: table;
    border-collapse: collapse;
    font-size: 14px;
    line-height: 3;
}

nav a {
    display: table-cell;
    text-align: center;
    border: 1px solid #fff;
    text-decoration: none;
    color: rgba(255, 255, 255, .8);
    background: hsl(181, 58%, 52%);
    transition: all .5s ease;
}

nav a:hover {
    color: #fff;
    background: hsl(181, 58%, 45%);
}

animation

  • animation enables more complex style changes
  • Defining keyframes
  • Specify animation behavior

Key frame

//html

   <i class="scroll-down">⬇️ </i>]
//css

@keyframes down {
    from {
        margin-top: 0;
        opacity: 1;
    }
    50% {
        margin-top: 0.5em;
        opacity: 0.3;
    }
    to {
        margin-top: 0;
        opacity: 1;
    }
}

.scroll-down {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-left: -0.5em;
    font: normal normal 48px/1 Helvetica;
    color: #f66;
    animation: down 1.5s ease infinite;
}

Posted by dannymm on Wed, 01 Apr 2020 11:34:23 -0700