>

js达成图片放大和拖拽特效代码分享,原生JS完结

- 编辑:正版管家婆马报彩图 -

js达成图片放大和拖拽特效代码分享,原生JS完结

正文实例为大家讲明了JS达成拖拽图片效果的详细代码,分享给我们供大家参谋,具体内容如下

本文实例呈报了js实现图片放大和拖拽特效代码。分享给大家供大家参谋。具体如下:
js完毕图片放大和拖拽特效是一款特别实用的js特效,达成了图片的松手和拖拽功效,没用用到jquery插件,是用原生javascript完毕的,除了点击放大和压缩按钮来决定图片的推广缩短,还足以接纳鼠标的滚轮调整图片的缩放。
运作效果图:                               ----------------------查看效果 源码下载-----------------------

javascript event 对象的clientX,offsetX,screenX,pageX的区别:

图片 1

图片 2

小提醒:浏览器中只要不可能健康运行,能够尝试切换浏览形式。
 为大家大饱眼福的js完成图片放大和拖拽特效代码如下

用html5的drag来贯彻拖拽有包容性难点,使用拖拽插件代码又比非常多,而这一个拖拽demo代码少,并且同盟全数浏览器,很值得在等级次序中选取,

<!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>

css样式如下:

大旨代码如下:

  #div1{ 
    width: 100px; height: 100px; 
    background-color: #4D4D4D; 
    position: absolute; cursor: pointer; 
    -webkit-box-shadow: 3px 3px 0px 3px #C7C7C7; 
    box-shadow: 3px 3px 3px 0px #C7C7C7; 
  }
  #parent{ 
    width: 500px; height: 500px; 
    border: 1px solid #CDCDCD; 
    position: relative; margin: 0 auto;  //父级元素设置为:
    background-color: #F4F4F4; 
  }
  .postText{
    width: 500px; height: 30px;margin: 0 auto; background-color: #F4F4F4;
  }
  .postText span{ 
   padding:0px 10px;
  }
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;
}

html代码如下:

js关键代码:

<div id="parent">
  <div id="div1" onmousemove="posMove(this.id)"></div>
</div>
<div class="postText">
  移动的距离Top:Left:
</div>
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代码及注释如下

以上即是为大家享用的js完毕图片放大和拖拽特效代码,希望我们能够欣赏。

function posMove(getdivid) {
    var oDiv = document.getElementById(getdivid);
    var oParent = document.getElementById('parent');
    var sent = {
      l: 10, //设置div在父元素的活动范围,10相当于给父div设置padding-left:10;
      r: oParent.offsetWidth - oDiv.offsetWidth, // offsetWidth:当前对象的宽度, offsetWidth = width+padding+border
      t: 10,
      b: oParent.offsetHeight - oDiv.offsetHeight,
      n: 10
    }
    drag(oDiv, sent); 
  }

  /**
   *
   * @param obj:被拖动的div
   * @param sent :设置div在容器中可以被拖动的区域
   */
  function drag(obj,sent){

    var dmW = document.documentElement.clientWidth || document.body.clientWidth;
    var dmH = document.documentElement.clientHeight || document.body.clientHeight;

    var sent = sent || {};
    var l = sent.l || 0;
    var r = sent.r || dmW - obj.offsetWidth;
    var t = sent.t || 0;
    var b = sent.b || dmH - obj.offsetHeight;
    var n = sent.n || 10;

    obj.onmousedown = function (ev){
      var oEvent = ev || event;
      var sentX = oEvent.clientX - obj.offsetLeft;
      var sentY = oEvent.clientY - obj.offsetTop;

      document.onmousemove = function (ev){
        var oEvent = ev || event;

        var slideLeft = oEvent.clientX - sentX;
        var slideTop = oEvent.clientY - sentY;

        if(slideLeft <= l){
          slideLeft = l;
        }
        if(slideLeft >= r){
          slideLeft = r;
        }
        if(slideTop <= t){
          slideTop = t;
        }
        if(slideTop >= b){
          slideTop = b;
        }

        obj.style.left = slideLeft + 'px';
        obj.style.top = slideTop + 'px';

        document.getElementById('posTop').innerHTML = slideTop;
        document.getElementById('posLeft').innerHTML = slideLeft;

      };
      document.onmouseup = function (){
        document.onmousemove = null;
        document.onmouseup = null;
      }

      return false;
    }
  }  

你或者感兴趣的篇章:

  • JavaScript 图片放大镜(可拖放、缩放效果)
  • js达成图片拖动更换各类附图
  • js达成选取鼠标拖拽切换图片的艺术
  • js完毕鼠标拖动图片并包容IE/FF火狐/Google等主流浏览器
  • javascript 网页编辑框及拖拽图片的难点
  • 原生JS完结拖拽图片效果
  • JS HTML5拖拽上传图片预览
  • JS实现放大、减少及拖拽图片的不二等秘书技【可包容IE、火狐】
  • JavaScript达成文字与图片拖拽效果的法子
  • JavaScript落到实处图片拖曳效果

如上就是关于js拖拽效果的贯彻代码,希望对我们的就学抱有扶助。

您可能感兴趣的小说:

  • JavaScript 图片放大镜(可拖放、缩放效果)
  • js达成图片拖动退换各种附图
  • js实现应用鼠标拖拽切换图片的章程
  • js达成图片放大和拖拽特效代码分享
  • js完结鼠标拖动图片并包容IE/FF火狐/谷歌(Google)等主流浏览器
  • javascript 网页编辑框及拖拽图片的难点
  • JS HTML5拖拽上传图片预览
  • JS实现放大、收缩及拖拽图片的措施【可包容IE、火狐】
  • JavaScript落实文字与图片拖拽效果的形式
  • JavaScript贯彻图片拖曳效果

本文由关于计算机发布,转载请注明来源:js达成图片放大和拖拽特效代码分享,原生JS完结