当我们写移动端的时候经常会碰到的轮播图,今天从触摸拖动的角度来简单实现一下
HTML
<div class="view">
<div class="banner">
<div style="background-color: blue;">1</div>
<div style="background-color: yellow;">2</div>
<div style="background-color: red;">3</div>
<div style="background-color: blue;">1</div>
</div>
</div>
CSS
<style>
* {
margin: 0;
padding: 0;
}
.view {
width: 500px;
height: 500px;
border: 1px solid red;
font-size: 50px;
color: #fff;
overflow: hidden;
}
.banner {
display: flex;
}
.banner div {
width: 500px;
height: 500px;
/* 为了让div在弹性布局之后不被压缩 */
flex-shrink: 0;
}
</style>
JS
<script>
var num = 0;
var x1, y1;
$('.view').on('touchstart', function () {
console.log(event);
// 触摸开始位置
x1 = event.changedTouches[0].clientX;
// y1 = event.changedTouches[0].clientY;
})
$('.view').on('touchend', function () {
console.log(event);
// 触摸结束位置
var x2 = event.changedTouches[0].clientX;
// v