>  기사  >  웹 프론트엔드  >  AngularJS에서 드래그 앤 드롭 기능을 구현하는 방법

AngularJS에서 드래그 앤 드롭 기능을 구현하는 방법

亚连
亚连원래의
2018-06-14 11:28:292053검색

이 글에서는 주로 AngularJS에서 구현한 간단한 드래그 앤 드롭 기능을 소개하며, AngularJS 이벤트 응답과 페이지 요소 속성의 동적 동작과 관련된 구현 기술이 필요한 친구들이 참고할 수 있습니다.

이 글에서는 구현된 간단한 드래그 앤 드롭 기능을 설명합니다. AngularJS로. 참고용으로 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <meta charset="UTF-8">
    <title>www.jb51.net AngularJS拖拽</title>
    <style>
    *{
      padding:0;
      margin:0;
    }
      .wei{
        width:100px;
        height:100px;
        background: red;
        position:absolute;
        cursor: pointer;
        /*left:0;top:0;*/
      }
    </style>
  </head>
  <body ng-controller="show">
      <p class="wei" wei-yi data="true"></p>
      <p class="wei" wei-yi data="false"></p>
    <script src="jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="angular.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
      var app = angular.module(&#39;myApp&#39;,[]);
      //自定义属性
      app.directive("weiYi",function(){
        return{
          restrict :&#39;A&#39;,//A属性,E标签,C类名,D注释
          link :function(scope,element,attr){
            attr.data=angular.equals(attr.data,"true");
            //console.log(attr.data);
            console.log(element);
            element.on("mousedown",function(e){
              var that = $(this);
              console.log(attr.data);
              if(attr.data){
                $p=$("<p>");
                console.log($p);
                $p.css({"width":"100px","height":"100px","border": "2px dotted green","position":"absolute","left":that.offset().left,"top":that.offset().top});
                $p.appendTo($("body"));
              }
              var x=e.clientX-$(this).offset().left;
              var y=e.clientY-$(this).offset().top;
              //console.log(x+":"+y);
              $(document).on("mousemove",function(e){
                if(attr.data){
                  $p.css({"left":e.clientX-x,"top":e.clientY-y});
                }else{
                  that.css({"left":e.clientX-x,"top":e.clientY-y});
                }
              });
              $(document).on("mouseup",function(e){
                //console.log($p);
                $(document).off();
                if(attr.data){
                  that.css({"left":$p.offset().left,"top":$p.offset().top});
                  $p.remove();
                }
              })
            })
          }
        }
      });
      app.controller(&#39;show&#39;,[&#39;$scope&#39;,function(scope$){
      }]);
    </script>
  </body>
</html>

작동 효과는 다음과 같습니다.

위 내용은 모두를 위해 정리한 내용입니다. 앞으로 모든 사람에게 도움이 되기를 바랍니다.

관련 기사:

React Native Flexbox 레이아웃의 자세한 해석

vue 단일 파일의 참조 경로에 대한 방법은 무엇입니까?

Koa를 사용하여 Node.js를 통해 프로젝트 빌드

위 내용은 AngularJS에서 드래그 앤 드롭 기능을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.