Dialog.vue 887 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <template>
  2. <div>
  3. <button @click="isShow = true">点我弹个窗</button>
  4. <teleport to="body">
  5. <div v-if="isShow" class="mask">
  6. <div class="dialog">
  7. <h3>我是一个弹窗</h3>
  8. <h4>一些内容</h4>
  9. <h4>一些内容</h4>
  10. <h4>一些内容</h4>
  11. <button @click="isShow = false">关闭弹窗</button>
  12. </div>
  13. </div>
  14. </teleport>
  15. </div>
  16. </template>
  17. <script>
  18. import {ref} from 'vue'
  19. export default {
  20. name:'Dialog',
  21. setup(){
  22. let isShow = ref(false)
  23. return {isShow}
  24. }
  25. }
  26. </script>
  27. <style>
  28. .mask{
  29. position: absolute;
  30. top: 0;bottom: 0;left: 0;right: 0;
  31. background-color: rgba(0, 0, 0, 0.5);
  32. }
  33. .dialog{
  34. position: absolute;
  35. top: 50%;
  36. left: 50%;
  37. transform: translate(-50%,-50%); /*表示相对于元素自身的移动距离 */
  38. text-align: center;
  39. width: 300px;
  40. height: 300px;
  41. background-color: green;
  42. }
  43. </style>