首頁 web前端 js教程 圖片旋轉、滑鼠滾輪縮放、鏡像、切換圖片js程式碼_javascript技巧

圖片旋轉、滑鼠滾輪縮放、鏡像、切換圖片js程式碼_javascript技巧

May 16, 2016 pm 03:19 PM
js 圖片旋轉 鏡像

本文实例为大家展示了图片旋转、鼠标滚轮缩放、镜像、切换图片多重效果,提供了详细的代码,分享给大家供大家参考,具体内容如下

具体代码:

<!DOCTYPE html>
<html lang="zh-cn">
 <head>
  <title>图片旋转,鼠标滚轮缩放,镜像,切换图片</title>
  <meta charset="utf-8" />
  <!--<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>-->
  <script type="text/javascript" src="js/abc.js"></script>
 </head>

 <body>

  <h1 style="text-align: center;color: blue;">效果预览</h1>
  <script>
   //容器对象
   var ImageTrans = function(container, options) {
    this._initialize(container, options);
    this._initMode();
    if (this._support) {
     this._initContainer();
     this._init();
    } else { //模式不支持
     this.onError("not support");
    }
   };
   ImageTrans.prototype = {
    //初始化程序
    _initialize: function(container, options) {
     var container = this._container = $$(container);
  this._clientWidth = container.clientWidth; //变换区域宽度
  this._clientHeight = container.clientHeight; //变换区域高度
  this._img = new Image(); //图片对象
  this._style = {}; //备份样式
  this._x = this._y = 1; //水平/垂直变换参数
  this._radian = 0; //旋转变换参数
  this._support = false; //是否支持变换
  this._init = this._load = this._show = this._dispose = $$.emptyFunction;
     var opt = this._setOptions(options);
     this._zoom = opt.zoom;
     this.onPreLoad = opt.onPreLoad;
     this.onLoad = opt.onLoad;
     this.onError = opt.onError;
     this._LOAD = $$F.bind(function() {
  this.onLoad();
  this._load();
  this.reset();
  this._img.style.visibility = "visible";
  }, this);
  $$CE.fireEvent(this, "init");
    },
    //设置默认属性
    _setOptions: function(options) {
     this.options = { //默认值
      mode: "css3|filter|canvas",
      zoom: .1, //缩放比率
      onPreLoad: function() {}, //图片加载前执行
      onLoad: function() {}, //图片加载后执行
      onError: function(err) {} //出错时执行
     };
     return $$.extend(this.options, options || {});
 },
 //模式设置
 _initMode: function() {
  var modes = ImageTrans.modes;
  this._support = $$A.some(this.options.mode.toLowerCase().split("|"), function(mode) {
  mode = modes[mode];
  if (mode && mode.support) {
  mode.init && (this._init = mode.init); //初始化执行程序
  mode.load && (this._load = mode.load); //加载图片执行程序
  mode.show && (this._show = mode.show); //变换显示程序
  mode.dispose && (this._dispose = mode.dispose); //销毁程序
  //扩展变换方法
  $$A.forEach(ImageTrans.transforms, function(transform, name) {
  this[name] = function() {
   transform.apply(this, [].slice.call(arguments));
   this._show();
  }
  }, this);
  return true;
  }
  }, this);
 },
 //初始化容器对象
 _initContainer: function() {
  var container = this._container,
  style = container.style,
  position = $$D.getStyle(container, "position");
  this._style = {
  "position": style.position,
  "overflow": style.overflow
  }; //备份样式
  if (position != "relative" && position != "absolute") {
  style.position = "relative";
  }
  style.overflow = "hidden";
  $$CE.fireEvent(this, "initContainer");
 },
 //加载图片
 load: function(src) {
  if (this._support) {
  var img = this._img,
  oThis = this;
  img.onload || (img.onload = this._LOAD);
  img.onerror || (img.onerror = function() {
  oThis.onError("err image");
  });
  img.style.visibility = "hidden";
  this.onPreLoad();
  img.src = src;
  }
 },
 //重置
 reset: function() {
  if (this._support) {
  this._x = this._y = 1;
  this._radian = 0;
  this._show();
  }
 },
 //销毁程序
 dispose: function() {
  if (this._support) {
  this._dispose();
  $$CE.fireEvent(this, "dispose");
  $$D.setStyle(this._container, this._style); //恢复样式
  this._container = this._img = this._img.onload = this._img.onerror = this._LOAD = null;
  }
 }
 };
 //变换模式
 ImageTrans.modes = function() {
 var css3Transform; //ccs3变换样式
 //初始化图片对象函数
 function initImg(img, container) {
  $$D.setStyle(img, {
  position: "absolute",
  border: 0,
  padding: 0,
  margin: 0,
  width: "auto",
  height: "auto", //重置样式
  visibility: "hidden" //加载前隐藏
  });
  container.appendChild(img);
 }
 //获取变换参数函数
 function getMatrix(radian, x, y) {
  var Cos = Math.cos(radian),
  Sin = Math.sin(radian);
  return {
  M11: Cos * x,
  M12: -Sin * y,
  M21: Sin * x,
  M22: Cos * y
  };
 }
 return {
  css3: { //css3设置
  support: function() {
  var style = document.createElement("div").style;
  return $$A.some(
  ["transform", "MozTransform", "webkitTransform", "OTransform"],
  function(css) {
   if (css in style) {
   css3Transform = css;
   return true;
   }
  });
  }(),
  init: function() {
  initImg(this._img, this._container);
  },
  load: function() {
  var img = this._img;
  $$D.setStyle(img, { //居中
  top: (this._clientHeight - img.height) / 2 + "px",
  left: (this._clientWidth - img.width) / 2 + "px",
  visibility: "visible"
  });
  },
  show: function() {
  var matrix = getMatrix(this._radian, this._y, this._x);
  //设置变形样式
  this._img.style[css3Transform] = "matrix(" + matrix.M11.toFixed(16) + "," + matrix.M21.toFixed(16) + "," + matrix.M12.toFixed(16) + "," + matrix.M22.toFixed(16) + ", 0, 0)";
  },
  dispose: function() {
  this._container.removeChild(this._img);
  }
  },
  filter: { //滤镜设置
  support: function() {
  return "filters" in document.createElement("div");
  }(),
  init: function() {
  initImg(this._img, this._container);
  //设置滤镜
  this._img.style.filter = "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand')";
  },
  load: function() {
  this._img.onload = null; //防止ie重复加载gif的bug
  this._img.style.visibility = "visible";
  },
  show: function() {
  var img = this._img;
  //设置滤镜
  $$.extend(
  img.filters.item("DXImageTransform.Microsoft.Matrix"),
  getMatrix(this._radian, this._y, this._x)
  );
  //保持居中
  img.style.top = (this._clientHeight - img.offsetHeight) / 2 + "px";
  img.style.left = (this._clientWidth - img.offsetWidth) / 2 + "px";
  },
  dispose: function() {
  this._container.removeChild(this._img);
  }
  },
  canvas: { //canvas设置
  support: function() {
  return "getContext" in document.createElement('canvas');
  }(),
  init: function() {
  var canvas = this._canvas = document.createElement('canvas'),
  context = this._context = canvas.getContext('2d');
  //样式设置
  $$D.setStyle(canvas, {
  position: "absolute",
  left: 0,
  top: 0
  });
  canvas.width = this._clientWidth;
  canvas.height = this._clientHeight;
  this._container.appendChild(canvas);
  },
  show: function() {
  var img = this._img,
  context = this._context,
  clientWidth = this._clientWidth,
  clientHeight = this._clientHeight;
  //canvas变换
  context.save();
  context.clearRect(0, 0, clientWidth, clientHeight); //清空内容
  context.translate(clientWidth / 2, clientHeight / 2); //中心坐标
  context.rotate(this._radian); //旋转
  context.scale(this._y, this._x); //缩放
  context.drawImage(img, -img.width / 2, -img.height / 2); //居中画图
  context.restore();
  },
  dispose: function() {
  this._container.removeChild(this._canvas);
  this._canvas = this._context = null;
  }
  }
 };
 }();
 //变换方法
 ImageTrans.transforms = {
 //垂直翻转
 vertical: function() {
  this._radian = Math.PI - this._radian;
  this._y *= -1;
 },
 //水平翻转
 horizontal: function() {
  this._radian = Math.PI - this._radian;
  this._x *= -1;
 },
 //根据弧度旋转
 rotate: function(radian) {
  this._radian = radian;
 },
 //向左转90度
 left: function() {
  this._radian -= Math.PI / 2;
 },
 //向右转90度
 right: function() {
  this._radian += Math.PI / 2;
 },
 //根据角度旋转
 rotatebydegress: function(degress) {
  this._radian = degress * Math.PI / 180;
 },
 //缩放
 scale: function() {
  function getZoom(scale, zoom) {
  return scale > 0 && scale > -zoom &#63; zoom :
  scale < 0 && scale < zoom &#63; -zoom : 0;
  }
  return function(zoom) {
  if (zoom) {
  var hZoom = getZoom(this._y, zoom),
  vZoom = getZoom(this._x, zoom);
  if (hZoom && vZoom) {
  this._y += hZoom;
  this._x += vZoom;
  }
  }
  }
 }(),
 //放大
 zoomin: function() {
  this.scale(Math.abs(this._zoom));
 },
 //缩小
 zoomout: function() {
  this.scale(-Math.abs(this._zoom));
 }
 };
 //拖动旋转
 ImageTrans.prototype._initialize = (function() {
 var init = ImageTrans.prototype._initialize,
  methods = {
  "init": function() {
  this._mrX = this._mrY = this._mrRadian = 0;
  this._mrSTART = $$F.bind(start, this);
  this._mrMOVE = $$F.bind(move, this);
  this._mrSTOP = $$F.bind(stop, this);
  },
  "initContainer": function() {
  $$E.addEvent(this._container, "mousedown", this._mrSTART);
  },
  "dispose": function() {
  $$E.removeEvent(this._container, "mousedown", this._mrSTART);
  this._mrSTOP();
  this._mrSTART = this._mrMOVE = this._mrSTOP = null;
  }
  };
 //开始函数
 function start(e) {
  var rect = $$D.clientRect(this._container);
  this._mrX = rect.left + this._clientWidth / 2;
  this._mrY = rect.top + this._clientHeight / 2;
  this._mrRadian = Math.atan2(e.clientY - this._mrY, e.clientX - this._mrX) - this._radian;
  $$E.addEvent(document, "mousemove", this._mrMOVE);
  $$E.addEvent(document, "mouseup", this._mrSTOP);
  if ($$B.ie) {
  var container = this._container;
  $$E.addEvent(container, "losecapture", this._mrSTOP);
  container.setCapture();
  } else {
  $$E.addEvent(window, "blur", this._mrSTOP);
  e.preventDefault();
  }
 };
 //拖动函数
 function move(e) {
  this.rotate(Math.atan2(e.clientY - this._mrY, e.clientX - this._mrX) - this._mrRadian);
  window.getSelection &#63; window.getSelection().removeAllRanges() : document.selection.empty();
 };
 //停止函数
 function stop() {
  $$E.removeEvent(document, "mousemove", this._mrMOVE);
  $$E.removeEvent(document, "mouseup", this._mrSTOP);
  if ($$B.ie) {
  var container = this._container;
  $$E.removeEvent(container, "losecapture", this._mrSTOP);
  container.releaseCapture();
  } else {
  $$E.removeEvent(window, "blur", this._mrSTOP);
  };
 };
 return function() {
  var options = arguments[1];
  if (!options || options.mouseRotate !== false) {
  //扩展钩子
  $$A.forEach(methods, function(method, name) {
  $$CE.addEvent(this, name, method);
  }, this);
  }
  init.apply(this, arguments);
 }
 })();
 //滚轮缩放
 ImageTrans.prototype._initialize = (function() {
 var init = ImageTrans.prototype._initialize,
  mousewheel = $$B.firefox &#63; "DOMMouseScroll" : "mousewheel",
  methods = {
  "init": function() {
  this._mzZoom = $$F.bind(zoom, this);
  },
  "initContainer": function() {
  $$E.addEvent(this._container, mousewheel, this._mzZoom);
  },
  "dispose": function() {
  $$E.removeEvent(this._container, mousewheel, this._mzZoom);
  this._mzZoom = null;
  }
  };
 //缩放函数
 function zoom(e) {
  this.scale((
  e.wheelDelta &#63; e.wheelDelta / (-120) : (e.detail || 0) / 3
  ) * Math.abs(this._zoom));
  e.preventDefault();
 };
 return function() {
  var options = arguments[1];
  if (!options || options.mouseZoom !== false) {
  //扩展钩子
  $$A.forEach(methods, function(method, name) {
  $$CE.addEvent(this, name, method);
      }, this);
     }
     init.apply(this, arguments);
    }
   })();
  </script>
  <style>
   #idContainer {
    border: 1px solid red;
    width: 1000px;
    height: 500px;
    background: black center no-repeat;
    margin: 0 auto;
   }

   input {
    margin: 10px;
    padding: 10px;
    border: 1px solid red;
    background: yellow;
    color: green;
    font-size: 16px;
   }

   #idSrc {
    width: auto;
   }
  </style>

  <div id="idContainer"></div>
  <input id="idLeft" value="向左旋转" type="button" />
  <input id="idRight" value="向右旋转" type="button" />
  <input id="idVertical" value="垂直翻转" type="button" />
  <input id="idHorizontal" value="水平翻转" type="button" />
  <input id="idReset" value="重置" type="button" />
  <input id="idCanvas" value="使用Canvas" type="button" />
  <input id="idSrc" value="img/07.jpg" type="text" />
  <input id="idLoad" value="换图" type="button" />
  <script>
   (function() {
    var container = $$("idContainer"),
  src = "img/7.jpg",
  options = {
  onPreLoad: function() {
  container.style.backgroundImage = "url('http://images.cnblogs.com/cnblogs_com/cloudgamer/169629/o_loading.gif')";
  },
  onLoad: function() {
  container.style.backgroundImage = "";
  },
  onError: function(err) {
  container.style.backgroundImage = "";
  alert(err);
  }
  },
  it = new ImageTrans(container, options);
 it.load(src);
 //垂直翻转
 $$("idVertical").onclick = function() {
      it.vertical();
     }
     //水平翻转
    $$("idHorizontal").onclick = function() {
  it.horizontal();
  }
  //左旋转
 $$("idLeft").onclick = function() {
      it.left();
     }
     //右旋转
    $$("idRight").onclick = function() {
  it.right();
  }
  //重置
 $$("idReset").onclick = function() {
      it.reset();
     }
     //换图
    $$("idLoad").onclick = function() {
  it.load($$("idSrc").value);
  }
  //Canvas
 $$("idCanvas").onclick = function() {
     if (this.value == "默认模式") {
      this.value = "使用Canvas";
      delete options.mode;
     } else {
      this.value = "默认模式";
      options.mode = "canvas";
     }
     it.dispose();
     it = new ImageTrans(container, options);
     it.load(src);
    }
   })()
  </script>

 </body>

