Vue v-for pop-up box, how to click outside the pop-up box to hide the pop-up box, such as the following figure and code

< H2 > question < / H2 >

Click the black button, and the pop-up box shows
Click outside the pop-up box to hide the pop-up box

< H2 > Picture < / H2 >

clipboard.png

< H2 > code, you can run < / H2 > directly.
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Vue  - (runoob.com)</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in data1">
                <span @click="toggle(index)"></span>
                <span>{{item.id}}</span>--
                <span>{{item.name}}</span>
                <div v-show="(cIndex == index) && dialog">dialog</div>
            </li>
        </ul>
    </div>

    <script>
        new Vue({
            el: "-sharpapp",
            data: {
                dialog:false,
                cIndex: -1,
                data1: [
                    { id: 1, name: "a" },
                    { id: 2, name: "b" },
                    { id: 3, name: "c" },
                    { id: 4, name: "d" },
                ]
            },
            methods:{
                toggle(index){
                    this.cIndex = index;
                    this.dialog = !this.dialog;
                }
            }
        })
    </script>
</body>

</html>
<style>
    ul,
    li {
        list-style: none;
    }

    li {
        position: relative;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid -sharp000;
    }

    li span:first-child {
        display: inline-block;
        width: 10px;
        height: 10px;
        background-color: -sharp000;
    }
    li div{
        position: absolute;
        left: 20px;
        top: 0;
        z-index: 1000;
        width: 100px;
        height: 100px;
        border: 1px solid red;
        background-color: -sharpfff;
    }
</style>
After
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Vue  - (runoob.com)</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in data1">
                <span @click.stop="toggle(index)"></span>
                <span>{{item.id}}</span>--
                <span>{{item.name}}</span>
                <div v-show="(cIndex == index) && dialog" class="modalDiaLog">dialog</div>
            </li>
        </ul>
    </div>

    <script>
        new Vue({
            el: '-sharpapp',
            data: {
                dialog:false,
                cIndex: -1,
                data1: [
                    { id: 1, name: "a" },
                    { id: 2, name: "b" },
                    { id: 3, name: "c" },
                    { id: 4, name: "d" },
                ]
            },
            mounted(){
                document.addEventListener('click', (e)=> {
                    if (e.target.className != 'modalDiaLog') {
                        this.dialog = false;
                    }
                })
            },
            methods:{
                toggle(index){
                    this.cIndex = index;
                    this.dialog = !this.dialog;
                }
            }
        })
    </script>
</body>

</html>
<style>
    ul,
    li {
        list-style: none;
    }

    li {
        position: relative;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid -sharp000;
    }

    li span:first-child {
        display: inline-block;
        width: 10px;
        height: 10px;
        background-color: -sharp000;
    }
    li div{
        position: absolute;
        left: 20px;
        top: 0;
        z-index: 1000;
        width: 100px;
        height: 100px;
        border: 1px solid red;
        background-color: -sharpfff;
    }
</style>

pop-up window, document listen click event close dialog


document listen click event
e.targetmonitor = pop-up window, close

Menu