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

原生js實(shí)現(xiàn)拖拽移動(dòng)與縮放效果

 更新時(shí)間:2020年08月24日 11:16:07   作者:前端小白白又白  
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)拖拽移動(dòng)與縮放效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)拖拽移動(dòng)與縮放效果的具體代碼,供大家參考,具體內(nèi)容如下

效果圖如下-實(shí)現(xiàn)了簡(jiǎn)單的拖拽和縮放功能

第一步—簡(jiǎn)單的拖拽功能

// 創(chuàng)建一個(gè)MoveClass構(gòu)造函數(shù)
 function MoveClass(id, options = {}) {
 // 綁定ele屬性
 this.ele = document.querySelector(id);

 this.move();
}
// 給MoveClass原型上綁定move方法
 MoveClass.prototype.move = function () {
 // ele的鼠標(biāo)按下事件調(diào)用mousedown
 this.ele.onmousedown = e => {
 // 獲取事件對(duì)象
 var e = e || window.event;
 // 鼠標(biāo)按下時(shí),鼠標(biāo)相對(duì)于元素的x坐標(biāo)
 var x = e.offsetX;
 // 鼠標(biāo)按下時(shí),鼠標(biāo)相對(duì)于元素的y坐標(biāo)
 var y = e.offsetY;
 // 鼠標(biāo)按下移動(dòng)時(shí)調(diào)用mousemove
 document.onmousemove = e => {
 // 元素ele移動(dòng)的距離l
 var l = e.clientX - x;
 // 元素ele移動(dòng)的距離l
 var t = e.clientY - y;
 this.ele.style.left = l + "px";
 this.ele.style.top = t + "px";
 }
 // 當(dāng)鼠標(biāo)彈起時(shí),清空onmousemove與onmouseup
 document.onmouseup = () => {
 document.onmousemove = null;
 document.onmouseup = null;
 }
 }
 }
 // new一個(gè)MoveClass對(duì)象
 var moveClass = new MoveClass("#box");

效果如下,簡(jiǎn)單的拖拽

第二步—簡(jiǎn)單的縮放功能

1.設(shè)置方位

// ele的左,左上,左下,右,右上,右下,上,下
 MoveClass.prototype.editoptions = {
 left_top: true,
 left: true,
 right: true,
 top: true,
 bottom: true,
 right_top: true,
 left_bottom: true,
 right_bottom: true,
 }

2.給原型綁定縮放的方法

