轮播滑块-如何平滑为卡片设置动画?

时间:2019-03-15 06:51:33

标签: javascript jquery css sass

我对CSS和动画不太满意,有人可以帮我做轮播动画吗?

这是我当前的工作:https://codepen.io/aceraven777/pen/NJYxjo

HTML:

<div class="carousel">
    <div class="card-carousel">
        <div class="my-card">111</div>
        <div class="my-card">222</div>
        <div class="my-card">333</div>
        <div class="my-card">444</div>
        <div class="my-card">555</div>
        <div class="my-card">666</div>
        <div class="my-card">777</div>
        <div class="my-card">888</div>
        <div class="my-card">999</div>
    </div>
</div>

SCSS:

body {
  padding-top: 50px;
}

.carousel {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        font-family: 'Quicksand', sans-serif;
        overflow-x: hidden;
        position: relative;
        top: -2.5rem;

        .card-carousel {
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;

            .my-card {
                height: 20rem;
                width: 170px;
                position: relative;
                z-index: 1;
                -webkit-transform: scale(0.6) translateY(-2rem);
                transform: scale(0.6) translateY(-2rem);
                opacity: 0;
                cursor: pointer;
                pointer-events: none;
                background: #2e5266;
                background: linear-gradient(to top, #2e5266, #6e8898);
                transition: 1s;
            }

            .my-card.active {
                z-index: 3;
                -webkit-transform: scale(1) translateY(0) translateX(0);
                transform: scale(1) translateY(0) translateX(0);
                opacity: 1;
                pointer-events: auto;
                transition: 1s;
            }

            .my-card.prev, .my-card.next {
                z-index: 2;
                opacity: 0.6;
                pointer-events: auto;
                transition: 1s;
            }

            .my-card.prev {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(26rem);
                transform: scale(0.8) translateY(-1rem) translateX(26rem);
            }

            .my-card.next {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(-26rem);
                transform: scale(0.8) translateY(-1rem) translateX(-26rem);
            }
        }
    }

JS:

var $num = $('.card-carousel .my-card').length;
    var $even = $num / 2;
    var $odd = ($num + 1) / 2;

    if ($num % 2 == 0) {
        $('.card-carousel .my-card:nth-child(' + $even + ')').addClass('active');
        $('.card-carousel .my-card:nth-child(' + $even + ')').prev().addClass('prev');
        $('.card-carousel .my-card:nth-child(' + $even + ')').next().addClass('next');
    } else {
        $('.card-carousel .my-card:nth-child(' + $odd + ')').addClass('active');
        $('.card-carousel .my-card:nth-child(' + $odd + ')').prev().addClass('prev');
        $('.card-carousel .my-card:nth-child(' + $odd + ')').next().addClass('next');
    }

    $('.card-carousel .my-card').on('click', function() {
        if ($('.card-carousel').is(':animated')) {
            return;
        }

        var $slide = $('.card-carousel .active').width();

        if ($(this).hasClass('next')) {
            $('.card-carousel').animate({left: '-=' + $slide});
        } else if ($(this).hasClass('prev')) {
            $('.card-carousel').animate({left: '+=' + $slide});
        }

        $(this).removeClass('prev next');
        $(this).siblings().removeClass('prev active next');

        $(this).addClass('active');
        $(this).prev().addClass('prev');
        $(this).next().addClass('next');
    });


    // Keyboard nav
    $('html body').keydown(function(e) {
        if (e.keyCode == 37) { // left
            $('.card-carousel .active').prev().trigger('click');
        }
        else if (e.keyCode == 39) { // right
            $('.card-carousel .active').next().trigger('click');
        }
    });

如何使动画流畅?就像何时移到下一张卡片一样,背景中的卡片也会绕圈旋转。如何使其直线移动。

我的代码基于这里:https://www.jqueryscript.net/demo/Smooth-Card-Carousel-jQuery-CSS3/

1 个答案:

答案 0 :(得分:0)

嘿,这就是您所需要的。

对SCSS所做的更改很少

您的代码

.my-card.prev {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(26rem);
                transform: scale(0.8) translateY(-1rem) translateX(26rem);
            }

            .my-card.next {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(-26rem);
                transform: scale(0.8) translateY(-1rem) translateX(-26rem);
            }

SCSS

 .my-card.prev {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(0rem);
                transform: scale(0.8) translateY(-1rem) translateX(0rem);
            }

            .my-card.next {
                -webkit-transform: scale(0.8) translateY(-1rem) translateX(0rem);
                transform: scale(0.8) translateY(-1rem) translateX(0rem);
            }

var $num = $('.card-carousel .my-card').length;
    var $even = $num / 2;
    var $odd = ($num + 1) / 2;

    if ($num % 2 == 0) {
        $('.card-carousel .my-card:nth-child(' + $even + ')').addClass('active');
        $('.card-carousel .my-card:nth-child(' + $even + ')').prev().addClass('prev');
        $('.card-carousel .my-card:nth-child(' + $even + ')').next().addClass('next');
    } else {
        $('.card-carousel .my-card:nth-child(' + $odd + ')').addClass('active');
        $('.card-carousel .my-card:nth-child(' + $odd + ')').prev().addClass('prev');
        $('.card-carousel .my-card:nth-child(' + $odd + ')').next().addClass('next');
    }

    $('.card-carousel .my-card').on('click', function() {
        if ($('.card-carousel').is(':animated')) {
            return;
        }

        var $slide = $('.card-carousel .active').width();
        
        if ($(this).hasClass('next')) {
            $('.card-carousel').animate({left: '-=' + $slide});
        } else if ($(this).hasClass('prev')) {
            $('.card-carousel').animate({left: '+=' + $slide});
        }
        
        $(this).removeClass('prev next');
        $(this).siblings().removeClass('prev active next');
        
        $(this).addClass('active');
        $(this).prev().addClass('prev');
        $(this).next().addClass('next');
    });


    // Keyboard nav
    $('html body').keydown(function(e) {
        if (e.keyCode == 37) { // left
            $('.card-carousel .active').prev().trigger('click');
        }
        else if (e.keyCode == 39) { // right
            $('.card-carousel .active').next().trigger('click');
        }
    });
body {
  padding-top: 50px;
}

.carousel {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  font-family: "Quicksand", sans-serif;
  overflow-x: hidden;
  position: relative;
  top: -2.5rem;
}
.carousel .card-carousel {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.carousel .card-carousel .my-card {
  height: 20rem;
  width: 170px;
  position: relative;
  z-index: 1;
  -webkit-transform: scale(0.6) translateY(-2rem);
  transform: scale(0.6) translateY(-2rem);
  opacity: 0;
  cursor: pointer;
  pointer-events: none;
  background: #2e5266;
  background: linear-gradient(to top, #2e5266, #6e8898);
  transition: 1s;
}
.carousel .card-carousel .my-card.active {
  z-index: 3;
  -webkit-transform: scale(1) translateY(0) translateX(0);
  transform: scale(1) translateY(0) translateX(0);
  opacity: 1;
  pointer-events: auto;
  transition: 1s;
}
.carousel .card-carousel .my-card.prev, .carousel .card-carousel .my-card.next {
  z-index: 2;
  opacity: 0.6;
  pointer-events: auto;
  transition: 1s;
}
.carousel .card-carousel .my-card.prev {
  -webkit-transform: scale(0.8) translateY(-1rem) translateX(26rem);
  transform: scale(0.8) translateY(-1rem) translateX(0rem);
}
.carousel .card-carousel .my-card.next {
  -webkit-transform: scale(0.8) translateY(-1rem) translateX(-26rem);
  transform: scale(0.8) translateY(-1rem) translateX(0rem);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="carousel">
	<div class="card-carousel">
		<div class="my-card">111</div>
		<div class="my-card">222</div>
		<div class="my-card">333</div>
		<div class="my-card">444</div>
		<div class="my-card">555</div>
		<div class="my-card">666</div>
		<div class="my-card">777</div>
		<div class="my-card">888</div>
		<div class="my-card">999</div>
	</div>
</div>