I want to select the first element in the screen or window regardless of vertical scrolling position
It doesn’t matter to me what element it is and it also doesn’t matter in what scrolling position I am (highest, lowest or somewhere in between), I just want to select the first element appearing from all elements appearing in the screen/window in a given scrolling position (or fold, if there is no scroll bar).
Hi @bendqh1, you can use getBoundingClientRect() to get an element’s position relative to the viewport, and the array find() method to get the first matching element (if any):
I replaced div to h2 and tried to run this on a Drupal website with very complex (yet well organized) markup and got surprising results, especially when logged in and a Drupal admin-menu is floating on the top of the viewport.
I basically just want to “record” the frist h2 so if a user goes to edit mode (from the regular usage of the site or “reading mode”), by some keyboard combo, than he will be transferred automatically to that heading in the WYSIWYG CKeditor.
Hm I don’t have a Drupal instance at hand, but maybe you just need a more specific selector then? An element inside a fixed or sticky header will always satisfy the test, so you’d need to query something like "main h2" (say) instead of just "h2".
BTW also note that with the above snippet you’ll get the first visible element as it appears in the document, which is not necessarily the topmost element in the viewport. If you need the latter, then you might reduce() the queried elements to the one with the smallest bottom > 0.
And you’re sure there’s no common class or ancestor element that you can use to identify these? oO Anyway, checking the style property like this will only work for styles set directly on the element (either using JS or a style attribute in the markup), not global / inherited CSS. To get the actual font size, use window.getComputedStyle():
I know that return stops a procedure and outputs a value, or, causes the procedure to stop and output a value (I don’t know which phrasing is more accurate).
But, besides that I misunderstand what is const { top, bottom }. Is this what’s called a “variable compound”? (over the years I came across many definitions of the term “variable compound” and I don’t know which is true).
Anyway, is there perhaps a slightly simpler code or even longer but more “abstract” or more “human-language” code than in this example?
So using destructing assignment you’d not assign the object itself to a variable, but the given object properties to variables of the same name. It’s just syntactic sugar though, you don’t have to use it.
Well, I’d say it just returns a value from the function. ^^ The function is indeed finished at this point, so following code would not get executed. In this case, the return value is a boolean indicating if the current element is inside the viewport, which again tells find() to return that element and stop iteration (see the link from my earlier reply for details).
I still have a bit of a hard time understanding it
I wish that the method was named boundOneOrMoreClientRectangle(); which is far clearer to me, although even then I would wonder, why only rectangles? Why not other shapes?
I also avoid using return because in all my years of elementary JavaScript I never had any case where it was obligatory to use it. Is it obligatory here?
Why one or more? It does return a single bounding client rectangle when called… as for the shape, I suppose a box just makes most sense in a rectangular screen where all elements are rectangular too (x and y position + width and height). How would you even define a bounding client triangle, say? Again just check out the links, it’s really well explained here:
Returning values from functions is fundamental concept not only in JS but most programming languages in general. You should really have a look at this, here’s another MDN link:
I guess you might rewrite your code to completely avoid using return statements (e.g. using global variables instead), but that would be more of an esoteric exercise really. ^^
Ah yes this code was supposed to be used inside the find() callback as part of the test for each element, not outside of it: