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

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

 更新時間:2022年10月13日 10:16:00   作者:名字起太長會有傻子跟著念  
這篇文章主要為大家介紹了js?實現(xiàn)div拖拽拉伸完整示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

今天和大家分享一下。如何用js實現(xiàn)div拖拽拉伸等功能。功能比較簡單,我就不贅述了。直接上代碼。

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 實現(xiàn)div拖拽拉伸完整示例的詳細內(nèi)容,更多關(guān)于js 實現(xiàn)div拖拽拉伸的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • JavaScript數(shù)組去重的幾種方法效率測試

    JavaScript數(shù)組去重的幾種方法效率測試

    JavaScript數(shù)組去重是前端面試酷愛的問題,問題簡單而又能看出程序員對計算機程序執(zhí)行過程的理解如何。數(shù)組去重的方法有很多,到底哪種是最理想的我不清楚。于是我測試了下數(shù)組去重的效率。測試二十萬個數(shù)據(jù),隨著數(shù)據(jù)越多效率很明顯的就體驗了出來。下面來一起看看吧。
    2016-10-10
  • 手機開發(fā)必備技巧:javascript及CSS功能代碼分享

    手機開發(fā)必備技巧:javascript及CSS功能代碼分享

    這篇文章主要介紹了手機開發(fā)必備技巧:javascript及CSS功能代碼分享,本文講解了viewport(可視區(qū)域)操作、鏈接操作、javascript事件等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • JS實現(xiàn)簡易貪吃蛇游戲

    JS實現(xiàn)簡易貪吃蛇游戲

    這篇文章主要為大家詳細介紹了JS實現(xiàn)簡易貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • javascript基本常用排序算法解析

    javascript基本常用排序算法解析

    這篇文章主要為大家詳細介紹了javascript基本常用排序算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • BootStrap CSS全局樣式和表格樣式源碼解析

    BootStrap CSS全局樣式和表格樣式源碼解析

    這篇文章主要為大家詳細解析了BootStrap圖片樣式、輔助類樣式和CSS組件源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • JavaScript二叉樹及各種遍歷算法詳情

    JavaScript二叉樹及各種遍歷算法詳情

    這篇文章主要介紹了JavaScript二叉樹及各種遍歷算法詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • 淺談ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別

    淺談ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別

    箭頭函數(shù)是ES6中一種新的函數(shù)的表達式,本文就來介紹一下ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別,非常具有實用價值,需要的朋友可以參考下
    2023-05-05
  • JavaScript實現(xiàn)自動跳轉(zhuǎn)文本功能

    JavaScript實現(xiàn)自動跳轉(zhuǎn)文本功能

    這篇文章主要為大家詳細介紹了JavaScript自動跳轉(zhuǎn)文本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • JavaScript中的await函數(shù)使用小結(jié)

    JavaScript中的await函數(shù)使用小結(jié)

    async 函數(shù)是 AsyncFunction 構(gòu)造函數(shù)的實例,并且其中允許使用 await 關(guān)鍵字,async 和 await 關(guān)鍵字讓我們可以用一種更簡潔的方式寫出基于 Promise 的異步行為,而無需刻意地鏈式調(diào)用 promise,這篇文章主要介紹了JavaScript中的await,需要的朋友可以參考下
    2024-01-01
  • Bootstrap整體框架之JavaScript插件架構(gòu)

    Bootstrap整體框架之JavaScript插件架構(gòu)

    這篇文章主要介紹了Bootstrap整體框架之JavaScript插件架構(gòu)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論

建德市| 德钦县| 乐陵市| 太康县| 天全县| 芜湖县| 开阳县| 苏尼特左旗| 弋阳县| 五华县| 巴彦淖尔市| 潜山县| 潜江市| 专栏| 白山市| 东平县| 慈利县| 张家界市| 洪江市| 沁水县| 区。| 疏附县| 白沙| 治多县| 得荣县| 米易县| 琼海市| 名山县| 彰化县| 施秉县| 平阴县| 师宗县| 江城| 潮州市| 治多县| 长治县| 洪雅县| 襄汾县| 沭阳县| 星子县| 泸定县|