// 給原型綁定縮放的方法
 MoveClass.prototype.editEle = function () {
 // console.log(this.ele.clientWidth,this.ele.clientHeight);
 // console.log(this.ele.offsetLeft,this.ele.offsetTop);
 var that = this;
 // 創(chuàng)建一個(gè)div
 var div = document.createElement("div");
 // 遍歷this.editoptions
 for (let attr in this.editoptions) {
 if (this.editoptions[attr]) {
 // 循環(huán)創(chuàng)建左,左上,左下,右,右上,右下,上,下方位的小點(diǎn)
 var dian = document.createElement("div");
 dian.className = "dian " + attr;
 // 設(shè)置類(lèi)型為對(duì)應(yīng)的attr
 dian.dataset.type = attr;
 // 當(dāng)按下對(duì)應(yīng)方位的小點(diǎn)時(shí)
 dian.onmousedown = e => {
 var e = e || window.event;
 // 先獲取鼠標(biāo)距離屏幕的left與top值
 var clientXY = {
 x: e.clientX,
 y: e.clientY
 }
 // 獲取鼠標(biāo)按下時(shí)ele的寬高
 var eleWH = {
 width: this.ele.clientWidth,
 height: this.ele.clientHeight,
 }
 // 阻止事件冒泡(針對(duì)父元素的move)
 e.stopPropagation();
 // 通過(guò)e.target獲取精準(zhǔn)事件源對(duì)應(yīng)的type值
 var type = e.target.dataset.type;
 // 鼠標(biāo)按下對(duì)應(yīng)方位小點(diǎn)移動(dòng)時(shí),調(diào)用mousemove
 document.onmousemove = function (e) {
 // 查找type中是否包含”right“
 if (type.indexOf('right') > -1) {
 // console.log("right");
 // 如果拖拽后的寬度小于最小寬度,就return出去
 if (that.options.minWidth > eleWH.width + e.clientX - clientXY.x) {
 return;
 }
 // ele拖拽后的寬度為:初始width+拖拽后鼠標(biāo)距離屏幕的距離 - 第一次按下時(shí)鼠標(biāo)距離屏幕的距離
 that.ele.style.width = (eleWH.width + e.clientX - clientXY.x) + "px";
 }
 // 與”right“相同原理
 if (type.indexOf("bottom") > -1) {
 // console.log("bottom");
 if (that.options.minHeight > eleWH.height + e.clientY - clientXY.y) {
 return;
 }
 that.ele.style.height = (eleWH.height + e.clientY - clientXY.y) + "px"
 }

 if (type.indexOf("top") > -1) {
 // console.log("top");
 if (that.options.minHeight > eleWH.height - e.clientY + clientXY.y) {
 return;
 }
 // ele拖拽后的高度為:初始height-拖拽后鼠標(biāo)距離屏幕的距離 + 第一次按下時(shí)鼠標(biāo)距離屏幕的距離
 that.ele.style.height = (eleWH.height - e.clientY + clientXY.y) + "px";
 // 重新設(shè)置ele的top值為此時(shí)鼠標(biāo)距離屏幕的y值
 that.ele.style.top = e.clientY + "px";
 }
 // 與”top“相同原理
 if (type.indexOf("left") > -1) {
 // console.log("left");
 if (that.options.minWidth > eleWH.width - e.clientX + clientXY.x) {
 return;
 }
 that.ele.style.width = (eleWH.width - e.clientX + clientXY.x) + "px";
 // 重新設(shè)置ele的left值為此時(shí)鼠標(biāo)距離屏幕的x值
 that.ele.style.left = e.clientX + "px";
 }
 }
 document.onmouseup = function () {
 document.onmousemove = null;
 document.onmouseup = null;
 }
 }
 }
 // 將類(lèi)名為”dian“的div添加到div中
 div.appendChild(dian);
 }
 // 為div設(shè)置類(lèi)名
 div.className = "kuang"
 // 將類(lèi)名為”kuang“的div添加到ele中
 this.ele.appendChild(div);
 }

效果圖如下

最終效果,盒子可以拖動(dòng),可以縮放。

盒子上的8個(gè)小點(diǎn)采用定位放上去的,事先寫(xiě)好了樣式

<style>
 * {
 margin: 0;
 padding: 0;
 }

 #box {
 width: 100px;
 height: 100px;
 background: orange;
 position: absolute;
 left: 100px;
 top: 100px;
 }

 .kuang {
 box-sizing: border-box;
 border: 1px solid #0f0;
 width: 100%;
 height: 100%;
 position: relative;
 }

 .kuang .dian {
 position: absolute;
 width: 10px;
 height: 10px;
 border-radius: 50%;
 background: #0f0;
 }

 .left_top {
 left: -5px;
 top: -5px;
 }

 .right_top {
 right: -5px;
 top: -5px;
 }

 .left_bottom {
 left: -5px;
 bottom: -5px;
 }

 .right_bottom {
 right: -5px;
 bottom: -5px;
 }

 .top {
 top: -5px;
 left: 50%;
 transform: translateX(-50%);
 }

 .bottom {
 bottom: -5px;
 left: 50%;
 transform: translateX(-50%);
 }

 .left {
 left: -5px;
 top: 50%;
 transform: translateY(-50%);
 }

 .right {
 right: -5px;
 top: 50%;
 transform: translateY(-50%);
 }
</style>
<body>
 <div id="box" class="border"></div>
</body>

所有的代碼能直接粘貼使用??s放的原理其實(shí)就是鼠標(biāo)按下去時(shí),獲取當(dāng)前的鼠標(biāo)位置和盒子的寬高,鼠標(biāo)按下并移動(dòng)后獲取此時(shí)的鼠標(biāo)位置。

