Skip to content

Instantly share code, notes, and snippets.

@artlili
Created February 20, 2020 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artlili/5398851d83b6076c12c32535b34f4c65 to your computer and use it in GitHub Desktop.
Save artlili/5398851d83b6076c12c32535b34f4c65 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.slider {
position:relative;
overflow: hidden;
margin: 0px;
width: 200px;
height: 200px;
}
.items {
white-space: nowrap;
}
.item {
display: inline-block;
}
</style>
<body>
<div id="slider">
<div class="items">
<div class="item">Курьерская доставка</div>
<div class="item">Самовывоз</div>
<div class="item">Почта России</div>
<div class="item">Курьерская доставка</div>
<div class="item">Самовывоз</div>
<div class="item">Почта России</div>
</div>
</div>
</body>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
$(function () {
var slider = $("#slider"),
items = $(".items"),
sliderWidth = slider.outerWidth(),
sliderScroll = slider[0].scrollWidth,
diff = (sliderScroll / sliderWidth) - 1, // разница ширины контейнеров
padding = 20, // Padding при скроле
damp = 20, // Для плавности скрола
mousePos = 20, // Позиция начальная
mousePos2 = 0, // Позиция после скрола
posX = 0,
moveArea = sliderWidth - (padding * 2), // доступная область передвижения мыши
moveRatio = (sliderWidth / moveArea); // доступный коэффицент передвижения
console.log(sliderScroll)
slider.on('swipe', function (e) {
mousePos = e.pageX - this.offsetLeft;
mousePos2 = Math.min(Math.max(0, mousePos - padding), moveArea) * moveRatio;
posX += (mousePos2 - posX) / damp;
items.css({
marginLeft: -posX * diff
});
});
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment