angularjs指令弹框点击空白处隐藏及常规方法

效果图展示:

 

第一种方法:angularjs自定义指令:

指令:

   app.directive('onBlankHide', function () {
        return {
            restrict: 'A',
            scope: {
                pop: '='
            },
            link: function (scope, element, attr) {
                element.on('click', function (e) {
                    $('#' + scope.pop).fadeIn();
                    $(document).click(function () {
                        $('#' + scope.pop).hide();
                    });
                     //阻止底层冒泡
                     e.stopPropagation();
                });

                $('#' + scope.pop).click(function (e) {
                    //阻止底层冒泡
                    e.stopPropagation();
                })
            }
        }
    });

调用方法:

<input id="btn" type="button" value="显示DIV" on-blank-hide pop="'myDiv'" />
    <div id="myDiv">
        This is a div;
    </div>

说明:pop是向指令传递的值,表示关联弹框的id名,不要忘‘’

完整栗子(代码可直接copy):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
    <title>Document</title>
    <style type="text/css">
        body {
            background-color: #999999;
        }

        #myDiv {
            background-color: #FFFFFF; width: 250px; height: 250px; display: none; } </style> </head> <body ng-app="myApp"> <input id="btn" type="button" value="显示DIV" on-blank-hide pop="'myDiv'" /> <div id="myDiv"> This is a div; </div> </body> <script type="text/javascript"> var app = angular.module("myApp", []); app.directive('onBlankHide', function () { return { restrict: 'A', scope: { pop: '=' }, link: function (scope, element, attr) { element.on('click', function (e) { $('#' + scope.pop).fadeIn(); $(document).click(function () { $('#' + scope.pop).hide(); }); //阻止底层冒泡  e.stopPropagation(); }); $('#' + scope.pop).click(function (e) { //阻止底层冒泡  e.stopPropagation(); }) } } }); </script> </html>

 

常规方法(代码可直接copy):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <title>Document</title>
    <style type="text/css">
        body {
            background-color: #999999;
        }

        #myDiv {
            background-color: #FFFFFF;
            width: 250px;
            height: 250px;
            display: none;
        }
    </style>
</head>

<body>
    <input id="btn" type="button" value="显示DIV" />
    <div id="myDiv">
        This is a div;
    </div>
</body>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function (event) {
            $('#myDiv').fadeIn();
            $(document).one("click",
                function () { //对document绑定一个影藏Div方法
                    $('#myDiv').hide();
                });

            event.stopPropagation(); //阻止事件向上冒泡
        });
        $('#myDiv').click(function (event) {

            event.stopPropagation(); //阻止事件向上冒泡
        });
    });
  
</script>

</html>

 

深夜还在码代码,但是我总觉得是值得的,因为我又懂了一个知识点。

 

 

 

 

 

 

转载于:https://www.cnblogs.com/DZzzz/p/10242108.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值