Vue3点击按钮展开表单且点击阴影部分和关闭按钮关闭

Vue3点击按钮展开表单且点击阴影部分和关闭按钮关闭


一、案例

<template>
  <div>
    <button @click="toggleForm">打开表单</button>
    <div class="overlay" v-if="showForm" @click="closeForm">
      <div class="form-container" @click.stop>
        <form-popup @close="closeForm" />
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import FormPopup from './FormPopup.vue'; // 假设表单组件在 ./FormPopup.vue 中

export default {
  components: {
    FormPopup
  },
  setup() {
    const showForm = ref(false);

    const toggleForm = () => {
      showForm.value = !showForm.value;
    };

    const closeForm = () => {
      showForm.value = false;
    };

    return {
      showForm,
      toggleForm,
      closeForm
    };
  }
};
</script>

<style>
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
</style>

在这个代码中,我们在 .form-container 上添加了 @click.stop 事件修饰符,这样点击表单内容时就不会触发父元素的点击事件,只有点击遮罩层时才会触发关闭表单的事件。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的示例代码,使用了Vue3的Composition API来实现: ```html <template> <div class="login-form"> <h2>登录</h2> <form @submit.prevent="submitForm"> <div class="form-group"> <label for="username">账号</label> <input type="text" id="username" v-model="username" placeholder="请输入11位手机号码" /> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" id="password" v-model="password" placeholder="请输入8-20位数字和字母" /> </div> <div class="form-group"> <button type="submit">登录</button> </div> </form> <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const username = ref(''); const password = ref(''); const errorMessage = ref(''); const submitForm = () => { if (!username.value || !password.value) { errorMessage.value = '账号或密码不能为空!'; return; } const phoneRegex = /^1\d{10}$/; const passwordRegex = /^[a-zA-Z0-9]{8,20}$/; if (!phoneRegex.test(username.value) || !passwordRegex.test(password.value)) { errorMessage.value = '用户名和密码错误!'; return; } // TODO: 发送登录请求 errorMessage.value = ''; }; return { username, password, errorMessage, submitForm, }; }, }; </script> <style scoped> .login-form { max-width: 400px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); } .form-group { margin-bottom: 10px; } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"] { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 3px; } button[type="submit"] { background-color: #4CAF50; color: #fff; padding: 8px 16px; border: none; border-radius: 3px; cursor: pointer; transition: background-color 0.2s; } button[type="submit"]:hover { background-color: #3e8e41; } .error-message { color: red; margin-top: 10px; } </style> ``` 在这个示例代码中,我们使用了Vue3的Composition API来编写组件逻辑。`ref`函数用于创建响应式数据,我们使用它来创建`username`、`password`和`errorMessage`变量。 我们在模板中使用了`v-model`指令来双向绑定输入框的值,使用`@submit`事件监听表单提交事件,调用`submitForm`方法来处理登录逻辑。 `submitForm`方法首先检查账号和密码是否为空,如果为空则设置`errorMessage`为相应的提示信息并返回。然后使用正则表达式检查账号和密码是否符合格式要求,如果不符合则同样设置`errorMessage`为相应的提示信息并返回。 最后,如果账号和密码都符合要求,我们可以发送登录请求,并清空`errorMessage`以便下次使用。如果你需要根据具体的业务逻辑来处理登录请求,可以在TODO注释下面编写相应的代码。 最后,我们使用了一些简单的CSS样式来美化表单,包括设置表单宽度、边框、圆角、阴影等等。同时,我们也为错误提示信息设置了一个红色颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

和烨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值