zoukankan      html  css  js  c++  java
  • angularjs 选项卡 --- 自定义属性

    <!DOCTYPE html>
    <html lang="en" ng-app="myApp">
    <head>
        <meta charset="UTF-8" />
        <title>Document</title>
        <style>
        #div1 { position:absolute; left: 0; top: 0; 150px; height:150px; border:1px red solid; background: red; cursor: pointer;}
        </style>
        <script src="jquery-1.11.1.js"></script>
        <script src="angular.min.js" ></script>
        <script type="text/javascript"> 
    
            var m1 = angular.module('myApp', []);
    
            // 指令是可以复用的
            m1.directive('myDrag', function() {
    
                // scope: 独立作用域, 只在当前标签起作用, 跟外面没有关系
                // 自定义指令内部数据的绑定方式, 共享的
                // @: 绑定的普通字符串 , = :解析的是数据, &: 绑定的是父级函数的传递方式
                // 属性指令跟模板没有太大的关系
                return {
                    restrict : 'A',   
                    link: function( scope, ele, attr ) {
                       var disX = 0, disY = 0;
                       attr.myDrag = angular.equals(attr.myDrag, 'true');
                       // alert( typeof attr.myDrag);
                       
                       ele.on('mousedown', function(ev) {
                            var This = this;
                            
                            disX = ev.pageX - $(this).offset().left;
                            disY = ev.pageY - $(this).offset().top;
    
                            if(attr.myDrag) {
                                var $line = $('<div>');
                                $line.css({ width : $(this).outerWidth() , height : $(this).outerHeight() , position : 'absolute' , left : $(this).offset().left , top : $(this).offset().top , border : '1px gray dotted'});
                                $('body').append($line);
                            }
    
                            $(document).on('mousemove', function(ev) {
                                // console.log($(this).get(0).tagName);
                                // 
                                if(attr.myDrag) {
                                    $line.css('left',ev.pageX - disX);
                                    $line.css('top',ev.pageY - disY);
    
                                } else {
                                    $(This).css('left', ev.pageX - disX);
                                    $(This).css('top', ev.pageY - disY);
                                }
                              
                            });
    
                            $(document).on('mouseup', function() {
                                $(document).off();
                                 if(attr.myDrag) {
                                    $(This).css('left',$line.offset().left);
                                    $(This).css('top',$line.offset().top);
                                    $line.remove();
                                } 
                            });
    
                            return false;
                       });
    
                    }
    
                };
    
            });
            m1.controller('tab', ['$scope', function($scope) {
                 // $scope.age = 101;
    
            }]);
    
        </script>
    </head>
    <body ng-controller="tab">
        <div id="div1" my-drag="true"></div>
    </body>
    </html>
    

      

  • 相关阅读:
    php配置GD库
    Linux 安装 Apache2+php5+gd+freetype2
    gd库
    数组和链表的区别
    python 整数中1出现的次数
    python栈--字符串反转,括号匹配
    Linux基础知识
    操作系统
    后台面试问题
    python 面向对象
  • 原文地址:https://www.cnblogs.com/zsongs/p/5573566.html
Copyright © 2011-2022 走看看