13.JavaScript实现鼠标左键拖拽效果

2021-02-07 02:18

阅读:421

标签:width   port   display   order   radius   title   方式   win   charset   

实现鼠标左键拖拽效果的两种方式:

方式一:

技术图片技术图片
DOCTYPE html>
html lang="en">

head>
    meta charset="UTF-8">
    meta name="viewport" content="width=device-width, initial-scale=1.0">
    title>Documenttitle>
    style>
        .move {
            width: 100px;
            height: 100px;
            background: red;
            border-radius: 50%;
            position:absolute;
            left:0;
            top:0;
        }
    style>
head>

body>
    div class="move">

    div>
    script>
        //制作一个鼠标左键拖拽效果
        var div = document.getElementsByClassName("move")[0];
        var style = window.getComputedStyle(div);
        var divLeft = parseFloat(style.left);
        var divTop = parseFloat(style.top);
        div.onmousedown = function(e){
            if(e.button !== 0){
                return ;//不是鼠标左键,return
            }
            window.onmousemove = function(e){
                divLeft += e.movementX;//距上次鼠标位置的X长度
                divTop += e.movementY;//距上次鼠标位置的Y长度
                div.style.left = divLeft + "px";
                div.style.top = divTop + "px";
            }
            window.onmouseup = window.onmouseleave = function(){
                if(e.button === 0){//鼠标左键
                    window.onmousemove = null;
                }
            }
        }
    script>
body>

html>
index.html

 

方式二:

技术图片技术图片
DOCTYPE html>
html lang="en">

head>
    meta charset="UTF-8">
    meta name="viewport" content="width=device-width, initial-scale=1.0">
    title>Documenttitle>
    style>
        .move {
            width: 100px;
            height: 100px;
            background: red;
            border-radius: 50%;
            position:absolute;
            left:0;
            top:0;
        }
    style>
head>

body>
    div class="move">

    div>
    script>
        //制作一个鼠标左键拖拽效果
        var div = document.getElementsByClassName("move")[0];
        var style = window.getComputedStyle(div);
        div.onmousedown = function(e){
            if(e.button !== 0){
                return ;//不是鼠标左键,return
            }
            var divLeft = parseFloat(style.left);
            var divTop = parseFloat(style.top);
            var divPageX = e.pageX;
            var divPageY = e.pageY;
            window.onmousemove = function(e){
                var disX = e.pageX - divPageX;
                var disY = e.pageY - divPageY;
                div.style.left = divLeft + disX + "px";
                div.style.top = divTop + disY + "px";
            }
            window.onmouseup = window.onmouseleave = function(){
                if(e.button === 0){//鼠标左键
                    window.onmousemove = null;
                }
            }
        }
    script>
body>

html>
index.html

 

13.JavaScript实现鼠标左键拖拽效果

标签:width   port   display   order   radius   title   方式   win   charset   

原文地址:https://www.cnblogs.com/lanshanxiao/p/12778804.html


评论


亲,登录后才可以留言!