两端拖拽并且交叉

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>推拽条</title>
	<style>
		.box{
			width:400px;
			height: 20px;
			margin:50px auto;
			position: relative;
			background:#ccc;
			border-radius: 10px;
		}
		.bigBox{
			width:400px;
			height: 20px;
			position: absolute;
			background:blue;
			border-radius: 10px;
			position: absolute;
			top:0;
			left:0;
		}
		.smallBox1{
			width: 20px;
			height: 20px;
			border-radius: 50%;
			background:red;
			position:absolute;
			top:0;
			left:0;
		}
		.smallBox2{
			width: 20px;
			height: 20px;
			border-radius: 50%;
			background:yellow;
			position:absolute;
			top:0;
			left:380px;
		}
	</style>
</head>
<body>
	<div class="box">
		<div class="bigBox"></div>
		<div class="smallBox1"></div>
		<div class="smallBox2"></div>
	</div>
	<script type="text/javascript">
		var oBox=document.querySelector('.box'),
			bBox=document.querySelector('.bigBox'),
			sBox1=document.querySelector('.smallBox1'),
			sBox2=document.querySelector('.smallBox2');
		var boxW=bBox.offsetWidth;

		//首先实现两个smallBox的拖拽
		sBox1.onmousedown=function(e){
			var ev=e||window.event;
			//鼠标在box1的哪个位置
			var offset_x=ev.offsetX;
			document.onmousemove=function(e){
				var ev=e||window.event;
				var x=ev.clientX-oBox.offsetLeft-offset_x;
				x>=oBox.offsetWidth-sBox1.offsetWidth?x=oBox.offsetWidth-sBox1.offsetWidth:x;
				x<=0?x=0:x;
				sBox1.style.left=x+'px';
				//同时可以设置bigBox的宽度随着小球移动而变化
				bBox.style.width=Math.abs(sBox2.offsetLeft-sBox1.offsetLeft)+sBox1.offsetWidth+'px';

				//实现sBox交叉效果
				if(sBox2.offsetLeft-sBox1.offsetLeft<0){
					bBox.style.left=sBox2.offsetLeft+'px';
				}else{
					bBox.style.left=x+'px';
				}

			}
			document.onmouseup=function(){
				document.onmousemove=null;
			}

		}

		sBox2.onmousedown=function(e){
			var ev=e||window.event;
			var offset_x=ev.offsetX;
			document.onmousemove=function(e){
				var ev=e||window.event;
				var x=ev.clientX-oBox.offsetLeft-offset_x;
				x>=oBox.offsetWidth-sBox2.offsetWidth?x=oBox.offsetWidth-sBox2.offsetWidth:x;
				x<=0?x=0:x;
				sBox2.style.left=x+'px';
				//同时可以设置bigBox的宽度随着小球移动而变化,此时要考虑另一端的滑块已经滑动了一些距离
				bBox.style.width=Math.abs(sBox2.offsetLeft-sBox1.offsetLeft)+sBox2.offsetWidth+'px';//由于之前移动了box1,所以bigbox的width以及left不再是初始值bigW那样了,可以在down的时候获得一下bigbox的宽度,但是注意不能在move的时候获得bigbox的width

				//实现sBox交叉效果
				if(sBox2.offsetLeft-sBox1.offsetLeft<0){
					bBox.style.left=x+'px';
				}else{
					bBox.style.left=sBox1.offsetLeft+'px';
				}

			}
			document.onmouseup=function(){
				document.onmousemove=null;
			}
		}
	</script>
</body>
</html>

onmousedown里面加入onmousemove可以实现拖拽。

主要的难点在于要实现两端拖拽,就要时刻更改滑条的宽度以及left值,特别是在交叉之后,它的left的值是由另外一端的滑块的left值决定的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值