做无缝滚动效果,从来一个superslide搞定一切,但平凡的某一天,UI提出了一个问题:这块一直在抖动啊。我定睛一看:确实是在抖,这……之前没注意啊。
找了其他的插件,效果都不尽如人意。
最后,参考蓝湖,用css动画实现。
(这里需要注意一下宽度的设置。)
代码奉上:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="img_wrap">
<div class="img_box img_box1">
<div class="img_item">
<img src="https://www.wetools.com/imgplaceholder/670x324" alt="">
</div>
<div class="img_item">
<img src="https://www.wetools.com/imgplaceholder/670x324" alt="">
</div>
</div>
<div class="img_box img_box2">
<div class="img_item">
<img src="https://www.wetools.com/imgplaceholder/670x324" alt="">
</div>
<div class="img_item">
<img src="https://www.wetools.com/imgplaceholder/670x324" alt="">
</div>
</div>
</div>
<style>
.img_wrap{
overflow: hidden;
width: 750px;
margin-left: auto;
margin-right: auto;
}
.img_box{
font-size: 0;
width: 2680px;
}
.img_box .img_item{
width: 50%;
display: inline-block;
vertical-align: top;
padding: 0 10px;
}
.img_box .img_item img{
width: 100%;
}
.img_box1{
margin-left: -1930px;
-webkit-animation: scrollToLeft 20s linear infinite;
animation: scrollToLeft 20s linear infinite;
}
.img_box2{
margin-top: 20px;
-webkit-animation: scrollToRight 20s linear infinite;
animation: scrollToRight 20s linear infinite;
}
@-webkit-keyframes scrollToRight {
from {
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
}
to {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
}
}
@keyframes scrollToRight {
from {
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
}
to {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
}
}
@-webkit-keyframes scrollToLeft {
from {
-webkit-transform: translate3d(50%, 0, 0);
transform: translate3d(50%, 0, 0);
}
to {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
}
}
@keyframes scrollToLeft {
from {
-webkit-transform: translate3d(50%, 0, 0);
transform: translate3d(50%, 0, 0);
}
to {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
}
}
</style>
</body>
</html>