</html>
登入後複製

abc.js

eval(function(p, a, c, k, e, r) {
 e = function(c) {
  return (c < 62 &#63; '' : e(parseInt(c / 62))) + ((c = c % 62) > 35 &#63; String.fromCharCode(c + 29) : c.toString(36))
 };
 if ('0'.replace(0, e) == 0) {
  while (c--) r[e(c)] = k[c];
  k = [function(e) {
   return r[e] || e
  }];
  e = function() {
   return '([3-59cf-hj-mo-rt-yCG-NP-RT-Z]|[12]\\w)'
  };
  c = 1
 };
 while (c--)
  if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]);
 return p
}('4 $$,$$B,$$A,$$F,$$D,$$E,$$CE,$$S;(3(1K){4 O,B,A,F,D,E,CE,S;O=3(id){5"2f"==1L id&#63;G
登入後複製

以上就是js代码实现图片旋转、鼠标滚轮缩放、镜像、切换图片等效果的代码,希望对大家学习javascript程序设计有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

ao3鏡像官方網站入口 ao3鏡像官方網站入口 Feb 24, 2024 am 11:34 AM

ao3鏡像是一個可以創作同人的平台,但多數的朋友不知道ao3鏡像的官方王站在哪裡,點擊https://ao3.cubeart.club/連結即可進入到ao3鏡像網站中,接下來就是小編為用戶帶來的ao3鏡像2024最新官方網站入口介紹,有興趣的用戶快來一起看看吧! ao3鏡像官方網站入口網站入口:https://ao3.cubeart.club/一、下載地址1、AO3:點擊下載》》2、AO3最新版:點擊下載》》二、進入網站方法1、複製網站到瀏覽器中查看,點選頁面右上角的【LogIn】進入;2、賬

CentOS7各版本鏡像下載地址及版本說明(包括Everything版) CentOS7各版本鏡像下載地址及版本說明(包括Everything版) Feb 29, 2024 am 09:20 AM

載CentOS-7.0-1406的時候,有很多可選則的版本,對於普通用戶來說,不知道選擇哪個好,下面做一下簡單介紹:(1)CentOS-xxxx-LiveCD.ios和CentOS-xxxx- bin-DVD.iso有什麼差別?前者只有700M,後者有3.8G。其差異不僅在大小上,其更本質的差異是,CentOS-xxxx-LiveCD.ios只能載入到記憶體裡運行,不能安裝。 CentOS-xxx-bin-DVD1.iso才可以安裝到硬碟上。 (2)CentOS-xxx-bin-DVD1.iso,Ce

如何使用JS和百度地圖實現地圖平移功能 如何使用JS和百度地圖實現地圖平移功能 Nov 21, 2023 am 10:00 AM

如何使用JS和百度地圖實現地圖平移功能百度地圖是一款廣泛使用的地圖服務平台,在Web開發中經常用於展示地理資訊、定位等功能。本文將介紹如何使用JS和百度地圖API實作地圖平移功能,並提供具體的程式碼範例。一、準備工作使用百度地圖API前,首先需要在百度地圖開放平台(http://lbsyun.baidu.com/)上申請一個開發者帳號,並建立一個應用程式。創建完成

建議:優秀JS開源人臉偵測辨識項目 建議:優秀JS開源人臉偵測辨識項目 Apr 03, 2024 am 11:55 AM

人臉偵測辨識技術已經是一個比較成熟且應用廣泛的技術。而目前最廣泛的網路應用語言非JS莫屬,在Web前端實現人臉偵測辨識相比後端的人臉辨識有優勢也有弱勢。優點包括減少網路互動、即時識別,大大縮短了使用者等待時間,提高了使用者體驗;弱勢是:受到模型大小限制,其中準確率也有限。如何在web端使用js實現人臉偵測呢?為了實現Web端人臉識別,需要熟悉相關的程式語言和技術,如JavaScript、HTML、CSS、WebRTC等。同時也需要掌握相關的電腦視覺和人工智慧技術。值得注意的是,由於Web端的計

如何使用PHP和JS創建股票蠟燭圖 如何使用PHP和JS創建股票蠟燭圖 Dec 17, 2023 am 08:08 AM

如何使用PHP和JS創建股票蠟燭圖股票蠟燭圖是股票市場中常見的技術分析圖形,透過繪製股票的開盤價、收盤價、最高價和最低價等數據,幫助投資者更直觀地了解股票的價格波動情形。本文將教你如何使用PHP和JS創建股票蠟燭圖,並附上具體的程式碼範例。一、準備工作在開始之前,我們需要準備以下環境:1.一台運行PHP的伺服器2.一個支援HTML5和Canvas的瀏覽器3

股票分析必備工具:學習PHP和JS繪製蠟燭圖的步驟 股票分析必備工具:學習PHP和JS繪製蠟燭圖的步驟 Dec 17, 2023 pm 06:55 PM

股票分析必備工具:學習PHP和JS繪製蠟燭圖的步驟,需要具體程式碼範例隨著網路和科技的快速發展,股票交易已成為許多投資者的重要途徑之一。而股票分析是投資人決策的重要一環,其中蠟燭圖被廣泛應用於技術分析。學習如何使用PHP和JS繪製蠟燭圖將為投資者提供更多直觀的信息,幫助他們更好地做出決策。蠟燭圖是一種以蠟燭形狀來展示股票價格的技術圖表。它展示了股票價格的

華為、浪潮等單位合作創建的開源容器鏡像中心,AtomHub,宣布正式開放公測,可穩定下載國內服務 華為、浪潮等單位合作創建的開源容器鏡像中心,AtomHub,宣布正式開放公測,可穩定下載國內服務 Jan 02, 2024 pm 03:54 PM

華為官方消息顯示,開放原子開發者大會以「一切為了開發者」為主題,在無錫舉辦了兩天,時間為12月16日至17日會上,由開放原子開源基金會主導,華為、浪潮、DaoCloud、諧雲、青雲、颶風引擎以及OpenSDV開源聯盟、openEuler社群、OpenCloudOS社群等成員單位共同發起建置的AtomHub可信任鏡像中心正式開放公測。 AtomHub秉承共建、共治、共享的理念,旨在為開源組織和開發者提供中立、開放共建的可信開源容器鏡像中心。鑑於DockerHub等鏡像倉庫的不穩定性和不可控性,以及一些

如何使用JS和百度地圖實現地圖點擊事件處理功能 如何使用JS和百度地圖實現地圖點擊事件處理功能 Nov 21, 2023 am 11:11 AM

如何使用JS和百度地圖實現地圖點擊事件處理功能概述:在網路開發中,經常需要使用地圖功能來展示地理位置和地理資訊。而地圖上的點擊事件處理是地圖功能中常用且重要的一環。本文將介紹如何使用JS和百度地圖API來實現地圖的點擊事件處理功能,並給出具體的程式碼範例。步驟:匯入百度地圖的API檔案首先,要在HTML檔案中匯入百度地圖API的文件,可以透過以下程式碼實現:

See all articles