Home >Web Front-end >JS Tutorial >angularjs创建弹出框实现拖动效果

angularjs创建弹出框实现拖动效果

WBOY
WBOYOriginal
2016-06-01 09:54:411695browse

本文实例介绍了angularjs创建弹出框实现拖动效果的相关代码,项目中需要将angular-ui-bootstrap中用到的弹出框,使之可拖动,分享给大家供大家参考,具体内容如下。

由于源文件中没有实现,需要自己实现指令,以下即为该指令,亲测可以实现。

<code class="language-javascript">.directive('draggable', ['$document', function($document) {
        return function(scope, element, attr) {
        var startX = 0, startY = 0, x = 0, y = 0;
        element= angular.element(document.getElementsByClassName("modal-dialog")); 
        element.css({
          position: 'relative',
          cursor: 'move'
        });
 
        element.on('mousedown', function(event) {
          // Prevent default dragging of selected content
          event.preventDefault();
          startX = event.pageX - x;
          startY = event.pageY - y;
          $document.on('mousemove', mousemove);
          $document.on('mouseup', mouseup);
        });
 
        function mousemove(event) {
          y = event.pageY - startY;
          x = event.pageX - startX;
          element.css({
          top: y + 'px',
          left: x + 'px'
          });
        }
 
        function mouseup() {
          $document.off('mousemove', mousemove);
          $document.off('mouseup', mouseup);
        }
        };
  }]);</code>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn