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;
width: 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>