• 前端页面,使用 dom 鼠标拖拽画一个矩形和监听键盘


    <!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;
             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,
         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,
         $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>
    
    
  • 相关阅读:
    ubuntu1804隐藏顶部工作栏
    minilzo使用流程
    Linux内核设计与实现 总结笔记(第十六章)页高速缓存和页回写
    VS2015编译问题:模块对于 SAFESEH 映像是不安全的
    Linux内核设计与实现 总结笔记(第十五章)进程地址空间
    Linux内核设计与实现 总结笔记(第十四章)块I/O层
    Qt setStyleSheet
    Linux内核设计与实现 总结笔记(第十三章)虚拟文件系统
    Linux内核设计与实现 总结笔记(第十二章)内存管理
    Linux内核设计与实现 总结笔记(第十一章)定时器和时间管理
  • 原文地址:https://www.cnblogs.com/htoooth/p/8639446.html
Copyright © 2020-2023  润新知