html实现百叶窗效果
原理很简单就是相当于把图片分成n份开始轮播,下面开始上代码并解析其步骤
style中的代码
* {
margin: 0px;
padding: 0px;
}
/*当然这里要用到绝对定位*/
.byc {
width: 1280px;
height: 720px;
position: relative;
}
/*想分成几份 子容器的宽度就是父容器的宽度/份数 也要用定位超出的部分让其隐藏*/
.byc>div {
width: 160px;
height: 720px;
float: left;
position: relative;
overflow: hidden;
}
.byc>div>p {
width: 160px;
height: 720px;
position: absolute;
}
/*添加图片 本次实例用了五张图片*/
.byc>div>p:nth-child(1) {
background: url(img/1.jpg);
}
.byc>div>p:nth-child(2) {
background: url(img/2.jpg);
}
.byc>div>p:nth-child(3) {
background: url(img/3.jpg);
}
.byc>div>p:nth-child(4) {
background: url(img/4.jpg);
}
.byc>div>p:nth-child(5) {
background: url(img/5.jpg);
}
/*定义每部分的位置*/
.byc>div:nth-child(2) p {
background-position: -160px 0px;
}
.byc>div:nth-child(3) p {
background-position: -320px 0px;
}
.byc>div:nth-child(4) p {
background-position: -480px 0px;
}
.byc>div:nth-child(5) p {
background-position: -640px 0px;
}
.byc>div:nth-child(6) p {
background-position: -800px 0px;
}
.byc>div:nth-child(7) p {
background-position: -960px 0px;
}
.byc>div:nth-child(8) p {
background-position: -1120px 0px;
}
/*定义动画效果*/
@-webkit-keyframes show {
0% {
left: 160px
}
100% {
left: 0px
}
}
@-webkit-keyframes hide {
0% {
left: 0px
}
100% {
left: -160px
}
}
.in {
-webkit-animation: show 0.5s;
left: 0px;
}
.out {
-webkit-animation: hide 0.5s;
left: 160px;
}
body代码,这个没啥好解释的
<div class="byc">
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
<div><p></p><p></p><p></p><p></p><p></p></div>
</div>
script代码
$(function() {
var i = 0;
setInterval(function() {
$(".byc>div").each(function(index, Element) {
$(Element).children("p").eq(i).removeClass("out").addClass("in").siblings().removeClass("in").addClass("out");
});
i++;
if(i == 7) {
i = 0;
}
}, 2000);
});