jquery 自定义插件 拖拽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <!-- 
        1.给jquery对象本身扩展方法  $.xxx
        2.给jquery Dom对象扩展方法  $(div).xxx
     -->

    <div class="div1">fff</div>
    <div class="div2">fff</div>
    <div class="div3">fff</div>
    <script>
        // 1.给jquery对象本身扩展方法
        $.extend({
            lg(){
                console.log('扩展lg方法')
            }
        })
        // $.lg()


        // 2.给jquery Dom对象扩展方法
        $.fn.extend({
            domfun(){
                // $(this); 指向调用者 $(div)
                console.log('扩展domfun方法')
                return $(this)
            }
        })
        

        // 或者
        $.fn.domfun2 = function(){
            // $(this); 指向调用者 $(div)
            console.log('扩展domfun方法')
            return $(this)
        }
        // $(div).domfun().domfun2


        $.fn.dragGable = function(options={}){
            const defaultObj = {
                limit: false,
            }
            Object.assign(defaultObj, options)

            const {limit} = defaultObj
            const dom = $(this)

            // 修改元素定位
            dom.css({
                position:'absolute',
                top:0,
                left:0,
                cursor: 'move'
            })

            dom.mousedown(function(e){
                let startX = e.pageX -  dom.offset().left
                let startY = e.pageY -  dom.offset().top

                $(document).mousemove(function(ed){
                    let moveX = ed.pageX - startX
                    let moveY = ed.pageY - startY

                    // 边界限制
                    if(limit){
                        let rightBorder =  $(this).innerWidth() - dom.outerWidth()
                        let bottomBorder =  $(this).innerHeight() - dom.outerHeight()
                        moveX = moveX <= 0 ? 0 : moveX
                        moveX = moveX >= rightBorder ? rightBorder : moveX
                        moveY = moveY <= 0 ? 0 : moveY
                        moveY = moveY >= bottomBorder ? bottomBorder : moveY
                    }
                    dom.css({
                        left: moveX,
                        top: moveY
                    })
                })

                $(document).mouseup(function(){
                    $(this).off()
                })

                return false
            })
        }

        $('.div1').dragGable({
            limit: true, // 是否限制范围
        })

        $('.div2').dragGable()
        $('.div3').dragGable()
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值