js实现图片放大和拖拽特效代码分享

本文实例讲述了js实现图片放大和拖拽特效代码。分享给大家供大家参考。具体如下:
js实现图片放大和拖拽特效是一款非常实用的js特效,实现了图片的放大和拖拽功能,没用用到jquery插件,是用原生javascript实现的,除了点击放大和缩小按钮来控制图片的放大缩小,还可以使用鼠标的滚轮控制图片的缩放。
运行效果图:                               ----------------------查看效果 源码下载-----------------------

js实现图片放大和拖拽特效代码分享

小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式。
 为大家分享的js实现图片放大和拖拽特效代码如下

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js实现图片放大和拖拽特效</title> <meta content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <link href="https://www.jb51.net/css/style.css"> <script type="text/javascript" src="https://www.jb51.net/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 onmouseup="document.selection.empty()" oncontextmenu="return false" onselectstart="return false" ondragstart="return false" onbeforecopy="return false" oncopy="document.selection.empty()" leftmargin="0" topmargin="0" onselect="document.selection.empty()" marginheight="0" marginwidth="0"> <div> <table cellspacing="2" cellpadding="0"> <tbody> <tr> <td>&nbsp; </td> <td> <img title="向上" src="https://www.jb51.net/images/up.gif"> </td> <td>&nbsp; </td> </tr> <tr> <td> <img title="向左" src="https://www.jb51.net/images/left.gif"> </td> <td> <img title="还原" src="https://www.jb51.net/images/zoom.gif"> </td> <td> <img title="向右" src="https://www.jb51.net/images/right.gif"> </td> </tr> <tr> <td>&nbsp; </td> <td> <img title="向下" src="https://www.jb51.net/images/down.gif"> </td> <td>&nbsp; </td> </tr> <tr> <td>&nbsp; </td> <td> <img title="放大" src="https://www.jb51.net/images/zoom_in.gif"> </td> <td>&nbsp; </td> </tr> <tr> <td>&nbsp; </td> <td> <img title="缩小" src="https://www.jb51.net/images/zoom_out.gif"> </td> <td>&nbsp; </td> </tr> </tbody> </table> </div> <p> <br> </p> <div> <img src="https://ww2.sinaimg.cn/large/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg"> </div> <div drag="0"> <img onmousewheel="return onWheelZoom(this)" src="https://www.jb51.net/images/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg"> </div> <div> </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; }

js关键代码:

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实现图片放大和拖拽特效代码,希望大家可以喜欢。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggdjg.html