JS滚轮控制图片缩放大小和拖动的实例代码
网络编程 2021-07-04 16:47www.168986.cn编程入门
本文通过实例代码给大家介绍了js 滚轮控制图片缩放大小和拖动,代码简单易懂,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
具体代码如下所示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .dragAble { position: relative; cursor: move; } .img-con { position: relative; width: 713px; height: 455px; overflow: hidden; border: 1px solid red; } </style> </head> <body> <p class="img-con"><img src="http://img03.tooopen./uploadfile/downs/images/20110714/sy_20110714135215645030.jpg" class="dragAble" /></p> <script type="text/javascript" charset="utf-8"> window.onload = function() { var oImg = document.getElementsByTagName("img")[0]; function fnWheel(obj, fn) { obj.onmousewheel = fn; if (obj.addEventListener) { obj.addEventListener('DOMMouseScroll', fn, false); } function fn(ev) { var oEvent = ev || window.event; var down = true; if (oEvent.detail) { down = oEvent.detail > 0 } else { down = oEvent.wheelDelta < 0 } if (fn) { fn.call(this, down, oEvent); } if (oEvent.preventDefault) { oEvent.preventDefault(); } return false; } }; fnWheel(oImg, function(down, oEvent) { var oldWidth = this.offsetWidth; var oldHeight = this.offsetHeight; var oldLeft = this.offsetLeft; var oldTop = this.offsetTop; var scaleX = (oEvent.clientX - oldLeft) / oldWidth; //比例 var scaleY = (oEvent.clientY - oldTop) / oldHeight; if (down) { this.style.width = this.offsetWidth 0.9 + "px"; this.style.height = this.offsetHeight 0.9 + "px"; } else { this.style.width = this.offsetWidth 1.1 + "px"; this.style.height = this.offsetHeight 1.1 + "px"; } var newWidth = this.offsetWidth; var newHeight = this.offsetHeight; this.style.left = oldLeft - scaleX (newWidth - oldWidth) + "px"; this.style. = oldTop - scaleY (newHeight - oldHeight) + "px"; }); } <!-- //拖拽 var ie = document.all; var nn6 = document.getElementByIdx && !document.all; var isdrag = false; var y, x; var oDragObj; function moveMouse(e) { if (isdrag) { oDragObj.style. = (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 Element = "HTML"; while (oDragHandle.tagName != Element && oDragHandle.className != "dragAble") { oDragHandle = nn6 ? oDragHandle.parentNode : oDragHandle.parentElement; } if (oDragHandle.className == "dragAble") { isdrag = true; oDragObj = oDragHandle; nTY = parseInt(oDragObj.style. + 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"); </script> </body> </html>
只缩放
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <img border="0" src="http://img03.tooopen./uploadfile/downs/images/20110714/sy_20110714135215645030.jpg" onmousewheel="return rollImg(this)"> </body> <script language="javascript"> function rollImg(o){ / 获取当前页面的缩放比 若未设置zoom缩放比,则为默认100%,即1,原图大小 / var zoom=parseInt(o.style.zoom)||100; / event.wheelDelta 获取滚轮滚动值并将滚动值叠加给缩放比zoom wheelDelta统一为±120,其中正数表示为向上滚动,负数表示向下滚动 / zoom+=event.wheelDelta/12; / 如果缩放比大于0,则将缩放比加载到页面元素 / if (zoom>0) o.style.zoom=zoom+'%'; / 如果缩放比不大于0,则返回false,不执行操作 / return false; } </script> </html>
以上所述是长沙网络推广给大家介绍的JS滚轮控制图片缩放大小和拖动的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程