track element in real time
can anyone tell me,
if i have an element in my webpage that's moving
how can i always keep an update of it's coordinates in real time
basically js will always be looking for the coordinate of that element regardless of it moving or being stationary
60 Replies
how is it moving?
I don't think you need to do anything. You can always access the properties in javascript using
getBoundingClientRect
and it will tell you the right coordinates.that too
yaa ik about that.. but when i use it, won't it just check the coordinate once?
how do I make the the run simultaneously so that it always keeps updating
could be moving anyway
in my case, it's moving along the x-axis on scroll
What do you mean run simultaneously?
What's the use case
are you moving it, or is it just moving when the user scrolls on the x-axis?
i mean like an animation where it's always moving
well, for what I'm currently doing, it's moving when it's scrolled
basically it's a slide of images
when I'm using touch to slide the images r moving
here I'm just tapping on the arrow
Like Joao asked, what's your use case?
what are you trying to accomplish?
wdym by that
okee
so lemme tell you
as u can see in the video, images slide in both way
left to right
right to left
now , what I want is, when the last image comes to the center of the screen i want the right arrow to disappear
I've managed to do it using click event which works fine when clicked
but i need to make the same while sliding with touch
so when I'm sliding the images with touch if i know the last image's coordinate in real time and i have the coordinate of the center i can use if condition to match their coordinate
and when the coordinate of the image is equal to the coordinate of the center meaning the image is now on the center,, i can just make the arrow disappear
so that's why i need to store the coordinate of the last img while it's moving when images r sliding
@jochemm
can you paste your code?
in a code block please, start with ```js and end with ```
basically like how u track the cursor
but in this case it's just an element
k
I really don't think you need to do that just to turn that arrow off
so that's why i need to store the coordinate of the last img while it's moving when images r slidingYou don't need to store the coordinates. Just use
getBoundingClientRect
to get them. You can then just check if x + element width is within viewport.
To track the element you can use intersection observer, or instead of using click event use touch event, which I think works for both touch screens and clicksI didn't know about touch events
Try that first, it's the simplest change and it may be enough?
what's the HTML look like?
(this is why we ask for an example in codepen in the #How To Ask Good Questions thread btw ๐)
but let's say, i used getBounding for touch events
now, it'll get the coordinate the moment i touch
but when i slide and the ing moves, it won't again get the coordinate will it?
k wait
It returns the coordinates at the time you call it, so if the element moved between calls, the coordinates will be different.
Ah, wait, you want the coordinates when the transition ends?
I'd vote for an
IntersectionObserver
(https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) on the :last-child
/ :first-child
so that it stays dynamic, and let the browser worry about the coordinatesIntersection Observer API - Web APIs | MDN
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
well, as I said, in real time
means it keeps updating as the element moves
getting the coordinate at the start or end means we r getting it in a stationary position
u know about the cursor track ryt?@joao6246
clientX and clientY for tracking mouse position
getting the coordinate at the start or end means we r getting it in a stationary positionAnd that's what you want. Getting the position when the last image is within a specific position.
it's basically that, BUT , it's not mouse rather an element
well, yaa i guess
I'm kinda confused here
around what i need for what I want
ClientX === getBoundingClientRect (for elements)
sorry, i didn't get it
To keep things simple, I agree that interserctionObserver is best here
I'll check em out tnx xD
okeee
I'll check it out
You set it up once and it will react when the element you tell it to observe comes within view. Which is essentially what you are after here.
okeey
gotcha
tnx for your valuable times guys
appreciate it very much
Super powerful API to play with and pretty simple to implement, too
okiii
tnx
Are you adding new elements dynamically as user scrolls?
nope
Good, nevermind then ๐
hihi
k
I'm very new to ks
js*
so hard to both understand and explain
sorry for my inconvenience
That's why this forum exists!
hey, no worries !
JS is awesome (and wait when you get to Typescript, it gets even better!) ๐
I think you can do it with just
element.scrollWidth
, but I'm not 100% sure how your scroll thing is set up with CSS, can you add that too?ahaha...k
well there's basically 8 images,
they r inside a div with a fixed height and width
and then i havw used
display : grid;
grid-template-columns : repeat (8 , 1fr);
to put those image in a row
and the simply
overflow-x : scroll
@สแดแดส 2.0 I've made a version using nothing but the
scroll
event listener and the basic properties on the container https://codepen.io/jochemm/pen/zYJvvBe?editors=0110you'll have to use some imagination for the layout and the fancy buttons, I skipped those cause they weren't the point of the question. If you want something closer to what you had, share a codepen that I can edit ๐
well, appreciate your effort
but that wasn't that i was actually looking for
say, when the last img is on display u want your button to have a grey background
so as you're scrolling the button is normal but the moment the last image gets displayed the Button turns grey
How'd you do that
well, I've been looking about intersectionObserver
seems like that might solve it
What happens when you detect you're at the end is up to you, right now I'm adding a class that hides the button, but if you want that class to make it gray you can just replace
display: none;
with color: silver;
this is what I came up with by far
I'm using a button element, so you could also set the disabled property to true instead of adding a class, but because you're using divs with fontAwesome icons, I didn't think that would be as handy
well rn in your code
the button doest change or disappear when i hit the last image tho
it's not snapping so you have to scroll all the way to the end yourself. It'll disappear then
it's just a proof of concept, I'm not going to rebuild the entire thing just to demonstrate one small bit
but if what you have works for you, that's fine too ๐
hmm..okeey
gotcha
i think I'll use intersectionObserver
that's seems like much less work and would also just work for both click and touch
but tnx anyways
๐
Unknown Userโข2y ago
Message Not Public
Sign In & Join Server To View
thank you very much
really really appreciate the effort ๐ซ
i will surely try this when I'm free
u understood everything here except the
can u what that does
that's prolly cause it's generating random numbers each every 3 seconds and calling updatePosition(); every 100ms i guess
and the displaying current position in console
Unknown Userโข2y ago
Message Not Public
Sign In & Join Server To View
ya it helped me tnx
^ ^
yaa okeey
gotchaa
tnx๐ซ
Unknown Userโข2y ago
Message Not Public
Sign In & Join Server To View