本文实例讲述了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 src="/static/imghwm/default1.png" data-src="images/up.gif" class="lazy" title="向上" style="max-width:90%" onClick="clickMove('down')" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> <img src="/static/imghwm/default1.png" data-src="images/left.gif" class="lazy" title="向左" style="max-width:90%" onClick="clickMove('right')" style="max-width:90%" style="max-width:90%"> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom.gif" class="lazy" title="还原" style="max-width:90%" onClick="realsize();" style="max-width:90%" style="max-width:90%"> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/right.gif" class="lazy" title="向右" style="max-width:90%" onClick="clickMove('left')" style="max-width:90%" style="max-width:90%"> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/down.gif" class="lazy" title="向下" style="max-width:90%" onClick="clickMove('up')" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom_in.gif" class="lazy" title="放大" style="max-width:90%" onClick="bigit();" style="max-width:90%" style="max-width:90%"> </td> <td> </td> </tr> <tr> <td> </td> <td> <img src="/static/imghwm/default1.png" data-src="images/zoom_out.gif" class="lazy" title="缩小" style="max-width:90%" onClick="smallit();" style="max-width:90%" style="max-width:90%"> </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="/static/imghwm/default1.png" data-src="http://ww2.sinaimg.cn/large/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg" class="lazy" border="0" name="images2" alt="js实现图片放大和拖拽特效代码分享_javascript技巧" > </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 src="/static/imghwm/default1.png" data-src="images/adde8400gw1eeazlmtqa8j20qd0mdadm.jpg" class="lazy" onmousewheel="return onWheelZoom(this)" style="max-width:90%" 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 = 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实现图片放大和拖拽特效代码,希望大家可以喜欢。

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

因此,在这里,您准备好了解所有称为Ajax的东西。但是,到底是什么? AJAX一词是指用于创建动态,交互式Web内容的一系列宽松的技术。 Ajax一词,最初由Jesse J创造

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

将矩阵电影特效带入你的网页!这是一个基于著名电影《黑客帝国》的酷炫jQuery插件。该插件模拟了电影中经典的绿色字符特效,只需选择一张图片,插件就会将其转换为充满数字字符的矩阵风格画面。快来试试吧,非常有趣! 工作原理 插件将图片加载到画布上,读取像素和颜色值: data = ctx.getImageData(x, y, settings.grainSize, settings.grainSize).data 插件巧妙地读取图片的矩形区域,并利用jQuery计算每个区域的平均颜色。然后,使用

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

本文将引导您使用jQuery库创建一个简单的图片轮播。我们将使用bxSlider库,它基于jQuery构建,并提供许多配置选项来设置轮播。 如今,图片轮播已成为网站必备功能——一图胜千言! 决定使用图片轮播后,下一个问题是如何创建它。首先,您需要收集高质量、高分辨率的图片。 接下来,您需要使用HTML和一些JavaScript代码来创建图片轮播。网络上有很多库可以帮助您以不同的方式创建轮播。我们将使用开源的bxSlider库。 bxSlider库支持响应式设计,因此使用此库构建的轮播可以适应任何

数据集对于构建API模型和各种业务流程至关重要。这就是为什么导入和导出CSV是经常需要的功能。在本教程中,您将学习如何在Angular中下载和导入CSV文件


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境