JS 简单的拖拽移动效果

本文展示了如何利用JavaScript实现HTML元素的拖拽移动功能。通过创建Drag类并设置拖拽处理方式,实现在网页上拖动指定元素的效果。示例中详细给出了JS代码和对应的HTML及CSS样式,以便于理解和应用。
摘要由CSDN通过智能技术生成

JS部分:


//html 元素 拖拽效果js类 --start--

function Drag()
{
    //初始化
    this.initialize.apply(this, arguments)
}
Drag.prototype = {
    //初始化
    initialize : function (drag, options)
    {
        this.drag = this.$(drag);
        this._x = this._y = 0;
        this._moveDrag = this.bind(this, this.moveDrag);
        this._stopDrag = this.bind(this, this.stopDrag);


        this.setOptions(options);


        this.handle = this.$(this.options.handle);
        this.maxContainer = this.$(this.options.maxContainer);


        this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;
        this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;


        this.limit = this.options.limit;
        this.lockX = this.options.lockX;
        this.lockY = this.options.lockY;
        this.lock = this.options.lock;


        this.onStart = this.options.onStart;
        this.onMove = this.options.onMove;
        this.onStop = this.options.onStop;


        //this.handle.style.cursor = "move";


        this.changeLayout();


        this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag))
    },


    changeLayout : function ()
    {
        this.drag.style.top = this.drag.offsetTop + "px";
        this.drag.style.left = this.drag.offsetLeft + "px";
        this.drag.style.position = "absolute";
        this.drag.style.margin = "0"
    },


    startDrag : function (event)
    {  
        var event = event || window.event;


        this._x = event.clientX - this.drag.offsetLeft;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值