js?實(shí)現(xiàn)div拖拽拉伸完整示例

前言
今天和大家分享一下。如何用js實(shí)現(xiàn)div拖拽拉伸等功能。功能比較簡(jiǎn)單,我就不贅述了。直接上代碼。
HTML
<div class="resize" data-key="drag">
<img src="../images/liya.jpg" alt="">
<div class="line line-n" data-key="n"></div>
<div class="line line-e" data-key="e"></div>
<div class="line line-s" data-key="s"></div>
<div class="line line-w" data-key="w"></div>
<div class="point point-n" data-key="n"></div>
<div class="point point-e" data-key="e"></div>
<div class="point point-s" data-key="s"></div>
<div class="point point-w" data-key="w"></div>
<div class="point point-ne" data-key="ne"></div>
<div class="point point-se" data-key="se"></div>
<div class="point point-sw" data-key="sw"></div>
<div class="point point-nw" data-key="nw"></div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
.container {
height: 100vh;
background: #000;
overflow: hidden;
user-select: none;
}
.resize {
position: relative;
outline: 1px solid #1890ff;
touch-action: none;
}
img {
display: block;
width: 100%;
height: 100%;
pointer-events: none;
}
.line {
position: absolute;
}
.line-n {
left: 0;
top: -3px;
width: 100%;
height: 6px;
cursor: n-resize;
}
.line-e {
top: 0;
right: -3px;
width: 6px;
height: 100%;
cursor: e-resize;
}
.line-s {
left: 0;
bottom: -3px;
width: 100%;
height: 6px;
cursor: s-resize;
}
.line-w {
top: 0;
left: -3px;
width: 6px;
height: 100%;
cursor: w-resize;
}
.point {
position: absolute;
width: 6px;
height: 6px;
background: #1890ff;
}
.point-n {
top: -3px;
left: calc(50% - 3px);
cursor: n-resize;
}
.point-e {
right: -3px;
top: calc(50% - 3px);
cursor: e-resize;
}
.point-s {
bottom: -3px;
left: calc(50% - 3px);
cursor: s-resize;
}
.point-w {
left: -3px;
top: calc(50% - 3px);
cursor: w-resize;
}
.point-ne {
top: -3px;
right: -3px;
cursor: ne-resize;
}
.point-se {
bottom: -3px;
right: -3px;
cursor: se-resize;
}
.point-sw {
left: -3px;
bottom: -3px;
cursor: sw-resize;
}
.point-nw {
left: -3px;
top: -3px;
cursor: nw-resize;
}
JS
// 獲取dom
const box = document.querySelector('.resize');
// 聲明全局變量
let width = 200, height = 160, minWidth = 100, minHeight = 80, isPointerdown = false,
x = (window.innerWidth - width) * 0.5, y = (window.innerHeight - height) * 0.5,
diff = { x: 0, y: 0 }, lastPointermove = { x: 0, y: 0 }, key = '', rect = null;
box.style.width = width + 'px';
box.style.height = height + 'px';
box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
const action = {
drag: function () {
x += diff.x;
y += diff.y;
},
n: function (e) {
if (rect.bottom - e.clientY > minHeight) {
height = rect.bottom - e.clientY;
y = e.clientY;
}
},
e: function (e) {
if (e.clientX - rect.left > minWidth) {
width = e.clientX - rect.left;
}
},
s: function (e) {
if (e.clientY - rect.top > minHeight) {
height = e.clientY - rect.top;
}
},
w: function (e) {
if (rect.right - e.clientX > minWidth) {
width = rect.right - e.clientX;
x = e.clientX;
}
},
ne: function (e) {
this.n(e);
this.e(e);
},
se: function (e) {
this.s(e);
this.e(e);
},
sw: function (e) {
this.s(e);
this.w(e);
},
nw: function (e) {
this.n(e);
this.w(e);
}
}
// 綁定事件
box.addEventListener('pointerdown', function (e) {
isPointerdown = true;
e.target.setPointerCapture(e.pointerId);
lastPointermove = { x: e.clientX, y: e.clientY };
key = e.target.dataset.key;
rect = box.getBoundingClientRect();
});
box.addEventListener('pointermove', function (e) {
if (isPointerdown) {
const current = { x: e.clientX, y: e.clientY };
diff.x = current.x - lastPointermove.x;
diff.y = current.y - lastPointermove.y;
lastPointermove = { x: current.x, y: current.y };
action[key](e);
box.style.width = width + 'px';
box.style.height = height + 'px';
box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0)';
e.preventDefault();
}
});
box.addEventListener('pointerup', function (e) {
if (isPointerdown) {
isPointerdown = false;
}
});
box.addEventListener('pointercancel', function (e) {
if (isPointerdown) {
isPointerdown = false;
}
});
Demo: jsdemo.codeman.top/html/divRes…
以上就是js 實(shí)現(xiàn)div拖拽拉伸完整示例的詳細(xì)內(nèi)容,更多關(guān)于js 實(shí)現(xiàn)div拖拽拉伸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript數(shù)組去重的幾種方法效率測(cè)試
JavaScript數(shù)組去重是前端面試酷愛的問題,問題簡(jiǎn)單而又能看出程序員對(duì)計(jì)算機(jī)程序執(zhí)行過程的理解如何。數(shù)組去重的方法有很多,到底哪種是最理想的我不清楚。于是我測(cè)試了下數(shù)組去重的效率。測(cè)試二十萬個(gè)數(shù)據(jù),隨著數(shù)據(jù)越多效率很明顯的就體驗(yàn)了出來。下面來一起看看吧。2016-10-10
手機(jī)開發(fā)必備技巧:javascript及CSS功能代碼分享
這篇文章主要介紹了手機(jī)開發(fā)必備技巧:javascript及CSS功能代碼分享,本文講解了viewport(可視區(qū)域)操作、鏈接操作、javascript事件等內(nèi)容,需要的朋友可以參考下2015-05-05
淺談ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別
箭頭函數(shù)是ES6中一種新的函數(shù)的表達(dá)式,本文就來介紹一下ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2023-05-05
JavaScript實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)文本功能
這篇文章主要為大家詳細(xì)介紹了JavaScript自動(dòng)跳轉(zhuǎn)文本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
JavaScript中的await函數(shù)使用小結(jié)
async 函數(shù)是 AsyncFunction 構(gòu)造函數(shù)的實(shí)例,并且其中允許使用 await 關(guān)鍵字,async 和 await 關(guān)鍵字讓我們可以用一種更簡(jiǎn)潔的方式寫出基于 Promise 的異步行為,而無需刻意地鏈?zhǔn)秸{(diào)用 promise,這篇文章主要介紹了JavaScript中的await,需要的朋友可以參考下2024-01-01
Bootstrap整體框架之JavaScript插件架構(gòu)
這篇文章主要介紹了Bootstrap整體框架之JavaScript插件架構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

