HTML動畫與輪播圖

HTML動畫與輪播圖

1.輪播圖

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>輪播圖</title>
</head>
<style type="text/css">
    .wrap{
        width: 300px;
        height: 200px;
        border: 5px green solid;
        margin: 200px auto;
        position: relative;
        overflow: hidden;
    }
    .box{
        width: 1500px;
        height: 200px;
        background-color: red;
        position: absolute;
        left: 0px;

    }
    .box div{
        width: 300px;
        height: 200px;
        line-height: 200px;
        text-align: center;
        color: white;
        float: left;
    }
    .item1{
        background-color: red;
    }
    .item2{
        background-color: green;
    }
    .item3{
        background-color: yellow;
    }
    .item4{
        background-color: blue;
    }
    .item1{
        background-color: gold;
    }
    .item1{
        background-color: green;
    }
    .item1{
        background-color: red;
    }
    .pre,.next{
        width: 30px;
        height: 60px;
        background-color: blue;     
        text-align: center;
        line-height: 60px;
        position: absolute;
        top: 75px;
    }
    .next{  
        position: absolute;
        right: 0px;
    }

</style>
<body>
<div class="wrap">
    <div class="box">
        <div class="item1">1</div>
        <div class="item2">2</div>
        <div class="item3">3</div>
        <div class="item4">4</div>
        <div class="item5">5</div>
        <div class="item6">6</div>
    </div>
    <div class="pre"><<</div>
    <div class="next">>></div>
</div>
</body>
<script type="text/javascript">
    var pre = document.getElementsByClassName('pre')[0];
    var next = document.getElementsByClassName("next")[0];
    var box = document.getElementsByClassName("box")[0];
    var indext = 0;
    next.onclick = function () {
        indext--;
        move(indext);
    }   
    pre.onclick = function(){
        indext++;
        move(indext);
    }
    function move(newIndex){
        indext = newIndex;
        var l = indext * 300; 
        if (l > 0) {
        l = -1200;
        indext = -4;
        }else if (l < -1200) {
          l = 0;
          indext = 0;
        }
        var a = l - box.offsetLeft;// 需要移動距離減去現在的距離
        var count  = 0;
        clearInterval(timer);
        var timer =  setInterval(function(){
            count++;
            box.style.left = box.offsetLeft + a/30 + "px";
            if(count == 30){
                clearInterval(timer);
                box.style.left = l + "px";
            }
        },10)
         }
setInterval(function(){
        next.onclick();
    },2000)
</script>
</html>

2.動畫

<!DOCTYPE html>
<html>
<head>
    <title>動畫</title>
    <style type="text/css">
        .redDiv{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            left: 0px;
            top: 0px;
            /*動畫名稱*/
            animation-name: run;
            /*動畫時長*/
            animation-duration: 5s;
            /*動畫運動方式*/
            /*linear勻速*/
            /*esse慢-快-慢*/
            /*ease-in越來越快*/
            /*ease-out越來越慢*/
            /*esse-in-ont低速開始和結束*/
            animation-timing-function: cubic-bezier(0.4,0.4,0.6,0.6);
            /*動畫次數*/
            animation-iteration-count: 2;
            /*動畫方向*/
            animation-direction: normal;
            /*normal正常*/
            /*reverse反向*/
            /*alternate 1/3/5..正向 2/4/6反向*/
            animation-fill-mode: both;
            /*animation-fill-mode: forwards;*/
            /*forwards動畫結束停留在結束位置*/
            /*backwards在動畫延時期間,元素位置在動畫起始位置*/
            /*both 都包括*/

        }
        @keyframes run{
            0%{
                left: 0px;
            }50%{
                top: 0px;
                left: 1200px;
            }100%{
                left: 500px;
                top: 300px;
            }
        }
    </style>
</head>
<body>
<div class="redDiv"></div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章