Using JQuery + EasyDrag to Move Elements or Images by Clicking on Them

This is a simple and easy-to-use jQuery plugin which enables drag and drop functionality to make your site more interactive so that readers can “play” with certain elements on the page by dragging them from one side to another. For example, they can drag the images with a script to move them on any part of the blog just with a mouse click.

Drag and Drop Elements or Images on click with jQuery & EasyDrag

To see how this works, please visit the demo blog and click on any item, then move it anywhere on the screen:

Adding EasyDrag & jQuery to Move Elements or Images in Blogger

1. Login to your Blogger account, go to ‘Template’ and click the ‘Edit HTML’ button:

2. Click anywhere inside the template’s code and press the CTRL + F keys to search for this tag:


3. Just before </head> paste the following scripts:

<script src=’’ type=’text/javascript’/>
<script type=’text/javascript’>
(function($){var isMouseDown=false;var currentElement=null;var dropCallbacks={};var dragCallbacks={};var lastMouseX;var lastMouseY;var lastElemTop;var lastElemLeft;var dragStatus={};$.getMousePosition=function(e){var posx=0;var posy=0;if(!e)var e=window.event;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY;}
else if(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}
return{‘x’:posx,’y’:posy};};$.updatePosition=function(e){var pos=$.getMousePosition(e);var spanX=(pos.x-lastMouseX);var spanY=(pos.y-lastMouseY);$(currentElement).css(“top”,(lastElemTop+spanY));$(currentElement).css(“left”,(lastElemLeft+spanX));};$(document).mousemove(function(e){if(isMouseDown&&dragStatus[]==’on’){$.updatePosition(e);if(dragCallbacks[]!=undefined){dragCallbacks[](e,currentElement);}
return false;}});$(document).mouseup(function(e){if(isMouseDown&&dragStatus[]==’on’){isMouseDown=false;if(dropCallbacks[]!=undefined){dropCallbacks[](e,currentElement);}
return false;}});$.fn.ondrag=function(callback){return this.each(function(){dragCallbacks[]=callback;});};$.fn.ondrop=function(callback){return this.each(function(){dropCallbacks[]=callback;});};$.fn.dragOff=function(){return this.each(function(){dragStatus[]=’off’;});};$.fn.dragOn=function(){return this.each(function(){dragStatus[]=’on’;});};$.fn.easydrag=function(allowBubbling){return this.each(function(){if(||!”easydrag”+(new Date().getTime());dragStatus[]=”on”;$(this).css(“cursor”,”move”);$(this).mousedown(function(e){$(this).css(“position”,”absolute”);$(this).css(“z-index”,”10000″);isMouseDown=true;currentElement=this;var pos=$.getMousePosition(e);lastMouseX=pos.x;lastMouseY=pos.y;lastElemTop=this.offsetTop;lastElemLeft=this.offsetLeft;$.updatePosition(e);return allowBubbling?true:false;});});};})(jQuery);

Note: If you already have jQuery, please remove the code in red.

4. Save the changes by clicking the ‘Save template’ button.

How to Move Elements or Images on Click Using EasyDrag & jQuery

Now, when you want to use EasyDrag to drag and drop an image, use the code below inside the HTML of your post or page (create a New post, then switch to the HTML tab):

<img id=”easydrag1” src=”image-URL” style=”border: 0px none; cursor: move;” />
<script type=”text/javascript”>
$(function(){ $(“#easydrag1“).easydrag();});

Note: change the text in blue with the URL of your image and please note that each image has an unique id. Here, for example, the id is called easeydrag1 which has been added both in the HTML of the image and JavaScript function.

If you need to use EasyDrag to move a second element, then add your image with a different id, for example easydrag2, otherwise it won’t work:

<img id=”easydrag1” src=”image-URL” style=”border: 0px none; cursor: move;” />
<script type=”text/javascript”>
<img id=”easydrag2” src=”image-URL” style=”border: 0px none; cursor: move;” />
<script type=”text/javascript”>
$(function(){ $(“#easydrag1“).easydrag();});
$(function(){ $(“#easydrag2“).easydrag();});

How to add a link to a draggable image?

We will add a JavaScript event, so that when we will double click on the image, to open the page we want.

The code to use should look something this:

<img id=”easydrag1” style=”cursor:move; border:0px;” ondblClick=”‘link-URL‘)” src=”image-URL” /><script type=”text/javascript”>
$(function(){ $(“#easydrag1“).easydrag();});

Using the script above, the image can be dragged around easily and be activated by double clicking on it.

Related Posts

Stop Blogger from Redirecting Blogspot to Country Specific URLs

Let’s say you’re from France and have set up – just for examples sake – a blog called where you share your thoughts and insights on…

Add a Pinterest Pin It Mouseover Button on Blogger Images

Images have always played a prominent role in attracting new visitors through search engines like Google and Bing. But when Pinterest decided to enter the social media…

Add Masonry, Grid Layouts to Blogger Posts with CSS and Javascript

How a website’s content is displayed can spell the difference between being popular and obscure. Anything that looks chaotic or hardly readable is never going to be…

Create Horizontal Navigation Menu With Drop Down Submenus Using CSS

This drop down menu made with CSS only is a horizontal menu with sub-tabs and a rounded search bar on the right side. A menu like this…

How to Center the Blogger Header Image

The header of a website is what distinguishes your blog; it’s your identity or digital fingerprint. Whenever a visitor accesses your blog for the first time, the…

Scriptaculous image slider/carousel for Blogger

Image galleries, sliders and slideshows have become increasingly popular within web pages and more and more developers have been creating these amazing powerful, versatile and sleek galleries….

Leave a Reply

Your email address will not be published.