拖拽后的寬高=初始寬高+(拖拽后鼠標(biāo)距離屏幕的位置 - 第一次按下時(shí)鼠標(biāo)距離屏幕的位置)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • uni-app小程序分享功能實(shí)現(xiàn)方法舉例

    uni-app小程序分享功能實(shí)現(xiàn)方法舉例

    這篇文章主要給大家介紹了關(guān)于uni-app小程序分享功能實(shí)現(xiàn)方法的相關(guān)資料,uni-app中有分享的API接口,但是需要現(xiàn)在QQ或者微信等開(kāi)發(fā)者平臺(tái)上注冊(cè)賬號(hào),驗(yàn)證公司信息,而且只能分享圖片或者文本等內(nèi)容,需要的朋友可以參考下
    2023-07-07
  • js獲取url參數(shù)值的幾種方式詳解

    js獲取url參數(shù)值的幾種方式詳解

    這篇文章為大家主要介紹了js獲取url參數(shù)值的幾種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-07-07
  • javascript 判斷整數(shù)方法分享

    javascript 判斷整數(shù)方法分享

    本文介紹了使用javascript判斷整數(shù)的2種方法中的一種正則判斷,因?yàn)橹鹱峙袛嘈侍拖铝?,有相同需求的小伙伴們參考?/div> 2014-12-12
  • 學(xué)習(xí)Javascript面向?qū)ο缶幊讨庋b

    學(xué)習(xí)Javascript面向?qū)ο缶幊讨庋b

    這篇文章主要幫助大家學(xué)習(xí)Javascript面向?qū)ο缶幊讨庋b,由淺入深的介紹了封裝的概念定義,感興趣的小伙伴們可以參考一下
    2016-02-02
  • webstorm中配置Eslint的兩種方式及差異比較詳解

    webstorm中配置Eslint的兩種方式及差異比較詳解

    這篇文章主要介紹了webstorm中配置Eslint的兩種方式及差異比較詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • ES6使用 const 聲明過(guò)程詳解

    ES6使用 const 聲明過(guò)程詳解

    這篇文章主要介紹了ES6使用 const 聲明,本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • JavaScript中forEach的錯(cuò)誤用法匯總

    JavaScript中forEach的錯(cuò)誤用法匯總

    js中foreach是用于遍歷數(shù)組的方法,將遍歷到的元素傳遞給回調(diào)函數(shù),遍歷的數(shù)組不能是空的要有值,下面這篇文章主要給大家介紹了關(guān)于JavaScript中forEach的錯(cuò)誤用法,需要的朋友可以參考下
    2022-06-06
  • 原生js無(wú)縫輪播插件使用詳解

    原生js無(wú)縫輪播插件使用詳解

    這篇文章主要為大家詳細(xì)介紹了原生js無(wú)縫輪播插件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • js html css實(shí)現(xiàn)復(fù)選框全選與反選

    js html css實(shí)現(xiàn)復(fù)選框全選與反選

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)復(fù)選框全選與反選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • JavaScript函數(shù)增強(qiáng)以及額外知識(shí)

    JavaScript函數(shù)增強(qiáng)以及額外知識(shí)

    函數(shù)就是封裝了一段可以被重復(fù)執(zhí)行調(diào)用的代碼塊,下面這篇文章主要給大家介紹了關(guān)于JavaScript函數(shù)增強(qiáng)以及額外知識(shí)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論

股票| 洪湖市| 仁寿县| 高密市| 武义县| 仪征市| 瑞丽市| 大渡口区| 商丘市| 郎溪县| 铜梁县| 万盛区| 连平县| 鄂托克旗| 博白县| 云南省| 武强县| 迁安市| 石林| 盐边县| 雷州市| 泊头市| 苏尼特右旗| 阳曲县| 嵊州市| 和硕县| 修文县| 利川市| 海伦市| 阿城市| 额尔古纳市| 垣曲县| 芦山县| 绥江县| 孟州市| 赣州市| 周口市| 定南县| 永昌县| 石渠县| 砀山县|