本文實例講述了js實作圖片放大和拖曳特效程式碼。分享給大家供大家參考。具體如下:
js實現圖片放大和拖曳特效是一款非常實用的js特效,實現了圖片的放大和拖曳功能,沒用到jquery插件,是用原生javascript實現的,除了點擊放大和縮小按鈕來控制圖片的放大縮小,還可以使用滑鼠的滾輪控制圖片的縮放。
運作效果圖: ----------------------看效果 ---------------
小提示:瀏覽器中如果無法正常運作,可以嘗試切換瀏覽模式。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js实现图片放大和拖拽特效</title> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" href="css/style.css"> <script type="text/javascript" src="js/drag_map.js"></script> <style type="text/css"> body{font-size: 12px;font-family: "Verdana" , "Arial" , "Helvetica" , "sans-serif";} td{font-size: 12px; line-height: 150%;} TD{font-size: 12px; color: #000000;} A{font-size: 12px; color: #000000;} #Layer1{z-index: 100; position: absolute; top: 150px;} #Layer2{z-index: 1; position: absolute;} </style> <script type="text/JavaScript"> function MM_reloadPage(init) { if (init == true) with (navigator) { if ((appName == "Netscape") && (parseInt(appVersion) == 4)) { document.MM_pgW = innerWidth; document.MM_pgH = innerHeight; onresize = MM_reloadPage; } } else if (innerWidth != document.MM_pgW || innerHeight != document.MM_pgH) location.reload(); } MM_reloadPage(true); </script> </head> <body onLoad="" onmouseup="document.selection.empty()" oncontextmenu="return false" onselectstart="return false" ondragstart="return false" onbeforecopy="return false" style="overflow-y: hidden; overflow-x: hidden" oncopy="document.selection.empty()" leftmargin="0" topmargin="0" onselect="document.selection.empty()" marginheight="0" marginwidth="0"> <div id="Layer1"> <table cellspacing="2" cellpadding="0" border="0"> <tbody> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/up.gif" class="lazy" title="向上" style="max-width:90%" onClick="clickMove('down')" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> <img src="/static/imghwm/default1.png" data-src="images/left.gif" class="lazy" title="向左" style="max-width:90%" onClick="clickMove('right')" style="max-width:90%" style="max-width:90%"> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom.gif" class="lazy" title="还原" style="max-width:90%" onClick="realsize();" style="max-width:90%" style="max-width:90%"> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/right.gif" class="lazy" title="向右" style="max-width:90%" onClick="clickMove('left')" style="max-width:90%" style="max-width:90%"> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/down.gif" class="lazy" title="向下" style="max-width:90%" onClick="clickMove('up')" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom_in.gif" class="lazy" title="放大" style="max-width:90%" onClick="bigit();" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom_out.gif" class="lazy" title="缩小" style="max-width:90%" onClick="smallit();" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> </tbody> </table> </div> <p> <br> </p> <div id="hiddenPic" style="z-index: 1; left: 0px; visibility: hidden; width: 0px; position: absolute; top: 150px; height: 0px"> <img src="/static/imghwm/default1.png" data-src="http://ww2.sinaimg.cn/large/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg" class="lazy" border="0" name="images2" alt="js實作圖片放大與拖曳特效程式碼分享_javascript技巧" > </div> <div class="dragAble" id="block1" onMouseOver="dragObj=block1; drag=1;" style="z-index: 10; left: 0px; width: 0px; position: absolute; top: 150px; height: 0px" onMouseOut="" drag="0"> <img src="/static/imghwm/default1.png" data-src="images/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg" class="lazy" onmousewheel="return onWheelZoom(this)" style="max-width:90%" border="0" name="images1"> </div> <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';"> </div> </body> </html>
function onWheelZoom(obj){ //滚轮缩放 zoom = parseFloat(obj.style.zoom); tZoom = zoom + (event.wheelDelta>0 ? 0.05 : -0.05); if(tZoom<0.1 ) return true; obj.style.zoom=tZoom; return false; }
drag = 0 move = 0 var ie=document.all; var nn6=document.getElementById&&!document.all; var isdrag=false; var y,x; var oDragObj; function moveMouse(e) { if (isdrag) { oDragObj.style.top = (nn6 ? nTY + e.clientY - y : nTY + event.clientY - y)+"px"; oDragObj.style.left = (nn6 ? nTX + e.clientX - x : nTX + event.clientX - x)+"px"; return false; } } function initDrag(e) { var oDragHandle = nn6 ? e.target : event.srcElement; var topElement = "HTML"; while (oDragHandle.tagName != topElement && oDragHandle.className != "dragAble") { oDragHandle = nn6 ? oDragHandle.parentNode : oDragHandle.parentElement; } if (oDragHandle.className=="dragAble") { isdrag = true; oDragObj = oDragHandle; nTY = parseInt(oDragObj.style.top+0); y = nn6 ? e.clientY : event.clientY; nTX = parseInt(oDragObj.style.left+0); x = nn6 ? e.clientX : event.clientX; document.onmousemove=moveMouse; return false; } } document.onmousedown=initDrag; document.onmouseup=new Function("isdrag=false"); function clickMove(s){ if(s=="up"){ dragObj.style.top = parseInt(dragObj.style.top) + 100; }else if(s=="down"){ dragObj.style.top = parseInt(dragObj.style.top) - 100; }else if(s=="left"){ dragObj.style.left = parseInt(dragObj.style.left) + 100; }else if(s=="right"){ dragObj.style.left = parseInt(dragObj.style.left) - 100; } } function smallit(){ var height1=images1.height; var width1=images1.width; images1.height=height1/1.2; images1.width=width1/1.2; } function bigit(){ var height1=images1.height; var width1=images1.width; images1.height=height1*1.2; images1.width=width1*1.2; } function realsize() { images1.height=images2.height; images1.width=images2.width; block1.style.left = 0; block1.style.top = 0; } function featsize() { var width1=images2.width; var height1=images2.height; var width2=701; var height2=576; var h=height1/height2; var w=width1/width2; if(height1<height2&&width1<width2) { images1.height=height1; images1.width=width1; } else { if(h>w) { images1.height=height2; images1.width=width1*height2/height1; } else { images1.width=width2; images1.height=height1*width2/width1; } } block1.style.left = 0; block1.style.top = 0; } function onWheelZoom(obj){ //滚轮缩放 zoom = parseFloat(obj.style.zoom); tZoom = zoom + (event.wheelDelta>0 ? 0.05 : -0.05); if(tZoom<0.1 ) return true; obj.style.zoom=tZoom; return false; }以上就是為大家分享的js實作圖片放大和拖曳特效程式碼,希望大家可以喜歡。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),