$ .extend、$ .fn.extend
扩展方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>温故而知“心”</title>
<style>
#demo {
width: 100px;
height: 100px;
background-color: #ff0000;
position: absolute;
}
</style>
</head>
<body>
<div id="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$.extend({
definedMandom: function (start, final) {
const len = final - start;
return Math.random() * len + start;
}
})
$.fn.extend({
drag: function () {
var disX,
disY,
self = this;
$(this).on('mousedown', function (e) {
disX = e.pageX - $(this).offset().left;
disY = e.pageY - $(this).offset().top;
$(document).on("mousemove", function (e) {
$(self).css({ left: e.pageX - disX, top: e.pageY - disY });
})
$(document).on("mouseup", function (e) {
$(document).off("mousemove").off("mouseup");
})
})
return this;
}
})
$('#demo').drag();
</script>
</html>