


The example in this article describes the js implementation of image enlargement and drag-and-drop special effects code. Share it with everyone for your reference. The details are as follows:
js is a very practical js special effect that implements image magnification and dragging. It realizes the magnification and dragging functions of images. It does not use the jquery plug-in. It is implemented with native javascript. In addition to clicking the zoom in and out buttons to control the image. You can also use the mouse wheel to zoom in or out the image.
运行效果图: ----------------------查看效果 源码下载-------- ---------------
Tips: If the browser does not work properly, you can try switching the browsing mode.
The code for implementing image magnification and dragging effects using js shared with everyone is as follows
<!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 implementation of image magnification and dragging special effects code sharing_javascript skills" > </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>
The core code is as follows:
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 key code:
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; }
The above is the js code to implement image enlargement and drag-and-drop special effects shared with you. I hope you like it.

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
