
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to Drag / Pan an Image in a Container div using jQuery?
With the help of events like mousedown, mouseup and mousemove, we can translate the image thereby creating a drag effect.
The following example portrays how an image can be moved using jQuery.
Example
<!DOCTYPE html> <html> <head> <style> #parent{ position: absolute; margin: 20px; width: 200px; height: 200px; border-radius: 25px; background-color: khaki; } #mover { position: relative; margin: 10px; } </style> </head> <body> <div id=parent> <img id="mover" src="https://images.unsplash.com/photo-1613333238609- ef9218f3ddbd?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=100&ixlib=rb1.2.1&q=80&w=100" /> </div> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> let ele = {startPositionX:0,startPositionY:0}; let disp = {x:0,y:0}; $('#parent').on("mousedown",function(e){ let container = $(this); ele.startPositionX=e.pageX-disp.x; ele.startPositionY=e.pageY-disp.y; $(document).on("mousemove",function(e){ disp.x=e.pageX-ele.startPositionX; disp.y=e.pageY-ele.startPositionY; $('#mover').css('transform','scale('+1.0+') translate('+disp.x+'px, '+disp.y+'px)'); }); }); $(document).on("mouseup",function(){ $(this).off("mousemove"); }); </script> </body> </html>
Output
This will produce the following result −
Drag
- Related Articles
- How to auto-resize an image to fit a div container using CSS?
- How to duplicate a div using jQuery?
- How to load a page in div using jQuery?
- How to toggle a div visibility using jQuery?
- How to create div dynamically using jQuery?
- How to print div content using jQuery?
- How to fetch nth div from an HTML page using jQuery?
- How to replace innerHTML of a div using jQuery?
- How to copy the content of a div into another div using jQuery?
- How to scroll to element in horizontal div using jQuery?
- How to put a complete HTML page in a div using jQuery?
- How to replace only text inside a div using jQuery?
- How to check if a div is visible using jQuery?
- How to center a div on the screen using jQuery?
- How to replace innerHTML of a div tag using jQuery?

Advertisements