经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTMLCSS » CSS » 查看文章
基于Vue实现图片在指定区域内移动
来源:cnblogs  作者:JioJun  时间:2018/11/11 10:19:03  对本文有异议

当图片比要显示的区域大时,需要将多余的部分隐藏掉,我们可以通过绝对定位来实现,并通过动态修改图片的left值和top值从而实现图片的移动。具体实现效果如下图,如果我们移动的是div 实现思路相仿。

未标题-1

此处需要注意的是

我们在移动图片时,需要通过draggable="false" 将图片的 <img src="/static/pano-dev/test/image-2.jpg" draggable="false" />,否则按下鼠标监听onmousemove事件时监听不到

然后还需要禁用图片的选中css 

  1. /*禁止元素选中*/
  2. -moz-user-select: none; /*火狐*/
  3. -webkit-user-select: none; /*webkit浏览器*/
  4. -ms-user-select: none; /*IE10*/
  5. -khtml-user-select: none; /*早期浏览器*/
  6. user-select: none;

 

Vue 代码

  1. <style lang="less" scoped>
  2. @import url("./test.less");
  3. </style>
  4. <template>
  5. <div class="page">
  6. <div class="image-move-wapper">
  7. <div class="image-show-box" ref="imageShowBox">
  8. <div class="drag-container" ref="dragContainer" :style="'left:' + dragContainer.newPoint.left+'px; top:' + dragContainer.newPoint.top+'px'" @mousedown="DragContainerMouseDown">
  9. <div class="drag-image-box">
  10. <img src="/static/pano-dev/test/image-2.jpg" draggable="false" />
  11. <div class="point" style="left:10%; top:10%" @mousedown="PointMouseDown"></div>
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. dragContainer: {
  23. box: {
  24. w: 0,
  25. h: 0
  26. },
  27. point: {
  28. left: 0,
  29. top: 0
  30. },
  31. newPoint: {
  32. left: 0,
  33. top: 0
  34. }
  35. },
  36. mousePoint: {
  37. x: 0,
  38. y: 0
  39. },
  40. imageShowBox: {
  41. box: {
  42. w: 0,
  43. h: 0
  44. },
  45. dragcheck: {
  46. h: true,
  47. v: true
  48. }
  49. }
  50. };
  51. },
  52. updated() {
  53. let _this = this;
  54. // 确保DOM元素已经渲染成功,然后获取拖拽容器和显示区域的大小
  55. _this.$nextTick(function() {
  56. _this.dragContainer.box = {
  57. w: _this.$refs.dragContainer.offsetWidth,
  58. h: _this.$refs.dragContainer.offsetHeight
  59. };
  60.  
  61. _this.imageShowBox.box = {
  62. w: _this.$refs.imageShowBox.offsetWidth,
  63. h: _this.$refs.imageShowBox.offsetHeight
  64. };
  65.  
  66. // 判断是否允许拖拽
  67. _this.imageShowBox.dragcheck = {
  68. h: _this.imageShowBox.box.w > _this.dragContainer.box.w ? false : true,
  69. v: _this.imageShowBox.box.h > _this.dragContainer.box.h ? false : true
  70. };
  71. });
  72. },
  73. methods: {
  74. // 鼠标在拖拽容器中按下时触发
  75. DragContainerMouseDown(e) {
  76. const _this = this;
  77. // 记录鼠标点击时的初始坐标
  78. this.mousePoint = {
  79. x: e.clientX,
  80. y: e.clientY
  81. };
  82. _this.dragContainer.point = _this.dragContainer.newPoint;
  83. document.body.onmousemove = _this.DragContainerMouseMove;
  84. document.onmouseup = _this.DragContainerMouseUp;
  85. return false;
  86. },
  87. // 容器拖拽时触发
  88. DragContainerMouseMove(e) {
  89. const _this = this;
  90. // 鼠标偏移量 = 初始坐标 - 当前坐标
  91. let dx = e.clientX - _this.mousePoint.x;
  92. let dy = e.clientY - _this.mousePoint.y;
  93.  
  94. // 获取拖拽容器移动后的left和top值
  95. if (_this.imageShowBox.dragcheck.h)
  96. var newx = dx > 0 ? Math.min(0, _this.dragContainer.point.left + dx) : Math.max(- _this.dragContainer.box.w + _this.imageShowBox.box.w, _this.dragContainer.point.left + dx );
  97. if (_this.imageShowBox.dragcheck.v)
  98. var newy = dy > 0 ? Math.min(0, _this.dragContainer.point.top + dy) : Math.max(- _this.dragContainer.box.h + _this.imageShowBox.box.h, _this.dragContainer.point.top + dy );
  99.  
  100. _this.dragContainer.newPoint = {
  101. left: typeof newx != 'undefined' ? newx : _this.dragContainer.point.left,
  102. top: typeof newy != 'undefined' ? newy : _this.dragContainer.point.top
  103. };
  104. console.log(_this.dragContainer.newPoint);
  105. return false;
  106. },
  107. // 移动完成 取消onmousemove和onmouseup事件
  108. DragContainerMouseUp(e) {
  109. document.body.onmousemove = null;
  110. document.onmouseup = null;
  111. },
  112. PointMouseDown(e) {
  113. //阻止事件冒泡
  114. e.stopPropagation();
  115. }
  116. }
  117. };
  118. </script>

  

样式表

  1. .page {
  2. background: #444;
  3. width: 100%;
  4. height: 100%;
  5. position: relative;
  6. .image-move-wapper {
  7. position: absolute;
  8. right: 50px;
  9. top: 50px;
  10. background: #fff;
  11. box-shadow: rgba(255, 255, 255, 0.5);
  12. padding: 10px;
  13. }
  14. .image-show-box {
  15. height: 400px;
  16. width: 400px;
  17. cursor: move;
  18. overflow: hidden;
  19. position: relative;
  20. .drag-container {
  21. position: absolute;
  22. left: 0px;
  23. top: 0;
  24. /*禁止元素选中*/
  25. -moz-user-select: none; /*火狐*/
  26. -webkit-user-select: none; /*webkit浏览器*/
  27. -ms-user-select: none; /*IE10*/
  28. -khtml-user-select: none; /*早期浏览器*/
  29. user-select: none;
  30. .drag-image-box {
  31. position: relative;
  32. .point {
  33. position: absolute;
  34. background: red;
  35. height: 30px;
  36. width: 30px;
  37. border-radius: 50%;
  38. }
  39. }
  40. }
  41. }
  42. }

原文地址:http://jiojun.com/Article/Detail?articleId=17

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号