【问题标题】:Smooth Infinite Scrolling Banner [CSS Only]平滑无限滚动横幅 [仅限 CSS]
【发布时间】:2015-07-13 23:41:42
【问题描述】:

希望优化这种无限滚动效果,但我不完全确定在循环回原始图像时如何创建平滑过渡。在 10 多岁时几乎不明显,但在 30 多岁时更明显。我假设它与结束位置边距有关,但不能完全确定它。最后一帧的值应该是多少?

JSFiddle

HTML:

<div class="container">
    <div class="photobanner">
        <img class="first" src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
    </div>
</div>
<div class="container">
    <div class="photobanner">
        <img class="second" src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
    </div>
</div>
<div class="container">
    <div class="photobanner">
        <img class="first" src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
        <img src="http://placehold.it/350x150">
    </div>
</div>

CSS:

.container {
    width: 100%;
    overflow: hidden;
    margin: 10px auto;
    background: white;
}

.photobanner, .photobanner2 {
    height: 233px;
    width: 3550px;
}

.photobanner img, .photobanner2 img {
    padding-right: 10px;
    height: 233px;
    width: 350px;
}

.photobanner img  {
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    -ms-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

.photobanner img:hover {
    -webkit-transform: scale(1.2);
    -moz-transform: scale(1.2);
    -o-transform: scale(1.2);
    -ms-transform: scale(1.2);
    transform: scale(1.2);
    cursor: pointer;

    -webkit-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
    -moz-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
    box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
}
/*keyframe animations*/
.first {
    -webkit-animation: bannermove 30s linear infinite;
       -moz-animation: bannermove 30s linear infinite;
        -ms-animation: bannermove 30s linear infinite;
         -o-animation: bannermove 30s linear infinite;
            animation: bannermove 30s linear infinite;
}

@keyframes "bannermove" {
 0% {margin-left: 0px;}
 100% {margin-left: -2125px;}
}

@-moz-keyframes bannermove {
 0% {margin-left: 0px;}
 100% {margin-left: -2125px;}
}

@-webkit-keyframes "bannermove" {
 0% {margin-left: 0px;}
 100% {margin-left: -2125px;}
}

@-ms-keyframes "bannermove" {
 0% {margin-left: 0px;}
 100% {margin-left: -2125px;}
}

@-o-keyframes "bannermove" {
 0% {margin-left: 0px;}
 100% {margin-left: -2125px;}
}

.second {
    -webkit-animation: bannermoves 30s linear infinite;
       -moz-animation: bannermoves 30s linear infinite;
        -ms-animation: bannermoves 30s linear infinite;
         -o-animation: bannermoves 30s linear infinite;
            animation: bannermoves 30s linear infinite;
}

@keyframes "bannermoves" {
 0% {margin-left: -2125px;}
 100% {margin-left: 0px;}
}

@-moz-keyframes bannermoves {
 0% {margin-left: -2125px;}
 100% {margin-left: 0px;}
}

@-webkit-keyframes "bannermoves" {
 0% {margin-left: -2125px;}
 100% {margin-left: 0px;}
}

@-ms-keyframes "bannermoves" {
 0% {margin-left: -2125px;}
 100% {margin-left: 0px;}
}

@-o-keyframes "bannermoves" {
 0% {margin-left: -2125px;}
 100% {margin-left: 0px;}
}

【问题讨论】:

    标签: html css css-transitions css-animations


    【解决方案1】:

    看看这个:https://jsfiddle.net/sergdenisov/wb28eeh2/3/。

    图像之间有不必要的空间(原因 display: inline,请阅读这篇文章 - https://css-tricks.com/fighting-the-space-between-inline-block-elements/)。我设置:

    .photobanner, .photobanner2 {
        font-size: 0
    }
    

    然后我删除padding-right: 2px 并设置:

    .photobanner img, .photobanner2 img {
        margin-right: 5px;
    }
    

    2 个img 标记之间的实际空格是6px,现在是5px。

    现在我们可以计算出动画所需的margin-left:6 x (350 + 5) = 2130px。就是这样。

    【讨论】:

    • 效果很好,感谢您提供指向那篇关于间距的文章的链接。有趣的解决方案!
    • 一个后续,为了计算所需的左边距,为什么是 6?
    • @MikeReddy 2125px 在您的代码中。它是 ~5,97 块宽度 (350 + 6)。我刚刚四舍五入到 6 块宽度 = 2130px.
    • @SergeyDenisov 我可以知道你在上面的计算中从哪里得到值 6,你正在乘以。
    • @SergeyDenisov 我面临着同样情况的问题,第一次滚动工作得更好,但当涉及到第二个循环时,它甚至没有关闭。你能建议我哪里错了吗?这是我的Demo
    猜你喜欢
    • 2022-01-23
    • 2014-08-02
    • 2013-08-01
    • 2023-03-30
    • 1970-01-01
    • 2011-02-04
    • 2016-08-03
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多