纯CSS轮播 - 更改幻灯片时滑动动画的问题

时间:2016-10-11 22:50:11

标签: css3 carousel css-animations

我创建了基于纯CSS的Carousal。我正在使用javascript只是为了自动更改幻灯片,除了一切都是CSS。我试图通过CSS“过渡”属性来改变每张幻灯片的滑动动画效果,但它并没有在整个幻灯片容器中滑动。 HTML:

<div class="carousel">
    <div class="carousel-inner" id="carousel-inner">
        <input class="carousel-open" type="radio" id="carousel-1" name="carousel" aria-hidden="true" hidden="" checked="checked">

        <div class="carousel-item">
            <h1>This is my first slide</h1>
        </div>
        <input class="carousel-open" type="radio" id="carousel-2" name="carousel" aria-hidden="true" hidden="">

        <div class="carousel-item">
            <h1>This is my second slide</h1>
        </div>
        <input class="carousel-open" type="radio" id="carousel-3" name="carousel" aria-hidden="true" hidden="">

        <div class="carousel-item">
            <h1>This is my third slide</h1>
        </div>
        <label for="carousel-3" class="carousel-control prev control-1">‹</label>
        <label for="carousel-2" class="carousel-control next control-1">›</label>
        <label for="carousel-1" class="carousel-control prev control-2">‹</label>
        <label for="carousel-3" class="carousel-control next control-2">›</label>
        <label for="carousel-2" class="carousel-control prev control-3">‹</label>
        <label for="carousel-1" class="carousel-control next control-3">›</label>
        <ol class="carousel-indicators">
            <li>
                <label for="carousel-1" class="carousel-bullet">&#9679;</label>
            </li>
            <li>
                <label for="carousel-2" class="carousel-bullet">&#9679;</label>
            </li>
            <li>
                <label for="carousel-3" class="carousel-bullet">&#9679;</label>
            </li>
        </ol>
    </div>
</div>

使用Javascript:

var n = 0;
window.ev = false;
document.getElementById("carousel-inner").onmouseenter = function () {
    window.ev = true;
};
document.getElementById("carousel-inner").onmouseleave = function () {
    window.ev = false;
    setTimeout(autoSlide, 400);
};

function autoSlide() {
    if (window.ev == false) {
        n++;
        if (n === 4)
            n = 1;
        document.getElementById("carousel-" + n).checked = true;
        setTimeout(autoSlide, 4000);
    }
}
autoSlide();

CSS:

.carousel {
    position: relative;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.64);
    width: 100%;
}

.carousel-inner {
    position: relative;
    overflow: hidden;
    width: 100%;
}

.carousel-open:checked + .carousel-item {
    position: static;
    opacity: 100;
}

.carousel-item {
    position: absolute;
    opacity: 0;
    -webkit-transition: opacity 0.6s ease-out;
    transition: opacity 0.6s ease-out;
    display: block;
    min-height: 330px;
    width: calc(100% - 100px);
    margin: 0 auto;
}

.carousel-control {
    border-radius: 50%;
    color: #aaa;
    cursor: pointer;
    display: none;
    font-size: 80px;
    line-height: 35px;
    position: absolute;
    top: 50%;
    -webkit-transform: translate(0, -50%);
    cursor: pointer;
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);
    text-align: center;
    width: 40px;
    z-index: 10;
}

.carousel-control.prev {
    left: 2%;
}

.carousel-control.next {
    right: 2%;
}

.carousel-control:hover {
    color: #000;
}

#carousel-1:checked ~ .control-1,
#carousel-2:checked ~ .control-2,
#carousel-3:checked ~ .control-3 {
    display: block;
}

.carousel-indicators {
    list-style: none;
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
}

.carousel-indicators li {
    display: inline-block;
}

.carousel-bullet {
    color: #757575;
    cursor: pointer;
    display: block;
    font-size: 24px;
    font-weight: 300;
}

#carousel-1:checked ~ .control-1 ~ .carousel-indicators li:nth-child(1) .carousel-bullet,
#carousel-2:checked ~ .control-2 ~ .carousel-indicators li:nth-child(2) .carousel-bullet,
#carousel-3:checked ~ .control-3 ~ .carousel-indicators li:nth-child(3) .carousel-bullet {
    color: #009eb1;
}
 .carousel-item {
    background: lightblue;
    padding: 20px;
    text-align: center;
}

这是js小提琴工作示例:https://jsfiddle.net/jt68zz9L/4/

如果你们中的任何人都有滑动动画的CSS解决方案,请告诉我们。感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

很酷的想法BTW,总是善于在CSS中利用CSS制作重/连续动画。

