1

How does one detect when a fixed element is over a div? In this case when the anchor is over .images:

var fixedElement = $('a'),
    images = $('.images'),
    imagesTop = images.offset().top,
    imagesLeft = images.offset().left,
    imagesWidth = images.width(),
    imagesHeight = images.height();            

if(`fixedElement` is over `images`) {
    fixedElement.addClass('active');
}
a {
  position: fixed;
  top: 0;
  left: 0;
}
a.active {
  font-weight: bold;
  color: #aaa;
}
.images {
  margin-top: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a>Hello world</a>
<div class="images">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
    <img src="http://placehold.it/200/200">
</div>

Came across Detect element if over another element via using CSS3 Transform but it looks way too complicated.

UPDATE: See kthornbloom's comment

Community
  • 1
  • 1
Mark Boulder
  • 13,577
  • 12
  • 48
  • 78
  • What is your end goal? Unless I'm misunderstanding, this type of thing is typically done by comparing measurements. i.e. how far down you've scrolled + the anchor height versus the position of your .images div. – kthornbloom Feb 18 '15 at 14:26
  • Hello! My goal is to add a class to the anchor once it's over the div in question. – Mark Boulder Feb 18 '15 at 14:28
  • 1
    Ok, here's some code I made awhile back that works by those measurements I was talking about. In this example, it uses slideUp and slideDown, but you could easily change it to addClass and removeClass. http://jsfiddle.net/euDb8/7/ Just posting this as a comment since it doesn't answer your question how it was asked. – kthornbloom Feb 18 '15 at 14:30
  • try [elementFromPoint()](https://developer.mozilla.org/en-US/docs/Web/API/document.elementFromPoint) – charlietfl Feb 18 '15 at 14:31

0 Answers0