HTML动画与轮播图

xiaoxiao2021-02-28  49

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>
转载请注明原文地址: https://www.6miu.com/read-2628976.html

最新回复(0)