欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJS实现的鼠标拖动画矩形框示例【可兼容IE8】

程序员文章站 2022-06-24 17:30:49
本文实例讲述了angularjs实现的鼠标拖动画矩形框。分享给大家供大家参考,具体如下:

本文实例讲述了angularjs实现的鼠标拖动画矩形框。分享给大家供大家参考,具体如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
  <style>
    html {color: #000;overflow-y: scoll;overflow: -moz-scrollbars-vertical}
    body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }
    #longzhoufeng {
      position: relative;
      left: 0;
      top: 0;
      margin: 0 auto;
      padding: 0;
      width: 800px;
      min-height: 800px;
      overflow: auto;
      height: 1000px;
      background-color: #e9e9e9;
    }
    .dashed-box {
      position: absolute;
      border: 1px dashed red;
      width: 0px;
      height: 0px;
      left: 0px;
      top: 0px;
      overflow: hidden;
    }
    #moving_box {
      border: 2px solid red;
    }
    .question-box {
      position: absolute;
      /* older safari/chrome browsers */
      -webkit-opacity: 0.5;
      /* netscape and older than firefox 0.9 */
      -moz-opacity: 0.5;
      /* safari 1.x (pre webkit!) 老式khtml内核的safari浏览器*/
      -khtml-opacity: 0.5;
      /* ie9 + etc...modern browsers */
      opacity: .5;
      /* ie 4-9 */
      filter: alpha(opacity=50);
      /*this works in ie 8 & 9 too*/
      -ms-filter: "progid:dximagetransform.microsoft.alpha(opacity=50)";
      /*ie4-ie9*/
      filter: progid:dximagetransform.microsoft.alpha(opacity=50);
    }
    .question-border {
      border: 2px dashed red;
      overflow: hidden;
      z-index: 1;
    }
    .del-box {
      width: 20px;
      height: 20px;
      float: right;
      color: #fff;
      position: relative;
      margin-top: 1px;
      margin-right: 1px;
      z-index: 99;
      background-color: red;
    }
    .active-border {
      border: 2px solid red;
      -webkit-opacity: 0.5;
      /* netscape and older than firefox 0.9 */
      -moz-opacity: 0.5;
      /* safari 1.x (pre webkit!) 老式khtml内核的safari浏览器*/
      -khtml-opacity: 0.5;
      /* ie9 + etc...modern browsers */
      opacity: .5;
      /* ie 4-9 */
      filter: alpha(opacity=50);
      /*this works in ie 8 & 9 too*/
      -ms-filter: "progid:dximagetransform.microsoft.alpha(opacity=50)";
      /*ie4-ie9*/
      filter: progid:dximagetransform.microsoft.alpha(opacity=50);
    }
    .box-background-image {
      position: absolute;
      width: 1000px;
      height: 1600px;
    }
  </style>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/angular.js/1.0.5/angular.min.js"></script>
  <script>
    var app = angular.module('miniapp', []);
    function appcontroller($scope, $timeout) {
      //阻止默认行为
      function stopdefault(e) {
        if(e && e.preventdefault)
          e.preventdefault();
        else
          window.event.returnvalue = false;
        return false;
      }
      $scope.bgimg = '1.jpg';
      $scope.getboxpicsize = function() {
        $scope.bgwidth = angular.element(".box-background-image").width();
        $scope.bgheight = angular.element(".box-background-image").height();
      }
      ///////////////////////////////////////
      var wid = "num";
      var index = 0;
      var target = null;
      var startx = 0, starty = 0;
      var flag = false;
      var startl = 0, startt = 0, boxheight = 0, boxwidth = 0;
      var boxobj = document.getelementbyid("longzhoufeng");
      var frame = $("#longzhoufeng");
      var framebox = {
        "pos": frame.offset(),
        "width": frame.outerwidth(),
        "height": frame.outerheight()
      }
      var newmarkpos = {
        "left": startl,//按下时鼠标距离的左边的距离
        "top": startt//按下时鼠标距离的上边的距离
      }
      var myevent = $scope.myevent = {
        //鼠标点击
        mousedown: function(e) {
          flag = true;
          var e = window.event || e;
          target = e.target || e.srcelement; //获取document 对象的引用
          //e.pagey,e.pagex兼容
          if(target.src) {
            stopdefault(e)
          }
          var scrolltop = boxobj.scrolltop;
          var scrollleft = boxobj.scrollleft;
          var epagex = e.clientx + scrollleft;
          var epagey = e.clienty + scrolltop;
          //按下时鼠标距离页面的距离
          startx = epagex;
          starty = epagey;
          //按下时鼠标距离的左边和上边的距离
          startl = startx - framebox.pos.left;
          startt = starty - framebox.pos.top;
          index++;
          var div = document.createelement("div");
          div.id = wid + index;
          div.classname = "dashed-box";
          boxobj.appendchild(div);
          div = null;
          // 如果鼠标在 box 上被按下
          if(target.classname.match(/del-box/i)) {
            // 允许拖动
            flag = false;
            // 设置当前 box 的 id 为 moving_box
            var movingbox = document.getelementbyid("moving_box")
            if(movingbox !== null) {
              movingbox.removeattribute("id");
            }
            target.id = "moving_box";
            // removebox(target);
          } else {
            var div = document.createelement("div");
            div.id = wid + index;
            div.classname = "dashed-box";
            boxobj.appendchild(div);
            div = null;
          }
        },
        //鼠标离开
        mouseup: function(e) {
          var e = window.event || e;
          if(boxwidth >= 1 || boxheight >= 1) {
            boxobj.removechild(dragbox(wid + index));
            index++;
            var div = document.createelement("div");
            div.classname = "question-box question-border";
            div.style.left = newmarkpos.left + "px";
            div.style.top = newmarkpos.top + "px";
            div.style.width = boxwidth + "px";
            div.style.height = boxheight + "px";
            boxobj.appendchild(div);
            div = null;
            boxwidth = 0;
            boxheight = 0;
          } else {
            if(flag) {
              boxobj.removechild(dragbox(wid + index));
            }
          }
          flag = false;
        },
        //鼠标移动
        mousemove: function(e) {
          var e = window.event || e;
          stopdefault(e)
          if(flag) {
            var scrolltop = boxobj.scrolltop;
            var scrollleft = boxobj.scrollleft;
            var epx = e.clientx + scrollleft;
            var epy = e.clienty + scrolltop;
            var disw = epx - startx;
            var dish = epy - starty;
            var l = startl + disw;
            var t = startt + dish;
            if(disw > 0) {
              if(l >= 0) {
                boxwidth = disw
              }
              newmarkpos.left = startl;
            } else {
              if(l <= 0) {
                l = 0;
                boxwidth = startl;
              }
              boxwidth = (startl - l);
              newmarkpos.left = l;
            }
            if(dish > 0) {
              if(t >= 0) {
                boxheight = dish
              }
              newmarkpos.top = startt;
            } else {
              if(t <= 0) {
                t = 0;
                boxheight = startt;
              }
              boxheight = (startt - t)
              newmarkpos.top = t;
            }
            dragbox(wid + index).style.left = newmarkpos.left + "px";
            dragbox(wid + index).style.top = newmarkpos.top + "px";
            dragbox(wid + index).style.width = math.abs(boxwidth) + "px";
            dragbox(wid + index).style.height = math.abs(boxheight) + "px";
            if(e.srcelement.src) {
              stopdefault(e)
            }
          }
        }
      }
      var dragbox = function(id) {
        return document.getelementbyid(id);
      }
    }
    //等待图片加载完成
    app.directive("loadimage", function() {
      return {
        restrict: 'a',
        link: function(scope, element, attrs) {
          element.bind('load', function() {
            scope.$apply(attrs.loadimage);
          });
        }
      }
    })
  </script>
</head>
<body>
<div ng-app="miniapp" ng-controller="appcontroller">
  <div class="main">
    <div id="longzhoufeng" ng-mousedown="myevent.mousedown($event)" ng-mouseup="myevent.mouseup($event)" ng-mousemove="myevent.mousemove($event)">
      <img ng-src="{{bgimg}}" class="box-background-image" load-image="getboxpicsize()">
    </div>
  </div>
</div>
</body>
</html>

感兴趣的朋友可以使用在线html/css/javascript代码运行工具http://tools.jb51.net/code/htmljsrun测试上述代码运行效果。