时间:2023-09-27 12:42:01 | 来源:网站运营
时间:2023-09-27 12:42:01 来源:网站运营
手把手教你用纯css3实现轮播图效果:<section class="slider-contaner"> <ul class="slider"> <li class="slider-item slider-item1"></li> <li class="slider-item slider-item2"></li> <li class="slider-item slider-item3"></li> <li class="slider-item slider-item4"></li> <li class="slider-item slider-item5"></li> </ul></section>
html代码没有什么可说的,样式的话首先必定slider的大盒子必定是相对定位,另外我们采用在li标签中添加background-image,因为这样才有可能用纯的css实现响应式,另外背景图为了在响应式中看清全貌,必然使用background-size:100%,另外就是高度问题了,显然slider-container必需是和li的高度一致,因为响应式中必然这个高度不能固定死,所以使用height属性显然不行,padding属性可以解决这个问题,一是background-image可以显示在padding中,二是padding中以%为单位是以父元素宽度为基准的。*{ margin:0; padding:0;}ul,li{ list-style: none;}.floatfix { *zoom: 1;}.floatfix:after { content: ""; display: table; clear: both;}.slider-contaner{ width:100%; position:relative;}.slider,.slider-item{ padding-bottom:40%;}.slider-item{ width:100%; position:absolute; background-size:100%;}.slider-item1{ background-image:url(imgs/1.jpg);}.slider-item2{ background-image:url(imgs/2.jpg);}.slider-item3{ background-image:url(imgs/3.jpg);}.slider-item4{ background-image:url(imgs/4.jpg);}.slider-item5{ background-image:url(imgs/5.jpg);}
@keyframes fade{0%{ opacity:0; z-index:2;}5%{ opacity:1; z-index: 1;}20%{ opacity:1; z-index:1;}25%{ opacity:0; z-index:0;}100%{ opacity:0; z-index:0;}}
接下来就是为每张图片添加animation-delay了,因为第一张图片必须显示在最前,所以其他通过相邻兄弟选择器使用opacity:0,第一张图片开始不需要淡入淡出,直接跳至停留也就是5%,所以animation-delay为-1s,第二章图片和第一张相隔20%,也就是4s,animation-delay为3s,以此类推.slider-item + .slider-item{ opacity:0;}.slider-item1{ animation-delay: -1s;}.slider-item2{ animation-delay: 3s;}.slider-item3{ animation-delay: 7s;}.slider-item4{ animation-delay: 11s;}.slider-item5{ animation-delay: 15s;}
这个时候我们的轮播图可以动了<div class="focus-container"><ul class="floatfix"> <li><div class="focus-item focus-item1"></div></li><li><div class="focus-item focus-item2"></div></li><li><div class="focus-item focus-item3"></div></li><li><div class="focus-item focus-item4"></div></li><li><div class="focus-item focus-item5"></div></li></ul></div>.focus-container{ position:absolute; bottom:2%; z-index:7; margin:0 auto; left:0; right:0;}.focus-container ul{ margin-left:46%;}.focus-container li{ width:10px; height:10px; border-radius:50%; float:left; margin-right:10px; background:#fff;}.focus-item{ width:100%; height:100%; background:#51B1D9; border-radius:inherit; animation-duration: 20s; animation-timing-function: linear; animation-name:fade; animation-iteration-count: infinite;}.focus-item1{ animation-delay: -1s;}.focus-item2{ animation-delay: 3s;}.focus-item3{ animation-delay: 7s;}.focus-item4{ animation-delay: 11s;}.focus-item5{ animation-delay: 15s;}
<section class="slider-contaner"><ul class="slider"><li class="slider-item slider-item1"></li><li class="slider-item slider-item2"></li><li class="slider-item slider-item3"></li><li class="slider-item slider-item4"></li><li class="slider-item slider-item5"></li></ul><div class="focus-container"><ul class="floatfix"> <li><div class="focus-item focus-item1"></div></li><li><div class="focus-item focus-item2"></div></li><li><div class="focus-item focus-item3"></div></li><li><div class="focus-item focus-item4"></div></li><li><div class="focus-item focus-item5"></div></li></ul></div></section>/*css reset start*/*{margin:0;padding:0;}ul,li{list-style: none;}/*css reset end*//*css public start*/.floatfix {*zoom: 1;}.floatfix:after {content: "";display: table;clear: both;}/*css public end*//*slider start*/.slider-contaner{width:100%;position:relative;}.slider-item + .slider-item{opacity:0;}.slider-item{width:100%;position:absolute;animation-timing-function: linear;animation-name:fade;animation-iteration-count: infinite;background-size:100%;}.focus-container{position:absolute;z-index:7;margin:0 auto;left:0;right:0;}.focus-container li{width:10px;height:10px;border-radius:50%;float:left;margin-right:10px;background:#fff;}.focus-item{width:100%;height:100%;border-radius:inherit;animation-timing-function: linear;animation-name:fade;animation-iteration-count: infinite;}.focus-item2,.focus-item3,.focus-item4,.focus-item5{opacity:0;}.focus-container ul{margin-left:46%;}/*设置轮播焦点的位置*/.focus-container{bottom:2%;}/*设置当前图片焦点的颜色*/.focus-item{background:#51B1D9;}/*设置动画,请根据实际需要修改秒数*/.slider-item,.focus-item{animation-duration: 20s;}.slider-item1,.focus-item1{animation-delay: -1s;}.slider-item2,.focus-item2{animation-delay: 3s;}.slider-item3,.focus-item3{animation-delay: 7s;}.slider-item4,.focus-item4{animation-delay: 11s;}.slider-item5,.focus-item5{animation-delay: 15s;}@keyframes fade{0%{opacity:0;z-index:2;}5%{opacity:1;z-index: 1;}20%{opacity:1;z-index:1;}25%{opacity:0;z-index:0;}100%{opacity:0;z-index:0;}}/*设置背景,响应式请利用媒体查询根据断点修改路径*/.slider-item1{background-image:url(imgs/1.jpg);}.slider-item2{background-image:url(imgs/2.jpg);}.slider-item3{background-image:url(imgs/3.jpg);}.slider-item4{background-image:url(imgs/4.jpg);}.slider-item5{background-image:url(imgs/5.jpg);}/*设置图片的高度,请根据具体需要修改百分比,响应式及时修改此值*/.slider,.slider-item{padding-bottom:40%;}
关键词:实现,效果,把手