javascript - jquery css how to retain position if you remove margin -
i poked around , couldn't find question in particular, if missed please kindly point me in right direction.
purpose: i'm in process of creating statistics reporting system our website. when first access it, has dashboard type of layout overall statistics, , graphs go along it. wanted make each individual set of stats/graphs can moved around page see fit, though starts out in center unless move it. i'm using jquery , jquery ui purpose.
problem: initial view, i'm using margin: 0 auto
center divs on page. first problem had when go drag it, couldn't drag left because of margin that's set on it. used draggable
event property start:
remove margin
property div. fixed problem not being able move around expect, when first begin drag it, removal of margin forces div jump left side of screen , have click again.
is there way prevent "jump" , force div retain current position while margin property being removed? going of in wrong way? appreciated, if tell me not go way , figure else out yourself.
thanks in advance!
jquery:
<script> $(document).ready(function () { $.post("getstats.php", function (data) { var ctx = document.getelementbyid("mychart").getcontext("2d"); var mynewchart = new chart(ctx).pie(data); }, 'json'); var contpos = $("#statoverallcont").position(); $("#statoverallcont").draggable({ snap: ".snaptarget", snapmode: "both", containment: "body", drag: function () { $("#statoverallcont").css("margin", ""); $("#statoverallcont").css("left", contpos.left); $("#statoverallcont").css("top", contpos.top); } }); }); </script>
^i tried grab original position, , attempt change position after margin removed. didn't seem @ all.
the div w/ style:
<div id="statoverallcont" style="background-color: #dbdbdb; position: relative; display: table; margin: 0 auto; text-align: center; clear:both; padding: 10px;border: 2px solid black" >
the drag function provides parameters it's callback. event
, ui
. ui
has property called ui.position
contains left
, top
. when drag strats, remove margins, make position of element absolute , set position values taken parameters!
looks this
drag: function (event,ui) { $("#statoverallcont").css("position", "absolute"); $("#statoverallcont").css("left", ui.position.left); $("#statoverallcont").css("top", ui.position.top); }
this should stick element mouse when dragged.
update :
you need update css removing margin:0px auto; , using left property of css set 50% horizontally centered. allow drag element without making jump. btw idea grasped fata1err0r win win situation. :d
Comments
Post a Comment