停止在点击模态框外部时关闭模态框。

huangapple go评论77阅读模式
英文:

Stop closing modal when pressing outside modal

问题

我正在尝试创建一个弹出式模态框,在这个模态框中,我不希望能够通过点击模态窗口外部来关闭模态框。我已经搜索并尝试了不同的解决方案,包括使用背景遮罩,但似乎都没有奏效。感谢任何帮助!我用于模态框的代码如下:

<template>
  <div>
    <transition name="modal">
      <div v-if="isOpen">
        <div class="overlay" @click.self="isOpen = false;">
          <div class="modal">
            <h1>模态框标题</h1>
            <p>这是我使用 vue.js 创建的第一个模态框</p>
          </div>
        </div>
      </div>
    </transition>
    <button @click="isOpen = !isOpen;">
      {{ isOpen ? "关闭" : "打开" }} 模态框
    </button>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      isOpen: false
    };
  }
};
</script>

希望这对你有所帮助!

英文:

I'm trying to make a popup modal where I don't want to be able to close the modal by clicking outside the modal window. I've searched and tried different solutions with backdrop but nothing seems to work. Would appreciate any help! The code I have for the modal is:

&lt;template&gt;
  &lt;div&gt;
    &lt;transition name=&quot;modal&quot;&gt;
      &lt;div v-if=&quot;isOpen&quot;&gt;
        &lt;div class=&quot;overlay&quot; @click.self=&quot;isOpen = false;&quot;&gt;
          &lt;div class=&quot;modal&quot;&gt;
            &lt;h1&gt;Modal heading&lt;/h1&gt;
            &lt;p&gt;This my first modal using vue.js&lt;/p&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/transition&gt;
    &lt;button @click=&quot;isOpen = !isOpen;&quot;&gt;
      {{ isOpen ? &quot;Close&quot; : &quot;Open&quot; }} modal
    &lt;/button&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data: function() {
    return {
      isOpen: false
    };
  }
};
&lt;/script&gt;

答案1

得分: 1

点击事件@click被放置在覆盖层上,我推测覆盖层位于模态框后方。这部分代码在点击覆盖层时关闭模态框。

@click.self="isOpen = false;"

只需移除上述代码。您可以将其放置在其他组件上,例如关闭按钮、按钮,或以任何您希望的方式触发关闭操作。

英文:

The @click event is placed on the overlay, which I presume is positioned behind the modal. This part closes the modal when clicking on the overlay layer.

@click.self=&quot;isOpen = false;&quot;

Just remove the code above. You can place it on another component; like a closing cross, button, or trigger the closing in any way you want.

huangapple
  • 本文由 发表于 2020年1月3日 19:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定