I'd like to be able to click hold my mouse inside a div and move it's background. Searched a lot on google and didn't found what I wanted.
Here's the target (the map displayed is the object to drag) : http://pontografico.net/pvt/gamemap/
Any tips?
Cheers!
From the ui draggable demo:
Dragging DOM elements are simple, no need to reinvent the wheel.
http://jqueryui.com/demos/draggable/
EDIT: Draggable background inside a div is also possible. See fiddle here: http://jsfiddle.net/FyFZA/
I know this is an old post, but if you're using jQueryUI another solution is to create a transparent element on top of the background, then use the drag callback to update the backgroundPosition of the original node. Here's an example:
I intentionally left a white background color with an opacity so you could see the element when testing.
Alright, got this to work; I think I got all the kinks out:
Final jQuery with Bounding Limits
http://jsfiddle.net/userdude/q6r8f/4/
Original Answer
HTML
CSS
jQuery
http://jsfiddle.net/userdude/q6r8f/2/
this post was an excellent starting point for my issue. i combined the above answer, and another pertaining to getting the original dimensions of your background image (SO:How do I get background image size in jQuery?)
with my particular setup i was using
background-size:cover
so i had to determine the ratio of height/width of the background image and compare it to the ratio of height/width of the container. this means that each image is only allowed to slide in one dimension.two extras in here you may not need. I added a border to the element I was currently manipulating just to keep it easy to see, removed it when i was done. also I updated an input with the offset values, so i could send them onto the server.
I tend to be very verbose with my comments, so it's a little lengthy, but i hope it helps someone.