最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Vue實現(xiàn)圖片在指定區(qū)域內(nèi)移動的思路詳解

 更新時間:2018年11月11日 09:54:05   作者:JioJun  
這篇文章主要介紹了基于Vue實現(xiàn)圖片在指定區(qū)域內(nèi)移動,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

  當(dāng)圖片比要顯示的區(qū)域大時,需要將多余的部分隱藏掉,我們可以通過絕對定位來實現(xiàn),并通過動態(tài)修改圖片的left值和top值從而實現(xiàn)圖片的移動。具體實現(xiàn)效果如下圖,如果我們移動的是div 實現(xiàn)思路相仿。

此處需要注意的是

我們在移動圖片時,需要通過draggable="false" 將圖片的 <img src="/static/pano-dev/test/image-2.jpg" draggable="false" />,否則按下鼠標(biāo)監(jiān)聽onmousemove事件時監(jiān)聽不到

然后還需要禁用圖片的選中css

/*禁止元素選中*/
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit瀏覽器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期瀏覽器*/
user-select: none;

Vue 代碼

<style lang="less" scoped>
@import url("./test.less");
</style>
<template>
 <div class="page">
  <div class="image-move-wapper">
   <div class="image-show-box" ref="imageShowBox">
    <div class="drag-container" ref="dragContainer" :style="'left:' + dragContainer.newPoint.left+'px; top:' + dragContainer.newPoint.top+'px'" @mousedown="DragContainerMouseDown">
     <div class="drag-image-box">
      <img src="/static/pano-dev/test/image-2.jpg" draggable="false" />
      <div class="point" style="left:10%; top:10%" @mousedown="PointMouseDown"></div>
     </div>
    </div>
   </div>
  </div>
 </div>
</template>
<script>
export default {
 data() {
  return {
   dragContainer: {
    box: {
     w: 0,
     h: 0
    },
    point: {
     left: 0,
     top: 0
    },
    newPoint: {
     left: 0,
     top: 0
    }
   },
   mousePoint: {
    x: 0,
    y: 0
   },
   imageShowBox: {
    box: {
     w: 0,
     h: 0
    },
    dragcheck: {
     h: true,
     v: true
    }
   }
  };
 },
 updated() {
  let _this = this;
  // 確保DOM元素已經(jīng)渲染成功,然后獲取拖拽容器和顯示區(qū)域的大小
  _this.$nextTick(function() {
   _this.dragContainer.box = {
    w: _this.$refs.dragContainer.offsetWidth,
    h: _this.$refs.dragContainer.offsetHeight
   };
   _this.imageShowBox.box = {
    w: _this.$refs.imageShowBox.offsetWidth,
    h: _this.$refs.imageShowBox.offsetHeight
   };
   // 判斷是否允許拖拽
   _this.imageShowBox.dragcheck = {
    h: _this.imageShowBox.box.w > _this.dragContainer.box.w ? false : true,
    v: _this.imageShowBox.box.h > _this.dragContainer.box.h ? false : true
   };
  });
 },
 methods: {
  // 鼠標(biāo)在拖拽容器中按下時觸發(fā)
  DragContainerMouseDown(e) {
   const _this = this;
   // 記錄鼠標(biāo)點(diǎn)擊時的初始坐標(biāo)
   this.mousePoint = {
    x: e.clientX,
    y: e.clientY
   };
   _this.dragContainer.point = _this.dragContainer.newPoint;
   document.body.onmousemove = _this.DragContainerMouseMove;
   document.onmouseup = _this.DragContainerMouseUp;
   return false;
  },
  // 容器拖拽時觸發(fā)
  DragContainerMouseMove(e) {
   const _this = this;
   // 鼠標(biāo)偏移量 = 初始坐標(biāo) - 當(dāng)前坐標(biāo)
   let dx = e.clientX - _this.mousePoint.x;
   let dy = e.clientY - _this.mousePoint.y;
   // 獲取拖拽容器移動后的left和top值
   if (_this.imageShowBox.dragcheck.h)
    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 );
   if (_this.imageShowBox.dragcheck.v)
    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 );
   _this.dragContainer.newPoint = {
    left: typeof newx != 'undefined' ? newx : _this.dragContainer.point.left,
    top: typeof newy != 'undefined' ? newy : _this.dragContainer.point.top
   };
   console.log(_this.dragContainer.newPoint);
   return false;
  },
  // 移動完成 取消onmousemove和onmouseup事件
  DragContainerMouseUp(e) {
   document.body.onmousemove = null;
   document.onmouseup = null;
  },
  PointMouseDown(e) {
   //阻止事件冒泡
   e.stopPropagation();
  }
 }
};
</script>

