Window.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div ref="main" class="main" @click="close" @mouseup.prevent.stop="onMouseUp" @mousemove.prevent.stop="onMouseMove">
  3. <div ref="windowBox" class="windowBox" @click.stop>
  4. <div class="window">
  5. <div ref="header" class="header" @mousedown.prevent.stop="onMouseDown"
  6. @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchmove.stop="onTouchMove">
  7. <span class="header-text"><slot name="header"></slot></span>
  8. <span class="close-button" @mousedown.stop @click="close"><i class="el-icon-close"></i></span>
  9. </div>
  10. <slot></slot>
  11. </div>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. //-----------------------------------------------------------------------------
  17. import Vue from 'vue';
  18. import Component from 'vue-class-component';
  19. export default @Component({
  20. props: {
  21. height: { type: String, default: '100%' },
  22. width: { type: String, default: '100%' },
  23. }
  24. })
  25. class Window extends Vue {
  26. init() {
  27. this.$nextTick(() => {
  28. this.$refs.windowBox.style.height = this.height;
  29. this.$refs.windowBox.style.width = this.width;
  30. const left = (this.$refs.main.offsetWidth - this.$refs.windowBox.offsetWidth)/2;
  31. const top = (this.$refs.main.offsetHeight - this.$refs.windowBox.offsetHeight)/2;
  32. this.$refs.windowBox.style.left = (left > 0 ? left : 0) + 'px';
  33. this.$refs.windowBox.style.top = (top > 0 ? top : 0) + 'px';
  34. });
  35. }
  36. onMouseDown(event) {
  37. if (this.$isMobileDevice)
  38. return;
  39. if (event.button == 0) {
  40. this.$refs.header.style.cursor = 'move';
  41. this.startX = event.screenX;
  42. this.startY = event.screenY;
  43. this.moving = true;
  44. }
  45. }
  46. onMouseUp(event) {
  47. if (event.button == 0) {
  48. this.$refs.header.style.cursor = 'default';
  49. this.moving = false;
  50. }
  51. }
  52. onMouseMove(event) {
  53. if (this.moving) {
  54. const deltaX = event.screenX - this.startX;
  55. const deltaY = event.screenY - this.startY;
  56. this.startX = event.screenX;
  57. this.startY = event.screenY;
  58. this.$refs.windowBox.style.left = (this.$refs.windowBox.offsetLeft + deltaX) + 'px';
  59. this.$refs.windowBox.style.top = (this.$refs.windowBox.offsetTop + deltaY) + 'px';
  60. }
  61. }
  62. onTouchStart(event) {
  63. if (!this.$isMobileDevice)
  64. return;
  65. if (event.touches.length == 1) {
  66. const touch = event.touches[0];
  67. this.$refs.header.style.cursor = 'move';
  68. this.startX = touch.screenX;
  69. this.startY = touch.screenY;
  70. this.moving = true;
  71. }
  72. }
  73. onTouchMove(event) {
  74. if (!this.$isMobileDevice)
  75. return;
  76. if (event.touches.length == 1 && this.moving) {
  77. const touch = event.touches[0];
  78. const deltaX = touch.screenX - this.startX;
  79. const deltaY = touch.screenY - this.startY;
  80. this.startX = touch.screenX;
  81. this.startY = touch.screenY;
  82. this.$refs.windowBox.style.left = (this.$refs.windowBox.offsetLeft + deltaX) + 'px';
  83. this.$refs.windowBox.style.top = (this.$refs.windowBox.offsetTop + deltaY) + 'px';
  84. }
  85. }
  86. onTouchEnd() {
  87. if (!this.$isMobileDevice)
  88. return;
  89. this.$refs.header.style.cursor = 'default';
  90. this.moving = false;
  91. }
  92. close() {
  93. if (!this.moving)
  94. this.$emit('close');
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. </script>
  99. <style scoped>
  100. .main {
  101. position: absolute;
  102. width: 100%;
  103. height: 100%;
  104. z-index: 50;
  105. }
  106. .windowBox {
  107. position: absolute;
  108. display: flex;
  109. height: 100%;
  110. width: 100%;
  111. }
  112. .window {
  113. flex: 1;
  114. display: flex;
  115. flex-direction: column;
  116. margin: 10px;
  117. background-color: #ffffff;
  118. border: 3px double black;
  119. border-radius: 4px;
  120. box-shadow: 3px 3px 5px black;
  121. }
  122. .header {
  123. display: flex;
  124. justify-content: flex-end;
  125. background-color: #59B04F;
  126. align-items: center;
  127. height: 30px;
  128. }
  129. .header-text {
  130. flex: 1;
  131. margin-left: 10px;
  132. margin-right: 10px;
  133. }
  134. .close-button {
  135. display: flex;
  136. justify-content: center;
  137. align-items: center;
  138. width: 30px;
  139. height: 30px;
  140. cursor: pointer;
  141. }
  142. .close-button:hover {
  143. background-color: #69C05F;
  144. }
  145. </style>