Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Paccione
9,017 PointsJavascript getBoundingClientRect bounce off edges of screen
I'm creating a landing page that features background text that moves about randomly. I am trying to get the text divs to bounce off the edges of the screen when they hit and to bounce of each other. Unfortunately, I haven't used .getBoundingClientRect() before so I'm a bit lost. Also I am using the GSAP animation platform.
var bgtext = document.querySelectorAll('.text');
function moveText(){
TweenLite.to(bgtext, 1, {opacity: 1});
for (var i = 0; i <= bgtext.length; i++) {
var xPos = Math.floor(Math.random() * 2001) - 1000;
var yPos = Math.floor(Math.random() * 2001) - 1000;
TweenLite.to(bgtext[i], 5, {xPercent:''+xPos+'', yPercent:''+yPos+'', force3D:true});
// var rect[i] = bgtext[i].getBoundingClientRect();
// var x[i] = rect[i].left;
// var y[i] = rect[i].top;
// var w[i] = rect[i].right - rect.left;
// var h[i] = rect[i].bottom - rect.top;
// if (x[i] <= 0 || y[i] <=0){
// xPos = Math.floor(Math.random() * 500);
// yPos = Math.floor(Math.random() * 500);
// TweenLite.to(bgtext[i], 5, {xPercent:''+xPos+'', yPercent:''+yPos+'', force3D:true});
// };
};
}
moveText();