zoukankan      html  css  js  c++  java
  • js 解决图片不能自由拖动

    <!DOCTYPE html>
    <html lang="">

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title Page</title>

        <!-- Bootstrap CSS -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
                <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.3/html5shiv.js"></script>
                <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
            <![endif]-->
        <!-- jQuery -->
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <!-- Bootstrap JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <style>
        div {
            position: absolute;
            left: 0;
            top: 0;
            background: red;
             100px;
            height: 100px;
        }
    </style>

    <body>
        <div><img src="https://www.baidu.com/img/flexible/logo/pc/result.png" ondragstart=true></div>
    </body>
    <script>
        $('div').mousedown(function () {
            e = window.event;

            
            // 重点重点重点重点重点重点重点重点重点重点重点
            if (e.stopPropagation) e.stopPropagation();
            if (e.preventDefault) e.preventDefault();
            e.cancelBubble = true;
            e.returnValue = false;



            $('img').dragstart = false
            x = e.screenX
            y = e.screenY
            left = $('div').css('left')
            tops = $('div').css('top')
            document.onmousemove = function () {
                e = window.event;
                var newx = e.screenX
                var newy = e.screenY
                nowLeft = parseInt(left) + parseInt(newx) - parseInt(x)
                nowTop = parseInt(tops) + parseInt(newy) - parseInt(y)
                $('div').css('left', nowLeft + "px")
                $('div').css('top', nowTop + "px")


            }
            document.onmouseup = function () {
                document.onmousemove = null
                document.mousedown = null
            }


        })
    </script>

    </html>
  • 相关阅读:
    [LeetCode] Output Contest Matches 输出比赛匹配对
    [LeetCode] 527. Word Abbreviation 单词缩写
    [LeetCode] Permutation in String 字符串中的全排列
    [LeetCode] 560. Subarray Sum Equals K 子数组和为K
    [LeetCode] Reshape the Matrix 重塑矩阵
    [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
    [LeetCode] IPO 上市
    [LeetCode] Binary Tree Tilt 二叉树的坡度
    [LeetCode] Array Partition I 数组分割之一
    [LeetCode] Zuma Game 祖玛游戏
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/13731488.html
Copyright © 2011-2022 走看看