CSS中的小问题: - )

您仅在控件上使用了transform属性,这是多余的。

您需要:

1)向所有项添加过渡,而不仅仅是项目的不透明度。 2)给予检查的项目翻译为0,未经检查的项目为100%平移x轴(你可以使该负面切换方向。)

以下是我向您分发的jsfiddle:JsFiddle

这是更新的代码加代码段:

var n = 0;
window.ev = false;
document.getElementById("carousel-inner").onmouseenter = function () {
    window.ev = true;
};
document.getElementById("carousel-inner").onmouseleave = function () {
    window.ev = false;
    setTimeout(autoSlide, 400);
};

function autoSlide() {
    if (window.ev == false) {
        n++;
        if (n === 4)
            n = 1;
        document.getElementById("carousel-" + n).checked = true;
        setTimeout(autoSlide, 4000);
    }
}
autoSlide();
.carousel {
    position: relative;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.64);
    width: 100%;
}

.carousel-inner {
    position: relative;
    overflow: hidden;
    width: 100%;
}

.carousel-open:checked + .carousel-item {
    position: static;
    opacity: 100;
}

.carousel-item {
    position: absolute;
    opacity: 0;
    -webkit-transition: all 1s ease-out;
    transition: all 1s ease-out;
    display: block;
    min-height: 330px;
    width: calc(100% - 100px);
    margin: 0 auto;
      -webkit-transform: translate(100%, 0px);
    cursor: pointer;
    -ms-transform: translate(100%, 0px);
    transform: translate(100%, 0px);
}

.carousel-open:checked + .carousel-item  {
  -webkit-transform: translate(0px, 0px);
    cursor: pointer;
    -ms-transform: translate(0px, 0px);
    transform: translate(0px, 0px);
}

.carousel-control {
    border-radius: 50%;
    color: #aaa;
    cursor: pointer;
    display: none;
    font-size: 80px;
    line-height: 35px;
    position: absolute;
    top: 50%;
   /* -webkit-transform: translate(0, -50%);
    cursor: pointer;
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);*/
    text-align: center;
    width: 40px;
    z-index: 10;
}

.carousel-control.prev {
    left: 2%;
}

.carousel-control.next {
    right: 2%;
}

.carousel-control:hover {
    color: #000;
}

#carousel-1:checked ~ .control-1,
#carousel-2:checked ~ .control-2,
#carousel-3:checked ~ .control-3 {
    display: block;
}

.carousel-indicators {
    list-style: none;
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
}

.carousel-indicators li {
    display: inline-block;
}

.carousel-bullet {
    color: #757575;
    cursor: pointer;
    display: block;
    font-size: 24px;
    font-weight: 300;
}

#carousel-1:checked ~ .control-1 ~ .carousel-indicators li:nth-child(1) .carousel-bullet,
#carousel-2:checked ~ .control-2 ~ .carousel-indicators li:nth-child(2) .carousel-bullet,
#carousel-3:checked ~ .control-3 ~ .carousel-indicators li:nth-child(3) .carousel-bullet {
    color: #009eb1;
}
 .carousel-item {
    background: lightblue;
    padding: 20px;
    text-align: center;
}
<div class="carousel">
    <div class="carousel-inner" id="carousel-inner">
        <input class="carousel-open" type="radio" id="carousel-1" name="carousel" aria-hidden="true" hidden="" checked="checked">

        <div class="carousel-item">
            <h1>This is my first slide</h1>
        </div>
        <input class="carousel-open" type="radio" id="carousel-2" name="carousel" aria-hidden="true" hidden="">

        <div class="carousel-item">
            <h1>This is my second slide</h1>
        </div>
        <input class="carousel-open" type="radio" id="carousel-3" name="carousel" aria-hidden="true" hidden="">

        <div class="carousel-item">
            <h1>This is my third slide</h1>
        </div>
        <label for="carousel-3" class="carousel-control prev control-1">‹</label>
        <label for="carousel-2" class="carousel-control next control-1">›</label>
        <label for="carousel-1" class="carousel-control prev control-2">‹</label>
        <label for="carousel-3" class="carousel-control next control-2">›</label>
        <label for="carousel-2" class="carousel-control prev control-3">‹</label>
        <label for="carousel-1" class="carousel-control next control-3">›</label>
        <ol class="carousel-indicators">
            <li>
                <label for="carousel-1" class="carousel-bullet">&#9679;</label>
            </li>
            <li>
                <label for="carousel-2" class="carousel-bullet">&#9679;</label>
            </li>
            <li>
                <label for="carousel-3" class="carousel-bullet">&#9679;</label>
            </li>
        </ol>
    </div>
</div>