JS——offset偏移量及client可视区应用

改变大小

要求:鼠标放到小div上点击改变大小,离开小div后点击则不再改变。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        padding: 0;
        margin: 0;
        border: 0;
    }
    #big{
        width: 200px;
        height: 200px;
        background-color: antiquewhite;
        position: relative;
    }
    #small{
        width: 50px;
        height: 50px;
        background-color: rgb(3, 3, 3);
        position: absolute;
        right: 0;
        bottom: 0;
    }
    #small:hover{
        /* 使鼠标变成双箭头形式 */
        cursor: nw-resize;
    }
    img{
        width: 100%;
        height:100%;
        
    }
</style>
<body>
    <div id="big" >
        
        <div id="small">
            
        </div>
    </div>
    <script>

        let smallDom=document.querySelector('#small')
        smallDom.onmousedown=function(event){
            let xDiff=event.offsetX;
            let yDiff=event.offsetY;
            //鼠标点击位置到smalldiv左边和下边的距离
            let rigth=smallDom.clientWidth-xDiff;
            let bottom=smallDom.clientHeight-yDiff;
            let bigdiv=document.querySelector('#big')
            //offsetLeft,offsetTop,与带有定位的父元素最左边和最上边之间的偏移量
            document.onmousemove=function(event){
                bigdiv.style.width=event.clientX-bigdiv.offsetLeft+rigth+'px'
                bigdiv.style.height=event.clientY-bigdiv.offsetTop+bottom+'px'
            }

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

拖拽

要求:鼠标按下div跟着鼠标移动,鼠标弹起div在鼠标弹起地位置随即停止移动。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    div{
        height: 80px;
        width: 80px;
        background-color: darksalmon;
        margin: auto;
        position: absolute;
    }
</style>
<body>
    <div>     
    </div>
    <script>
        let div=document.querySelector('div')
        //鼠标按下
        div.onmousedown=function(event1){           
            //鼠标移动
            let X=event1.offsetX;
            let Y=event1.offsetY;
            document.onmousemove=function(event){
                div.style.left=event.clientX-X+'px'
                div.style.top=event.clientY-Y+'px'
            }
            //鼠标弹起
            document.onmouseup=function(){
                document.onmousemove=null;
                document.onmouseup=null;
            }           
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值