国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現圖片放大和拖拽特效代碼分享

2019-11-20 11:36:51
字體:
來源:轉載
供稿:網友

本文實例講述了js實現圖片放大和拖拽特效代碼。分享給大家供大家參考。具體如下:
js實現圖片放大和拖拽特效是一款非常實用的js特效,實現了圖片的放大和拖拽功能,沒用用到jquery插件,是用原生javascript實現的,除了點擊放大和縮小按鈕來控制圖片的放大縮小,還可以使用鼠標的滾輪控制圖片的縮放。
運行效果圖:                               ----------------------查看效果 源碼下載-----------------------

小提示:瀏覽器中如果不能正常運行,可以嘗試切換瀏覽模式。
 為大家分享的js實現圖片放大和拖拽特效代碼如下

<!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 title="向上" style="cursor: hand" onClick="clickMove('down')" height="20" src="images/up.gif"       width="20">     </td>     <td>            </td>    </tr>    <tr>     <td>      <img title="向左" style="cursor: hand" onClick="clickMove('right')" height="20" src="images/left.gif"       width="20">     </td>     <td>      <img title="還原" style="cursor: hand" onClick="realsize();" height="20" src="images/zoom.gif"       width="20">     </td>     <td>      <img title="向右" style="cursor: hand" onClick="clickMove('left')" height="20" src="images/right.gif"       width="20">     </td>    </tr>    <tr>     <td>            </td>     <td>      <img title="向下" style="cursor: hand" onClick="clickMove('up')" height="20" src="images/down.gif"       width="20">     </td>     <td>            </td>    </tr>    <tr>     <td>            </td>     <td>      <img title="放大" style="cursor: hand" onClick="bigit();" height="20" src="images/zoom_in.gif"       width="20">     </td>     <td>            </td>    </tr>    <tr>     <td>            </td>     <td>      <img title="縮小" style="cursor: hand" onClick="smallit();" height="20" src="images/zoom_out.gif"       width="20">     </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="http://ww2.sinaimg.cn/large/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg" border="0" name="images2"> </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 onmousewheel="return onWheelZoom(this)" style="zoom: 0.7" src="images/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg"   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;}

js關鍵代碼:

drag = 0move = 0var 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實現圖片放大和拖拽特效代碼,希望大家可以喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 含山县| 肃北| 铜山县| 诸城市| 茂名市| 丰宁| 桃园市| 衡东县| 金秀| 崇仁县| 阳山县| 吉木萨尔县| 嘉峪关市| 铜鼓县| 勃利县| 乌拉特中旗| 宜宾市| 江津市| 洛阳市| 陵川县| 宜良县| 通榆县| 东方市| 永登县| 枣强县| 盖州市| 浦东新区| 辽源市| 怀远县| 南召县| 上蔡县| 西青区| 昌黎县| 博客| 容城县| 乐清市| 张家界市| 太白县| 丽江市| 兴安县| 张家界市|