Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
jQuery - Find out which element is directly behind another

I am creating a little box opening game where my users can open a box and an animated spinner scrolls along all of the prizes and lands on their winning prize (indicated by the black line in the middle in the pic below)

Image of the spinner

After the animation, a whole bunch of box elements that represent the prizes are added to the page. What I am trying to figure out is:

Using jQuery, how can I find which box the black line lands on?

Any help would be appreciated, thanks.

HTML STRUCTURE

<section class="boxes box-background">
    <div class="winning-line" id="line">&nbsp;</div>
        <div class="container boxes-container">
            <div class="row">
                <div class="col-12">
                    <div class="open-box">
                        <div id="unbox-area" id="unbox">
                            <div id="cardList">
                               <div class="card" id="prize1">...</div>
                               <div class="card" id="prize2">...</div>
                               <div class="card" id="prize3">...</div>
                               //so on
                            </div>
                         </div>
                     </div>
                 </div>
             </div>
         </div>
     </div>
 </section>

PROBLEM

Currently, using the code below, I can only get the "open-box" element, not the actual individual "card" class element that the black line landed on.

let blackLineEl = document.getElementById("line");  
let rect = blackLineEl.getBoundingClientRect();
let left = rect.x;
let originalDisplay = blackLineEl.style.display;
blackLineEl.style.display = "none"
let winner = document.elementFromPoint(left, 180.0);
blackLineEl.style.display = originalDisplay
console.log(winner.className);

I have tried adjusting the static top position to different values, but no luck.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try the elementFromPoint(x, y) method.

Since document.elementFromPoint returns the topmost element, you'll need to temporarily set your draggable to display:none or pointer-events:none to find elements below it.

Something like:

let lineEl = document.getElementById("blackLine")
let x = parseFloat(getComputedStyle(lineEl).left)
let y = parseFloat(getComputedStyle(lineEl).top)
let h = parseFloat(getComputedStyle(lineEl).height)

let originalDisplay = getComputedStyle(lineEl).display
  //hide black line to get element behind it
lineEl.style.display = "none"

let winningBox = document.elementFromPoint(x, y+0.5*h);
lineEl.style.display = originalDisplay

// Do something with winningBox

Edit: You can use Mike Abdullah's gist that gets all the elements in a given point

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!