检测固定元素是否在div之上

时间:2015-02-18 14:04:49

标签: jquery

如何检测固定元素何时超过div?在这种情况下,锚点超过.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>

遇到Detect element if over another element via using CSS3 Transform,但看起来太复杂了。

更新:请参阅kthornbloom的评论

0 个答案:

没有答案