日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue 移动到图片浮动_基于Vue实现图片在指定区域内移动

發(fā)布時間:2023/12/2 vue 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue 移动到图片浮动_基于Vue实现图片在指定区域内移动 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

此處需要注意的是

我們在移動圖片時,需要通過draggable="false"?將圖片的 ,否則按下鼠標監(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 代碼

@import url("./test.less");

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: {

// 鼠標在拖拽容器中按下時觸發(fā)

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;

},

// 容器拖拽時觸發(fā)

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();

}

}

};

樣式表

.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 移动到图片浮动_基于Vue实现图片在指定区域内移动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。