gpt4 book ai didi

javascript - Carousel slider - 如何流畅地制作卡片动画?

转载 作者:行者123 更新时间:2023-11-28 19:30:28 25 4
gpt4 key购买 nike

我不太擅长 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/

最佳答案

嘿,这是你需要的吗。

对您的 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>

关于javascript - Carousel slider - 如何流畅地制作卡片动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55177100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com