基於Vue實現圖片在指定區域內移動

来源:https://www.cnblogs.com/HJbk/archive/2018/11/10/9938870.html
-Advertisement-
Play Games

基於Vue通過onmousemove、onmouseup、onmousedown 實現圖片在指定區域內移動功能實現... ...


當圖片比要顯示的區域大時,需要將多餘的部分隱藏掉,我們可以通過絕對定位來實現,並通過動態修改圖片的left值和top值從而實現圖片的移動。具體實現效果如下圖,如果我們移動的是div 實現思路相仿。

未標題-1

此處需要註意的是

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

然後還需要禁用圖片的選中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元素已經渲染成功,然後獲取拖拽容器和顯示區域的大小
    _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: {
    // 滑鼠在拖拽容器中按下時觸發
    DragContainerMouseDown(e) {
      const _this = this;
      // 記錄滑鼠點擊時的初始坐標
      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;
    },
    // 容器拖拽時觸發
    DragContainerMouseMove(e) {
      const _this = this;
      // 滑鼠偏移量 = 初始坐標 - 當前坐標
      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%;
        }
      }
    }
  }
}

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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前文瞭解了什麼是前端,那麼前端技術到底有哪些呢?最核心的就這三個: html/html5 css/css3 javascript 什麼是HTML HyperText Markup Language,超文本標記語言 ...
  • 2018-11-10 今天是我第一次寫博客,因為沒經驗嘛,我就隨便寫寫,我也希望自己以後能堅持寫下去,不為別的,就為了自己能夠更好地學習編程,能夠追隨行業大牛的腳步,從此贏取白富美,走上人生巔峰(額,理想遠大呵!) 好吧,步入正題,因為目前在學java,加了一些培訓機構的群,跟著做了一個小項目,就是 ...
  • 1.sass 1 @charset "utf-8"; 2 /** 3 * @module 背景與邊框 4 * @description 為元素添加邊框(包括1px邊框) 5 * @method border 6 * @version 2.0.0 7 * @param {String} $border ...
  • 微信小程式車牌號碼模擬鍵盤輸入練習, 未經允許,禁止轉載,抄襲,如需借鑒參考等,請附上該文章連接。 相關資料參考:https://blog.csdn.net/littlerboss/article/details/79877918; 先來一波預覽圖。 預覽圖片一: 預覽圖二: 預覽圖三: 預覽圖四: ...
  • 1.什麼是web前端 說這個之前,我們先瞭解web前端工程師是乾什麼的,百度百科的解釋: Web前端開發工程師,主要職責是利用(X)HTML/CSS/JavaScript/Flash等各種Web技術進行客戶端產品的開發。完成客戶端程式(也就是瀏覽器端)的開發,開發JavaScript以及Flash... ...
  • 為了在CardSimulate項目中方便的顯示技能和效果列表,決定重構以前編寫的一段JavaScript代碼——att表格繪製庫,這段代碼的作用是將特定的JavaScript數據對象轉化為表格,支持精細的樣式設置和一些複雜報表功能並且提供了自由的擴展性。可以用較新的Chrome瀏覽器訪問https: ...
  • 最近由於工作原因以及自己的懈怠,已經很久都沒有更新過博客了。通過這段時間,我發現堅持一件事情是真的很難,都說萬事開頭難,但是在放棄這件事上好像開頭了後面就順理成章的繼續下去了。中間即使不怎麼情願也在努力的每周更新博客,但是自從9月份以來,第一次因為工作需要加班而斷更之後,後面好像很容易找到理由斷更。 ...
  • 初識模塊化開發工具:git 是分散式代碼管理工具,管理代碼的npm 是包管理工具,管理插件、工具啊,是個轉換器,他是哪來的了,他是伴隨node下載下來的,版本也是伴隨node變化;node 是個後臺的環境;首先安裝node,然後用命令安裝browserify:npm install -g brows ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...