javascript - Trying to flip the output if element will land off screen -
var $elem = elem; var position = $elem.position(); var output = {}; var winx = $(window).height(); var winy = $(window).width(); output.width = $elem.width(); output.height = $elem.height(); output.bottomleftx = position.left;//left top output.bottomlefty = position.top + output.height;//left bottom output.bottomrightx = position.left + output.width;//right top output.bottomrighty = position.top + output.height;//right bottom if(position.left > 0 && position.top > 0 && position.left+output.width < winy && position.top+output.height < winx) { //something } else { alert('not gonna fit') } return output;
no matter way try this, can't seem right, above last attempt before coming here trying on this. tring position of element in dom, , based on position show element. kind of tool tip different in few aspects. anyway. trying figure out how can flip outputs around if need in event element show going go off screen , in will, want show different direction..
currently element triggers showing hidden element. has elements top left edges touching trigger elements bottom left edges. in event going go off screen such trigger element being down near bottom of page , right want element thats going showing line bottom right edge touching top right edge of trigger element. or if trigger element on left bottom of page want line left top edge of trigger element.. eseentially need cover bases of finding edge , keeping element in window. element being shown 200px 550px wide, , im not sure if in particular tie overall need in code above.
any ways figure stuff out without coming dozen+ logics hey going fall off 1 of edges of window/view port, need reposition it. match
alright, no here, , playing around , getting headache came solution works me. above bit ended putting own function gauge aspects of being passed around, , created function measure elements thats being show , placement on screen. part of problem due being added dynamically dom, , having style setting of display:none
script unable determin or or how placed until physically showing. after show element run second funciton on move needed when needed.
function tippedoffelemposition(elem) { var $elem = elem; var $msg_elem = $elem.next('.tippedoff_msg'); var position = $elem.position(); var offset = $elem.offset(); var output = {}; var win_height = $(window).height(); var win_width = $(window).width(); output.win_height = win_height; output.win_width = win_width; //hover trigger output.elem_width = $elem.width(); output.elem_height = $elem.height(); output.elem_left_top = position.left;//left top output.elem_left_bottom = position.top + output.elem_height;//left bottom output.elem_right_top = position.left + output.elem_width;//right top output.elem_right_bottom = position.top + output.elem_height;//right bottom output.elem_offset_left = offset.left; output.elem_offset_top = offset.top; //final position (default) output.setting_top = output.elem_left_bottom; output.setting_left = output.elem_left_top; /* debugstr = ''; debugstr += 'ref = '+$elem.data('ref')+'<br>'; debugstr += 'win_height = '+win_height+'<br>'; debugstr += 'win_width = '+win_width+'<br>'; debugstr += 'output.elem_width = '+output.elem_width+'<br>'; debugstr += 'output.elem_height = '+output.elem_height+'<br>'; debugstr += 'output.elem_left_top = '+output.elem_left_top+'<br>'; debugstr += 'output.elem_left_bottom = '+output.elem_left_bottom+'<br>'; debugstr += 'output.elem_right_top = '+output.elem_right_top+'<br>'; debugstr += 'output.elem_right_bottom = '+output.elem_right_bottom+'<br>'; debugstr += 'output.elem_offset_left = '+output.elem_offset_left+'<br>'; debugstr += 'output.elem_offset_top = '+output.elem_offset_top+'<br>'; debugstr += 'output.setting_top = '+output.setting_top+'<br>'; debugstr += 'output.setting_left = '+output.setting_left+'<br><br>'; $('#debug').html(debugstr); */ return output; } function tippedoffmsgelempos(msg_elem, elpos) { $msg_elem = msg_elem; var msg_position = $msg_elem.position(); var msg_offset = $msg_elem.offset(); var msg_width = $msg_elem.width(); var msg_height = $msg_elem.height(); var msg_right_side = msg_offset.left + msg_width; var msg_bottom = msg_offset.top + msg_height; //$('#debug').append('<br><br>msg_right_side = '+msg_right_side+'<br>msg_bottom = '+msg_bottom+'<br>elpos.elem_right_top ='+elpos.elem_right_top); var final_top = elpos.setting_top; var final_left = elpos.setting_left; if(msg_right_side > elpos.win_width) { final_left = elpos.setting_left-(msg_width-elpos.elem_width); } if(msg_bottom > elpos.win_height) { final_top = elpos.elem_offset_top-(msg_bottom+elpos.elem_height); } //$('#debug').append('<br>final_left = '+final_left); $msg_elem.css({"top":final_top, "left":final_left}).show(); }
is final result.. in attempting make tooltip plugin jquery can custom tailor needs, , 1 doesn't need hard coded messages in elements when first start out. end result hope have in near future more elabort serves purpose me enough now.
Comments
Post a Comment