简体   繁体   English

如何确定一个HTML元素是否在另一个HTML元素后面?

[英]How to find out if a HTML Element is behind another HTML Element?

I have this HTML : 我有这个HTML:

HTML : HTML:

<div class="stave-container">
  <section id="Captcha"></section>
  <div id="horiz_container_outer">
     <img class="img1" />
       ...
     <img class="imgn"/>
  </div>
</div>

++++++++++++++++++++++++++++++++++++++++
+         +                            +
+#Captcha +  img | img | img | img     +
+         +  div#horiz_container_outer +
++++++++++++++++++++++++++++++++++++++++

The section #Captcha is fixed! #Captcha部分已修复!

The div #horiz_container_outer is scrollable and contains img element! div #horiz_container_outer可滚动并包含img元素!

How can i detect when a img element are behind the Div #Captcha while scrolling? 滚动时如何检测img元素何时位于Div #Captcha后面?

My Solution : 我的解决方案:

I put it into the jquery : .scroll function 我把它放进了jquery: .scroll函数

    $("#horiz_container_outer > img").each(function(){
      if( $("#Captcha").offset().left > $(this).offset().left )
        {
          console.log($(this).attr("class"));
        }
    })

If you are attempting to have a section of the screen that doesn't scroll (which is what it sounds like, but I am not quite sure from your question) the standards compliant way to do this is: 如果您试图让屏幕的某个部分不滚动(这听起来像是,但是我不确定您的问题),那么符合标准的方法是:

.non-scroll-div {
  position:fixed;
}

Keep compatability in mind however: http://caniuse.com/#feat=css-fixed 但是请记住兼容性: http : //caniuse.com/#feat=css-fixed

To copy all the elements from one div to another you could do something like this: 要将所有元素从一个div复制到另一个div,您可以执行以下操作:

html HTML

<div id="fixedDiv"></div>
<div id="stuff"><span>some stuff</span><br /><hr /></div>

js JS

document.getElementById("fixedDiv").innerHTML = document.getElementById("stuff").innerHTML;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM