左右拖拽div

代码如下:

<!DOCTYPE html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<style>
    body,html{
		margin:0;
		padding:0;
		height:100%;
	}
	#box{
		width:600px;
		height:500px;
		overflow:hidden;
	}
	#left{
		width:calc(30% - 5px);
		height:100%;  
		background:skyblue;
		float:left;
	}

	#resize{
		width:5px;
		height:100%;
		cursor: w-resize;
		float:left;
	}

	#right{
		float:right;
		width:70%;
		height:100%;  
		background:tomato;
	}
    </style>
	

</head>
<body>



<div id="box">
  <div id="left"></div>
  <div id="resize"></div>
  <div id="right"></div>
</div>
</body>
<script>
	window.onload = function(){
	var resize = document.getElementById("resize");
	var left = document.getElementById("left");
	var right = document.getElementById("right");
	var box = document.getElementById("box");
	resize.onmousedown = function(e){
    var startX = e.clientX;
    resize.left = resize.offsetLeft;
    document.onmousemove = function(e){
      var endX = e.clientX;

      var moveLen = resize.left + (endX - startX);
      var maxT = box.clientWidth - resize.offsetWidth;
      if(moveLen<150) moveLen = 150; 
      if(moveLen>maxT-150) moveLen = maxT-150;

      resize.style.left = moveLen;
      left.style.width = moveLen + "px";
      right.style.width = (box.clientWidth - moveLen - 5) + "px";
    }
    document.onmouseup = function(evt){
      document.onmousemove = null;
      document.onmouseup = null; 
      resize.releaseCapture && resize.releaseCapture();
    }
    resize.setCapture && resize.setCapture();
    return false;
  }
}


</script>
</html>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用uni-app自带的swiper组件实现左右拖动div换位的效果。具体做法如下: 1. 在页面中添加swiper组件,并设置为横向滚动。 ``` <swiper class="swiper-container" :duration="300" :autoplay="false" :circular="true" :previousMargin="0" :nextMargin="0" :touchable="true" :touchMoveStopPropagation="true" :touchEndStopPropagation="true" :threshold="50" :pagination="{clickable: true}" :navigation="{nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev'}"> <swiper-item v-for="(item, index) in items" :key="index"> <div class="item-wrapper" @touchstart="touchstart(index)" @touchmove="touchmove(index)" @touchend="touchend(index)"> {{item}} </div> </swiper-item> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </swiper> ``` 2. 在样式文件中设置swiper-container的宽度和高度,以及item-wrapper的样式。 ``` .swiper-container { width: 100%; height: 200px; } .item-wrapper { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; font-size: 30px; background-color: #fff; } ``` 3. 在页面组件的methods中添加touchstart、touchmove和touchend方法,来处理拖动换位的逻辑。 ``` methods: { touchstart(index) { this.startIndex = index; this.startX = event.touches[0].clientX; }, touchmove(index) { let moveX = event.touches[0].clientX; let distance = moveX - this.startX; if (Math.abs(distance) > 50) { // 滑动距离大于50时,才允许换位 let moveIndex = this.startIndex + (distance > 0 ? -1 : 1); if (moveIndex >= 0 && moveIndex < this.items.length) { // 更新items数组中的数据 let temp = this.items[this.startIndex]; this.items.splice(this.startIndex, 1); this.items.splice(moveIndex, 0, temp); // 更新startIndex this.startIndex = moveIndex; // 更新startX this.startX = moveX; } } }, touchend(index) { // 手指离开时,停止滑动 this.$refs.swiper.stopAutoplay(); } } ``` 这样,就可以实现左右拖动div换位的效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值