如何在滑动时旋转和缩放卡片

Vin*_*nce 7 html javascript css

当我尝试制作一个旋转的轮播时,我想知道如何移动旋转的卡片,并且当用户不断滑动时,它的缩放比例从最小 0.8(左卡和右卡)到最大比例 1(中心卡)

Scale: 
left = 0.8 
center = 1 
right = 0.8
Run Code Online (Sandbox Code Playgroud)

我正在尝试解决如何使用变换和 z-index 属性来旋转它们。卡片也会旋转,但是我仍在尝试制定一个公式来说明如何创建使卡片旋转的函数

接受任何替代解决方案该动画与 codepen 中的轮播类似,但它不会滑动轮播旋转

Scale: 
left = 0.8 
center = 1 
right = 0.8
Run Code Online (Sandbox Code Playgroud)
const CONTAINER_FLEX = document.querySelector('.container-flex');
const items = document.querySelectorAll('.item');

let touchStartX = 0;
let touchMoveX = 0;
let count = 0;

let current_translate = 0;
let previous_translate = 0;

CONTAINER_FLEX.addEventListener('touchstart', (event) => {
  touchStartX = event.touches[0].pageX;
});

CONTAINER_FLEX.addEventListener('touchmove', (event) => {
  touchMoveX = event.touches[0].pageX;

  current_translate = previous_translate + (touchMoveX - touchStartX);

  console.log(current_translate);

  items[1].style.transform = `translateX(${current_translate}px)`;
});

CONTAINER_FLEX.addEventListener('touchend', () => {
  current_translate = touchMoveX - touchStartX;
  previous_translate = current_translate;
});
Run Code Online (Sandbox Code Playgroud)
*,
 ::before,
 ::after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.5;
  background-color: #131b24;
}

.main-container {
  padding: 30px 0;
  height: 300px;
  width: 900px;
  border-top: 1px solid #444;
  border-bottom: 1px solid #444;
  overflow: hidden;
  background-color: white;
}

.container-flex {
  height: 100%;
  display: flex;
  transition: transform 400ms cubic-bezier(0.165, 0.84, 0.44, 1);
}

.item {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 300px;
  max-width: 300px;
}

.item h1 {
  font-size: 40px;
  color: white;
}


/* ITEMS */

.item-1 {
  background-color: #2c3e50;
  transform: translateX(100px);
  z-index: 1;
}

.item-2 {
  background-color: #3498db;
  z-index: 2;
}

.item-3 {
  background-color: #1abc9c;
  transform: translateX(-100px);
  z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例https://jsfiddle.net/4ue5sgm9/3/

rad*_*bob 0

我想知道当卡片从 0 到 200 时如何移动卡片

将所有卡片排成一个数组。

var cards = [ thing1, thing2, thing3];
Run Code Online (Sandbox Code Playgroud)

使用%-模运算符,这是循环回到起点的秘密

      cardIndex = (cardIndex + 1) % cards.length
Run Code Online (Sandbox Code Playgroud)

聊天中复制过来的。为了清楚起见,我把它写得很详细

scrollLeft() {
 nextIndex = items.indexOf(displayed[2])
 
 nextIndex = ++nextIndex % items.length-1
 displayed = items[nextIndex]
 
 nextIndex = ++nextIndex % items.length-1;
 displayed.push( items[nextIndex] );
 
 nextIndex = ++nextIndex % items.length-1;
 displayed.push( items[nextIndex] )
}
Run Code Online (Sandbox Code Playgroud)

PS项目数组的写入和迭代器。迭代器在最后一项之后停止。迭代器就是“for (x in thisArray)”起作用的原因。但是编写 next() 函数来返回“% this.length-1” => 现在你有一个永无休止的循环程序。上面的所有代码都消失了。