I have a div that is position: absolute. Sometimes on smaller screen resolutions it goes off the screen. Is it possible to calculate in px how much of it is off the screen?
Asked
Active
Viewed 59 times
0
-
1thats not how you should approach this problem. research "responsive webdesign" – David Nov 03 '17 at 10:27
-
what exactly is meant by "off the screen"? – Ben jamin Nov 03 '17 at 10:27
1 Answers
0
As has been already mentioned, this really isn't the way for solving this problem, however the code you need is:
const offsetRight = document.querySelector('.your-element').getBoundingClientRect().right - window.innerWidth
This code gets the px value of the right edge of your element and calculates the difference between that and the edge of the window.
For the left side:
const offsetLeft = 0 - document.querySelector('.your-element').getBoundingClientRect().left;

Ben Kolya Mansley
- 1,768
- 16
- 24