development:js:get_elem_pos

Get element position in browser with JavaScript

If you use table cells as menu buttons and autosize them, this code finds the left position of element with id 'menubutton', and then places element with id 'menubody' to the same left position:

var mbtn = document.getElementById('menubutton').getBoundingClientRect();
var mbody = document.getElementById('menubody');
mbody.style.left = mbtn.left+'px';

Or using jQuery

var newLeft = $('#menubutton').offset().left;
$('#menubody').css('left',newLeft);
Enter your comment:
G N A L W
 
  • development/js/get_elem_pos.txt
  • Last modified: 2019-10-31 09:04
  • (external edit)