<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RunJS</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
.container {
background: green;
height: 200px;
position: relative;
margin-left: 100px;
} .rect {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
border: 1px solid red;
background: rgba(230, 230, 230, 0.5);
}
</style>
</head>
<body>
<div class="container">
<div class="rect"></div>
</div>
<script> var $container = $(".container");
var $rect = $(".rect");
var parentOffset = $container.offset() var startPoint = null;
var isDown = null; function init(opts) {
$rect.css({
top: opts.top + 'px',
left: opts.left + 'px'
}); $rect.width(opts.width);
$rect.height(opts.height);
} function reset() {
init({
top: 0,
left: 0,
width: 0,
height: 0
}); $rect.hide();
} reset(); $rect.on('click', function(e) {
edit()
}); $container.on('mousedown', function(e) {
start(e);
}); $container.on("mousemove", function(e) {
move(e);
}) $container.on('mouseup', function(e) {
end(e);
}); $container.on('mouseleave', function(e) {
end(e);
}); document.addEventListener("keydown", keydown); function start(e) {
var re1 = $(e.target); if (re1.is($rect)) {
return
} isDown = true;
$rect.width(0);
$rect.height(0);
$rect.show(); startPoint = {
y: e.pageY - parentOffset.top,
x: e.pageX - parentOffset.left
}; $rect.css({
top: startPoint.y + 'px',
left: + startPoint.x + 'px'
});
} function move(e) {
if (isDown) {
$rect.width((e.pageX - parentOffset.left) - startPoint.x);
$rect.height((e.pageY - parentOffset.top )- startPoint.y);
}
} function end(e) {
if (!isDown) {
return;
} var x = e.pageX > (parentOffset.left + $container.width()) ? parentOffset.left + $container.width() : e.pageX;
var y = e.pageY > (parentOffset.top + $container.height()) ? parentOffset.top + $container.height() : e.pageY; $rect.width((x - parentOffset.left) - startPoint.x);
$rect.height((y - parentOffset.top )- startPoint.y);
isDown = false;
startPoint = null; save();
} function save() {
console.log({
top: $rect.position().top,
left: $rect.position().left,
width: $rect.width(),
height: $rect.height()
})
} function edit() {
console.log('edit');
} function goNext() {
console.log('goNext');
} function goPrev() {
console.log('goPrev');
} function keydown(e) {
if (e.key === 'd' || e.key === "Delete") {
reset();
e.preventDefault();
} else if (e.key === 'e') {
edit();
e.preventDefault();
} else if (e.key === 'ArrowRight' || e.key === "f" || e.key === "Enter") {
goNext();
e.preventDefault();
} else if (e.key === 'ArrowLeft' || e.key === "s") {
goPrev();
e.preventDefault();
}
} </script>
</body>
</html>

最新文章

  1. 1125mysqbinlog日志
  2. MySQL_监控用户下单地址没有就近仓库配送情况_20161215
  3. VS报错:The build tools for v140 (Platform Toolset = &#39;v140&#39;) cannot be found
  4. spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法
  5. atitit.编辑表单的实现最佳实践dwr jq easyui
  6. wordpress nginx 开启链接为静态
  7. JAVA 锁
  8. ExtJS和AngularJS比较
  9. Xcode和IOS模拟器
  10. javascript 特性
  11. 笔记一、初识 Javascript
  12. DSR on Openstack POC
  13. RxJava系列4(过滤操作符)
  14. (NO.00002)iOS游戏精灵战争雏形(四)
  15. :only-child和:only-of-type选择器的比较
  16. export,export default和import的区别以及用法
  17. boost 学习笔记 2: timer
  18. elasticsearch安装步骤
  19. php 导入excel文件
  20. linux 比较两个文件夹不同 (diff命令, md5列表)

热门文章

  1. 多线程辅助类-CountDownLatch的用法
  2. Linux下监控网卡流量的软件iftop
  3. 七号信令中TUP协议的主要消息和故障问题
  4. 安装tensorflow-gpu出现的问题
  5. SNMP:简单网络管理协议
  6. python2代码批量转为python3代码
  7. css animation 简写和参数
  8. HDOJ-2160
  9. 1、webpack课程介绍
  10. Windows下启动停止Oracle11g服务-为解决系统变慢而生