javascript - Specific start and end 'x' coordinates for a Draggable div inside a parent div with overflow:hidden -
i'm having trouble trying create formula universal within responsive website. using bootstrap.
specifically, have "timeline" div inside border div. have draggable. trying make stop dragging when reaches specific point on either side of timeline.
see here: draggable timeline
dragging timeline right towards first year no problem. have working. stops dragging supposed to.
however, dragging timeline left towards last year, stops dragging in different spot depending on window size.
when think have working @ 1 window size, resize window , off.
in images below, end result trying achieve. timeline should have 15px margin on either end.
the end position (not working)
the timeline dynamically generated javascript. problem keep @ 5000.4px in width.
this html:
<div id="timeline-border" class="timeline-border"> <div id="timeline" class="timeline"> </div><!-- end timeline --> </div><!-- end timeline-border -->
here scrolling code.
var timeline = document.getelementbyid("timeline"); //set timeline width var timelinewidth = (numofmonths*16.668); timeline.setattribute("style","width:"+timelinewidth+"px;"); var zindex = 0; //timeline width: 5000.4 console.log("timelinewidth: "+timelinewidth); // timeline parent width: 750 var timelineborderwidth = document.getelementbyid("timeline-border").offsetwidth; console.log("timelineborderwidth: "+timelineborderwidth); //timelinevisible: 735 var timelinevisible = timelineborderwidth - 15; console.log("timelinevisible: "+timelinevisible); //timeline hidden length: 4265.4 var timelinehidden = timelinewidth - (timelineborderwidth-15); console.log("timelinehidden: "+timelinehidden); //timeline offset left: 96.5 var timelineleftpos = $("#timeline").offset().left; console.log("timelineoffsetleft: "+timelineleftpos); //timeline offset left: 81.5 var timelineleftposition = $('#timeline').position().left; console.log("timelineleftposition: "+timelineleftposition); //timeline offset parent: 16 var elem = $("#timeline"); var offset = elem.offset().left - elem.parent().offset().left; console.log("timeline offset parent: "+offset); // magicxnum: -4361.9 var magicxnum = timelinehidden + timelineleftpos; console.log("magicxnum: -"+magicxnum); $( "#timeline" ).draggable({ axis: 'x', scroll: false, drag: function(event, ui) { var offset = $(this).offset(); var xpos = offset.left; $('#posx').text('#timeline offset.left: ' + xpos); if (ui.position.left >= 0) { ui.position.left = 0; } else if (ui.position.left <= -magicxnum){ //small size -4632){ ui.position.left = -magicxnum;// small size -4632; } }, stop: function(event, ui) { } });
here console output.
timelinewidth: 5000.4 timelineborderwidth: 750 timelinevisible: 735 timelinehidden: 4265.4 timelineoffsetleft: 96.5 timelineleftposition: 81.5 timeline offset parent: 16 magicxnum: -4361.9
goto line 53: subtraction , addition there like:
line 53: var magicxnum = timelinewidth - timelineborderwidth + 30;
now has update every time when user stretches window..
so magicxnumber dynamicmagicxnumber adding below line 53:
$(window).resize(function() { timelineborderwidth = document.getelementbyid("timeline-border").offsetwidth; magicxnum = timelinewidth - timelineborderwidth + 30; console.log("magicxnum: "+magicxnum); });
bootstrap makes timeline jump out of place when stretched out.
are going add marker on calendar? when jump timeline centers marker?
Comments
Post a Comment