樣式表

.page {
 background: #444;
 width: 100%;
 height: 100%;
 position: relative;
 .image-move-wapper {
  position: absolute;
  right: 50px;
  top: 50px;
  background: #fff;
  box-shadow: rgba(255, 255, 255, 0.5);
  padding: 10px;
 }
 .image-show-box {
  height: 400px;
  width: 400px;
  cursor: move;
  overflow: hidden;
  position: relative;
  .drag-container {
   position: absolute;
   left: 0px;
   top: 0;
   /*禁止元素選中*/
   -moz-user-select: none; /*火狐*/
   -webkit-user-select: none; /*webkit瀏覽器*/
   -ms-user-select: none; /*IE10*/
   -khtml-user-select: none; /*早期瀏覽器*/
   user-select: none;
   .drag-image-box {
    position: relative;
    .point {
     position: absolute;
     background: red;
     height: 30px;
     width: 30px;
     border-radius: 50%;
    }
   }
  }
 }
} 

總結(jié)

以上所述是小編給大家介紹的基于Vue實現(xiàn)圖片在指定區(qū)域內(nèi)移動的思路詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

  • Vue實現(xiàn)獲取后端接口API代碼片段(已封裝Service方法名)

    Vue實現(xiàn)獲取后端接口API代碼片段(已封裝Service方法名)

    Vue實現(xiàn)獲取后端接口API代碼片段(已封裝Service方法名),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue3?邏輯復(fù)用的實現(xiàn)示例

    vue3?邏輯復(fù)用的實現(xiàn)示例

    在項目開發(fā)中,有兩個功能特別類似,如果單獨(dú)實現(xiàn),會有很多重復(fù)的代碼,這時候就會用到邏輯復(fù)用,本文主要介紹了vue3?邏輯復(fù)用的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • vue多級復(fù)雜列表展開/折疊及全選/分組全選實現(xiàn)

    vue多級復(fù)雜列表展開/折疊及全選/分組全選實現(xiàn)

    這篇文章主要介紹了vue多級復(fù)雜列表展開/折疊及全選/分組全選實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 關(guān)于Element-ui中Table表格無法顯示的問題及解決

    關(guān)于Element-ui中Table表格無法顯示的問題及解決

    這篇文章主要介紹了關(guān)于Element-ui中Table表格無法顯示的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • undefined是否會變?yōu)閚ull原理解析

    undefined是否會變?yōu)閚ull原理解析

    這篇文章主要為大家介紹了undefined是否會變?yōu)閚ull原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • vue代理模式解決跨域詳解

    vue代理模式解決跨域詳解

    這篇文章主要介紹了vue代理模式解決跨域詳解的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • vue項目中Eslint校驗代碼報錯的解決方案

    vue項目中Eslint校驗代碼報錯的解決方案

    這篇文章主要介紹了vue項目中Eslint校驗代碼報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue Computed底層原理深入探究

    Vue Computed底層原理深入探究

    computed是vue的配置選項,它的值是一個對象,其中可定義多個計算屬性,每個計算屬性就是一個函數(shù),下面這篇文章主要給大家介紹了關(guān)于vue中計算屬性computed的詳細(xì)講解,需要的朋友可以參考下
    2022-08-08
  • vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置

    vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置

    我們使?vue-element-admin前端框架開發(fā)后臺管理系統(tǒng)時,?般都會涉及到菜單的權(quán)限控制問題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 在vue項目中正確使用iconfont的方法

    在vue項目中正確使用iconfont的方法

    今天小編就為大家分享一篇在vue項目中正確使用iconfont的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論

苗栗市| 潞西市| 西平县| 赤峰市| 云南省| 岐山县| 阳城县| 铜川市| 茌平县| 化德县| 洛扎县| 岳西县| 家居| 烟台市| 额敏县| 灵寿县| 荥阳市| 五莲县| 雷州市| 买车| 南靖县| 常德市| 衡水市| 牙克石市| 宣恩县| 克拉玛依市| 金门县| 建平县| 汽车| 冀州市| 阿拉善左旗| 荆州市| 西藏| 达尔| 青海省| 沅江市| 丰城市| 吉林市| 图木舒克市| 宣汉县| 方城县|