html拖拽 门户,实现H5左右拖拽

html,

body {

width: 100%;

}

* {

margin: 0;

padding: 0;

}

div {

width: 100%;

height: 500px;

background-color: aliceblue;

overflow: hidden;

}

ul {

display: flex;

height: 100%;

width: 400%;

position: relative;

left: 0;

}

li {

list-style: none;

width: 100%;

height: 100%;

font-size: 86px;

}

  • a
  • b
  • c
  • d

let oBox = document.getElementById("test");

oBox.addEventListener("touchstart", function(ev) {

let startX = e.targetTouches[0].clientX;

let startY = e.targetTouches[0].clientY;

let disX = startX - oBox.offsetLeft;

let disY = startY - oBox.offsetTop;

let dir = ""; //拖拽方向 x代表横向拖拽 y代表纵向

function fnMove(ev) {

let curX = e.targetTouches[0].clientX;

let curY = e.targetTouches[0].clientY;

if (dir == '') {

if (Math.abs(curX - startX) >= 5) {

dir = 'x';

} else if (Math.abs(curY - startY) >= 5) {

dir = 'y';

}

} else {

if (dir == 'x') {

oBox.style.left = e.targetTouches[0].clientX - disX + 'px';

} else if (dir == 'y') {

oBox.style.top = e.targetTouches[0].clientY - disY + 'px';

}

}

}

function fnEnd() {

oBox.removeEventListener('touchmove', fnMove, false);

oBox.removeEventListener('touchend', fnEnd, false);

}

oBox.addEventListener('touchmove', fnMove, false);

oBox.addEventListener('touchend', fnEnd, false);

})

9e10ffcc1d96

image.png

手风琴效果

html,

body {

width: 100%;

}

* {

margin: 0;

padding: 0;

}

div {

width: 100%;

height: 500px;

background-color: aliceblue;

overflow: hidden;

}

ul {

display: flex;

height: 100%;

width: 400%;

position: relative;

left: 0;

transition: left .1s;

}

li {

list-style: none;

width: 100%;

height: 100%;

font-size: 86px;

}

  • a
  • b
  • c
  • d

let oBox = document.getElementById("test");

function touchMoveFunc (index = 0) {

let children_legnth = oBox.children.length;

let child_length = oBox.clientWidth / children_legnth;

function next () {

oBox.style.left

}

let startLeft = 0;

let direction = '';

// let startX =

oBox.addEventListener("touchstart", function(e) {

let startX = e.targetTouches[0].clientX;

let startY = e.targetTouches[0].clientY;

let disX = startX - oBox.offsetLeft;

let disY = startY - oBox.offsetTop;

let dir = ""; //拖拽方向 x代表横向拖拽 y代表纵向

function touchMoveHandler(e) {

let curX = e.targetTouches[0].clientX;

let curY = e.targetTouches[0].clientY;

if (dir == '') {

if (Math.abs(curX - startX) >= 5) {

dir = 'x';

} else if (Math.abs(curY - startY) >= 5) {

dir = 'y';

}

} else {

if (dir == 'x') {

// oBox.style.left = e.targetTouches[0].clientX - disX + 'px';

oBox.style.left = e.targetTouches[0].clientX - disX + 'px';

} else if (dir == 'y') {

oBox.style.top = e.targetTouches[0].clientY - disY + 'px';

}

}

}

function touchEndHandler() {

let startX = e.targetTouches[0].clientX;

let disX = startX - oBox.offsetLeft;

let endLeft = e.targetTouches[0].clientX - disX

if (startLeft > endLeft) {

direction = 'LEFT';

if (index > -(children_legnth) + 1) {

index--

}

} else if (startLeft < endLeft) {

direction = 'RIGHT';

if (index < 0) {

index++

}

} else {

direction = 'NONE';

}

setIndex(index);

oBox.removeEventListener('touchmove', touchMoveHandler, false);

oBox.removeEventListener('touchend', touchEndHandler, false);

}

oBox.addEventListener('touchmove', touchMoveHandler, false);

oBox.addEventListener('touchend', touchEndHandler, false);

})

function setIndex (index) {

oBox.style.left = index * child_length + 'px';

startLeft = index * child_length;

}

function next () {

if (index > -(children_legnth) + 1) {

index--

setIndex(index);

}

}

function prev () {

if (index < 0) {

index++

setIndex(index);

}

}

return {

next,

prev

}

}

const obj = touchMoveFunc()

``

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值