用鼠标左右拖拽 html,js实现鼠标拖拽div左右滑动

本文实例为大家分享了js鼠标拖拽div左右滑动的具体代码,供大家参考,具体内容如下

c27dc0b13801a621c8868303550e2e8d.png

d6392c39c1c6a6d471c052fe56224832.png

Title

body{

position: relative;

margin:0;

padding:0;

width:100%;

height: 1000px;

}

#box{

height: 50px;

width:200px;

position: absolute;

left:50%;

top:50%;

margin-left:-200px;

margin-top:-200px;

background: #CDCDCD;

}

#small-box{

height: 50px;

width:50px;

position: absolute;

left:0;

top:0;

background: #FF66CC;

cursor:move ;

opacity: 0.7;

}

var box=$("#small-box");

var body=$('body');

var index=0;

var x1;

box.mousedown(function(){

index=1; //鼠标按下才能触发onmousemove方法

var x=event.clientX; //鼠标点击的坐标值,x

var left= this.style.left;

left=left.substr(0,left.length-2); //去掉px

x1=parseInt(x-left);

});

box.mousemove(function(){

if(index===1){

this.style.left=event.clientX-x1+'px';

if(this.style.left.substr(0,this.style.left.length-2)<0){

this.style.left=0;

};

if(this.style.left.substr(0,this.style.left.length-2)>150){

this.style.left='150px';

};

}

});

box.mouseup(function(){

index=0;

});

body.mouseup(function(){

index=0;

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2020-01-14

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值