dc8baa92541455497d33b2685ce6cfb18cfcc13b.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!--
  2. <drag v-show="dragVisible" @closeDrag="dragVisible = false"></drag>
  3. refName: ref名字 默认 drag+时间戳
  4. title: 标题 默认"详细信息"
  5. initTop: 初始top 默认"center"居中
  6. initLeft: 初始left 默认"center"居中
  7. initWidth: 初始宽度 默认500
  8. initHeight: 初始高度 默认350
  9. topMin: 距离顶部最小距离 默认0
  10. leftMin: 距离左侧最小距离 默认0
  11. widthMin: 最小宽度 默认300
  12. heightMin: 最小高度 默认200
  13. -->
  14. <template>
  15. <div
  16. class="draggableContainer"
  17. :ref="refName"
  18. :style="{
  19. top:initPosition.top + 'px',
  20. left:initPosition.left + 'px',
  21. width:initPosition.width + 'px',
  22. height:initPosition.height + 'px'
  23. }"
  24. >
  25. <div
  26. class="draggableTitleClass"
  27. @mousedown="mousedownfun"
  28. >
  29. {{title}}
  30. <i
  31. @click="closeResult"
  32. class="draggableClose el-icon-close"
  33. ></i>
  34. </div>
  35. <div
  36. style="display:flex;flex-direction;column;width:100%;background-color:rgba(255, 255, 255, 0.3);"
  37. :style="{'height':initPosition.height-31 + 'px'}"
  38. >
  39. <div
  40. @mousedown="mousedown_left"
  41. class="leftClass"
  42. ></div>
  43. <div class="slotClass">
  44. <slot></slot>
  45. </div>
  46. <div
  47. @mousedown="mousedown_right"
  48. class="rightClass"
  49. ></div>
  50. </div>
  51. <div
  52. @mousedown="mousedown_bottom"
  53. class="bottomClass"
  54. ></div>
  55. <img
  56. style="cursor:se-resize;position: inherit;width: 10px;height: 10px;bottom: 0px;right: 0px;"
  57. src="/src/assets/img/rightCorner.png"
  58. @mousedown="rightCornerFun"
  59. alt="缩放"
  60. >
  61. </div>
  62. </template>
  63. <script>
  64. export default {
  65. props: {
  66. refName: {
  67. type: String,
  68. default: "drag" + new Date().getTime()
  69. },
  70. title: {
  71. type: String,
  72. default: "详细信息"
  73. },
  74. initTop: {
  75. type: [String, Number],
  76. default: "center"
  77. },
  78. initLeft: {
  79. type: [String, Number],
  80. default: "center"
  81. },
  82. initWidth: {
  83. type: Number,
  84. default: 500
  85. },
  86. initHeight: {
  87. type: Number,
  88. default: 350
  89. },
  90. topMin: {
  91. type: Number,
  92. default: 0
  93. },
  94. leftMin: {
  95. type: Number,
  96. default: 0
  97. },
  98. widthMin: {
  99. type: Number,
  100. default: 300
  101. },
  102. heightMin: {
  103. type: Number,
  104. default: 200
  105. }
  106. },
  107. data() {
  108. return {
  109. heightMax: 0,
  110. widthMax: 0,
  111. initPosition: {
  112. top: 0,
  113. left: 0,
  114. width: 0,
  115. height: 0
  116. }
  117. };
  118. },
  119. created() {
  120. this.init();
  121. },
  122. methods: {
  123. init() {
  124. this.heightMax = window.innerHeight;
  125. this.widthMax = window.innerWidth;
  126. if (this.initTop == "center") {
  127. let top = (this.heightMax - this.initHeight) / 2;
  128. this.initPosition.top = top > this.topMin ? top : this.topMin;
  129. } else {
  130. this.initPosition.top = this.initTop;
  131. }
  132. if (this.initLeft == "center") {
  133. this.initPosition.left = (this.widthMax - this.initWidth) / 2;
  134. } else {
  135. this.initPosition.left = this.initLeft;
  136. }
  137. this.initPosition.width = this.initWidth;
  138. this.initPosition.height = this.initHeight;
  139. },
  140. closeResult() {
  141. this.$emit("closeDrag");
  142. },
  143. mousedownfun(e) {
  144. var that = this;
  145. var el = this.$refs[this.refName];
  146. var disx = e.pageX - el.offsetLeft;
  147. var disy = e.pageY - el.offsetTop;
  148. document.onmousemove = function(e) {
  149. var left = e.pageX - disx;
  150. var top = e.pageY - disy;
  151. if (left <= that.leftMin) {
  152. left = that.leftMin;
  153. } else if (left + el.offsetWidth > that.widthMax - 2) {
  154. left = that.widthMax - el.offsetWidth - 2;
  155. }
  156. if (top < that.topMin) {
  157. top = that.topMin;
  158. } else if (top + el.offsetHeight > that.heightMax - 2) {
  159. top = that.heightMax - el.offsetHeight - 2;
  160. }
  161. el.style.left = left + "px";
  162. el.style.top = top + "px";
  163. };
  164. document.onmouseup = this.onmouseupNow;
  165. },
  166. mousedown_bottom(e) {
  167. var that = this;
  168. var el = that.$refs[this.refName];
  169. document.onmousemove = function(e) {
  170. if (
  171. e.pageY - el.offsetTop > that.heightMin &&
  172. e.pageY < that.heightMax
  173. ) {
  174. var height = e.pageY - el.offsetTop;
  175. that.initPosition.height = height;
  176. }
  177. };
  178. document.onmouseup = this.onmouseupNow;
  179. },
  180. mousedown_left(e) {
  181. var that = this;
  182. var el = this.$refs[this.refName];
  183. document.onmousemove = function(e) {
  184. if (
  185. e.pageX > that.leftMin &&
  186. (el.offsetLeft > e.pageX || el.offsetWidth > that.widthMin)
  187. ) {
  188. that.initPosition.left = e.pageX;
  189. that.initPosition.width = el.offsetLeft + el.offsetWidth - e.pageX;
  190. }
  191. };
  192. document.onmouseup = this.onmouseupNow;
  193. },
  194. mousedown_right(e) {
  195. var that = this;
  196. var el = this.$refs[this.refName];
  197. document.onmousemove = function(e) {
  198. if (
  199. e.pageX - el.offsetLeft > that.widthMin &&
  200. e.pageX < that.widthMax
  201. ) {
  202. var width = e.pageX - el.offsetLeft;
  203. that.initPosition.width = width;
  204. }
  205. };
  206. document.onmouseup = this.onmouseupNow;
  207. },
  208. rightCornerFun(e) {
  209. var that = this;
  210. var el = this.$refs[this.refName];
  211. document.onmousemove = function(e) {
  212. if (
  213. e.pageX - el.offsetLeft > that.widthMin &&
  214. e.pageX < that.widthMax
  215. ) {
  216. var width = e.pageX - el.offsetLeft;
  217. that.initPosition.width = width;
  218. }
  219. if (
  220. e.pageY - el.offsetTop > that.heightMin &&
  221. e.pageY < that.heightMax
  222. ) {
  223. var height = e.pageY - el.offsetTop;
  224. that.initPosition.height = height;
  225. }
  226. };
  227. document.onmouseup = this.onmouseupNow;
  228. },
  229. onmouseupNow() {
  230. document.onmousemove = document.onmouseup = null;
  231. }
  232. }
  233. };
  234. </script>
  235. <style>
  236. .draggableContainer {
  237. position: absolute;
  238. display: flex;
  239. flex-direction: column;
  240. box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.3);
  241. border-radius: 7px 7px 0px 0px;
  242. z-index: 10;
  243. background-color: white;
  244. }
  245. .draggableTitleClass {
  246. position: relative;
  247. height: 30px;
  248. box-sizing: border-box;
  249. padding: 5px;
  250. text-align: center;
  251. color: white;
  252. background-color: #1862f7;
  253. background-image: url("/src/assets/img/rough-cloth-light.png");
  254. cursor: move;
  255. border-radius: 7px 7px 0px 0px;
  256. }
  257. .bottomClass {
  258. border-bottom: 2px solid rgb(78, 137, 243);
  259. cursor: n-resize;
  260. position: relative;
  261. }
  262. .leftClass {
  263. border-left: 2px solid rgb(78, 137, 243);
  264. cursor: w-resize;
  265. }
  266. .rightClass {
  267. border-right: 2px solid rgb(78, 137, 243);
  268. cursor: e-resize;
  269. }
  270. .slotClass {
  271. height: 100%;
  272. width: 100%;
  273. margin: 0 auto;
  274. overflow: auto;
  275. padding-bottom: 2px;
  276. background-color: white;
  277. }
  278. .draggableClose {
  279. position: absolute;
  280. top: calc(50% - 9px);
  281. right: 10px;
  282. color: white;
  283. cursor: pointer;
  284. font-size: large;
  285. transform: rotate(0deg);
  286. transition: transform 0.1s linear;
  287. }
  288. .draggableClose:hover {
  289. transform: rotate(90deg);
  290. }
  291. </style>