JS實現放大、縮小及拖拽圖片的方法【可相容IE、Firefox】,js拖拽

來源:互聯網
上載者:User

JS實現放大、縮小及拖拽圖片的方法【可相容IE、Firefox】,js拖拽

本文執行個體講述了JS實現放大、縮小及拖拽圖片的方法。分享給大家供大家參考,具體如下:

var divId;var v_left;var v_top;window.onload=function(){divId = document.getElementById("block1");var height1 = images1.height;//圖片的高度var width1 = images1.width;//圖片的寬度v_left=(document.body.clientWidth-width1)/2;v_top=(document.body.clientHeight-height1)/2;divId.style.left=v_left;divId.style.top=v_top;}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;//document.onmouseup=MUp;// 事件會在滑鼠按鍵被鬆開時發生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() {//將圖片縮小,失去熱點height1 = images1.height;width1 = images1.width;images1.height = height1 / 1.1;images1.width = width1 / 1.1;}//放大倍數function bigit() {/*//將圖片放大,不失熱點var zoom = parseInt(images1.style.zoom, 10) || 100;zoom += event.wheelDelta / 12;if (zoom > 0) {images1.style.zoom = (zoom+10) + "%";}*///將圖片放大,失去熱點height1 = images1.height;width1 = images1.width;images1.height = height1 * 1.1;images1.width = width1 * 1.1;}//還原function realsize() {images1.style.zoom=100+"%";images1.height = images2.height;images1.width = images2.width;divId.style.left=v_left;divId.style.top=v_top;}function featsize() {var width1 = images2.width;var height1 = images2.height;var width2 = 360;var height2 = 200;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 bbimg(o) {/*var eleLeft;var eleTop;if(document.documentElement){eleLeft = document.documentElement.scrollLeft;eleTop = document.documentElement.scrollTop;}else{eleLeft = document.body.scrollLeft;eleTop = document.body.scrollTop;}v_left+=eleLeft;v_top+=eleTop;//divId.style.left=v_left;//divId.style.top=v_top;alert(document.documentElement.scrollTop +"----xxx"+document.body.scrollTop+window.pageyoffset);*/var zoom = parseInt(o.style.zoom, 10) || 100;zoom += event.wheelDelta / 12;if (zoom > 0) {o.style.zoom = zoom + "%";}return false;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.