javascript拖拽效果延伸學(xué)習(xí)
本文總結(jié)一下,拖拽所延伸出來的一些效果,供大家參考,具體內(nèi)容如下
1.實(shí)現(xiàn)拖拉圖片時(shí),帶框的效果。即當(dāng)鼠標(biāo)拖動(dòng)某一個(gè)圖片或物體時(shí),其原有位置扔保留其型。
這種效果,其實(shí)很簡(jiǎn)單,主要是另外創(chuàng)建一個(gè)物體,使其與被拖拽的物體,寬和高一樣,然后,將其變?yōu)橥献У膶?duì)象。
直接上代碼:
<html <head> <style> #div1 {width:100px; height:100px; background:yellow; position:absolute;} .box{border: 1px solid black;position: absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <script> window.onload=function ()//凡是被拖拽的物體,其必須定位為absolute { var oDiv=document.getElementById('div1'); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; var oNewDiv=document.createElement('div'); oNewDiv.className='box'; oNewDiv.style.width=oDiv.offsetWidth-2+'px';//將2px的邊框去掉 oNewDiv.style.height=oDiv.offsetHeight-2+'px'; oNewDiv.style.left=oDiv.offsetLeft+'px'; oNewDiv.style.top=oDiv.offsetTop+'px'; document.body.appendChild(oNewDiv); document.onmousemove=function (ev) { var oEvent=ev||event; oNewDiv.style.left=oEvent.clientX-disX+'px'; oNewDiv.style.top=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; document.body.removeChild(oNewDiv); oDiv.style.left=oNewDiv.style.left; oDiv.style.top=oNewDiv.style.top; }; }; }; </script> </head> <body> <div id="div1"> </div> </body> </html>
2.關(guān)于窗口拖拉放大縮小的效果,就是在上面的div之中再包一個(gè)div。
<html <head> <style> #div1 {width:10px; height:10px; background:url(images/1.gif); position:absolute;bottom: 0;right: 0}//拖拉的物體,改為圖片 #div2{width: 200px;height: 200px;position: relative;background: #ccc;} .box{border: 1px solid black;position: absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <script> window.onload=function ()//凡是被拖拽的物體,其必須定位為absolute { var oDiv=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; var oNewDiv=document.createElement('div'); //oNewDiv.className='box'; oNewDiv.style.width=oDiv.offsetWidth-2+'px'; oNewDiv.style.height=oDiv.offsetHeight-2+'px'; oNewDiv.style.left=oDiv.offsetLeft+'px'; oNewDiv.style.top=oDiv.offsetTop+'px'; document.body.appendChild(oNewDiv); document.onmousemove=function (ev) { var oEvent=ev||event; oDiv2.style.width=oEvent.clientX-disX+'px';//這里是它的父級(jí) oDiv2.style.height=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; document.body.removeChild(oNewDiv); oDiv.style.left=oDiv2.style.left; oDiv.style.top=oDiv2.style.top; }; }; }; </script> </head> <body> <div id='div2'> <div id="div1"> </div> </div> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- JS組件Bootstrap ContextMenu右鍵菜單使用方法
- JS實(shí)現(xiàn)仿Windows7風(fēng)格的網(wǎng)頁右鍵菜單效果代碼
- 深入探討JavaScript、JQuery屏蔽網(wǎng)頁鼠標(biāo)右鍵菜單及禁止選擇復(fù)制
- js禁止頁面復(fù)制功能禁用頁面右鍵菜單示例代碼
- JavaScript 對(duì)任意元素,自定義右鍵菜單的實(shí)現(xiàn)方法
- ExtJs grid行 右鍵菜單的兩種方法
- 利用JS重寫Cognos右鍵菜單的實(shí)現(xiàn)代碼
- 再次談?wù)揜eact.js實(shí)現(xiàn)原生js拖拽效果引起的一系列問題
- 基于JavaScript實(shí)現(xiàn)右鍵菜單和拖拽功能
相關(guān)文章
使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果
這篇文章主要介紹了使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果的相關(guān)資料,需要的朋友可以參考下2017-01-01HTML5canvas 繪制一個(gè)圓環(huán)形的進(jìn)度表示實(shí)例
這篇文章主要介紹了HTML5canvas繪制一個(gè)圓環(huán)形的進(jìn)度表示實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12使用原生JS實(shí)現(xiàn)滾輪翻頁效果的示例代碼
這篇文章主要介紹了使用原生JS實(shí)現(xiàn)滾輪翻頁效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05javascript charAt() arr[i]數(shù)組實(shí)例代碼
實(shí)例區(qū)別一下charAt()和arr[i].toString()的使用方法2008-08-08