JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.6/gsap.min.js"></script>
<div class="container">
<div class="main">
<div class="cell"><div class="slide"><a href="#12">123</a></div></div>
<div class="cell"><div class="slide">2</div></div>
<div class="cell"><div class="slide">3</div></div>
</div>
</div>
CSS
body {
margin: 0;
}
.clear {
clear: both;
}
.container {
width: 500px;
height: 800px;
background: #333;
overflow: hidden;
}
.touch {
width: 100%;
height: 100%;
}
.block {
width: 50px;
height: 50px;
background: red;
}
.main {
height: 300px;
background: darkslategrey;
position: relative;
}
.cell {
width: 100%;
/* full width */
height: 160px;
/* height of carousel */
margin-right: 30px;
display: grid;
justify-content: space-around;
align-content: center;
position: absolute;
background: darkseagreen;
}
p {
font: 30px Arial;
color: #fff;
float: left;
margin-left: 40px;
display: inline;
user-select: none;
}
a {
user-select: none;
user-drag: none;
}
.pcont {
height: 80px;
width: 1200px;
}
.slide {
background: cadetblue;
width: 300px;
height: 150px;
user-select: none;
}
JavaScript
var posSlide;
for (var i = 1; i <= document.querySelectorAll(".cell").length; i++) {
console.log("ПРОХОД");
posSlide = (i - 1) * 530;
gsap.set(".cell:nth-of-type(" + i + ")", {
left: posSlide
});
}
var startX = 0;
var endX = 0;
var mouse;
var slideX;
$('.container').on('mousedown', function(e) {
slideX = gsap.getProperty(".main", "x");
console.log("Начальная", e.clientX)
startX = e.clientX;
function move(e) {
moveX = e.clientX;
console.log("Движение", moveX);
slideXnow = gsap.getProperty(".main", "x");
if (slideXnow > 0) {
gsap.set(".main", {
x: 0,
ease: "power2.out"
});
} else if (slideXnow < posSlide * (-1)) {
gsap.set(".main", {
x: posSlide * (-1),
ease: "power2.out"
});
} else if (slideXnow <= 0 && slideXnow >= posSlide * (-1)) {
gsap.to(".main", {
x: slideX + moveX - startX,
ease: "power2.out",
duration: 1
});
}
}
function end(e) {
$(document).off('mousemove', move);
lastX = moveX;
console.log("Отпустили", lastX)
}
$(document).on('mousemove', move)
$(document).one('mouseup', end)
})