$(function() {
//global vars to track current interaction info
var dragging = false;
var lastE;
var dragWhat;
var dragOffX;
var dragOffY;
//listen for mousedown on a campaign placeholder
$('.campaign').mousedown(function(e){
//figure out where on the placeholder the user clicked
dragOffX = e.pageX - $($(e.currentTarget)[0]).offset().left;
dragOffY = e.pageY - $($(e.currentTarget)[0]).offset().top;
//set global dragging to true
dragging = true;
//set global var with target of current drag interaction
dragWhat = e.currentTarget;
});
//listen for mousemove and mouse up on body so that no matter where you mouse is when you let go it will register
$('body').mousemove(function(e){
lastE = e;
}).mouseup(function(e){
//you have released the mouse and aren't dragging anymore, so stop dragging!
placeThingFinal(lastE);
dragging = false;
dragWhat = null;
});
//move the thing to where the mouse is (roughly)
var placeThing = function placeThing(e) {
//determine the coordinates of the thing are based on where the mouse is and the offset between where the mouse is and the thing is
var newX = e.pageX - dragOffX;
var newY = e.pageY - dragOffY;
//choose which column to highlight. De-highlight all other columns. You do not want to build on this if statement, your solution should use... something else...
if(e.pageX <= 320){
$('#boundary1').css({'background-color':'#FFD'});
$('#boundary2').css({'background-color':'white'});
}else if(e.pageX > 320){
$('#boundary1').css({'background-color':'white'});
$('#boundary2').css({'background-color':'#FFD'});
}
//keep the thing inside of the bounds of the draggable areas (keep the 20px gutters in mind)
if(newX >= 40 && newX <= 440){
$(dragWhat).offset({ left: newX });
}else if(newX < 40){
$(dragWhat).offset({ left: 40 });
}else{
$(dragWhat).offset({ left: 440 });
}
if(newY >= 240 && newY <=...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.