简体   繁体   English

在显示隐藏元素时,由于滚动,它显示在错误的位置,仅在chrome中

[英]On display of hidden element it shows on wrong place because of scroll, only in chrome

I don't realy know how to explain this thing in short sentence. 我真的不知道该如何用短句来解释。 I don't know if it is bug or not.. 我不知道这是不是bug。

In parent div with fixed height and overflow-y scroll, I have multiple children elements, which has jquery function click, what displays hidden element in these divs. 在具有固定高度和y滚动的父div中,我有多个子元素,这些元素具有jquery函数click,这些子元素显示这些div中的隐藏元素。 When I scroll down to last div, after click, hidden element displays in wrong place. 当我向下滚动到上一个div时,单击后隐藏元素显示在错误的位置。

I tried to search for this problem, cause it should be pretty common. 我试图搜索此问题,因为它应该很常见。 But nothing came up.s 但是什么都没出现。

It's realy hard to explain with words. 真的很难用语言来解释。 Just look at this jquery example with mozilla and after that with chrome. 只需使用mozilla查看此jquery示例,然后再使用chrome。 https://jsfiddle.net/zvwcdzjz/2/# https://jsfiddle.net/zvwcdzjz/2/#

PS I need my original example work and look exactly the same on chrome and mozilla, cause right now on mozilla everything looks exactly as i want it to be, but it bugs on chrome. PS我需要我的原始示例工作,并且在chrome和mozilla上看起来完全一样,因为现在在mozilla上,一切看起来都与我想要的完全一样,但是在chrome上却有问题。 It can be solved with jQuery too, makes no difference for me. 它也可以用jQuery解决,对我来说没有任何区别。

HTML: HTML:

<div id="el">
  <div class="content">
    <div class="block">
      <div class="blocktoopen"></div>
      <div class="button">click to open</div>
    </div>
    <div class="block">
      <div class="blocktoopen"></div>
      <div class="button">click to open</div>
    </div>
    <div class="block">
      <div class="blocktoopen"></div>
      <div class="button">click to open</div>
    </div>
  </div>
</div>

CSS: CSS:

#el {
  width: 300px;
  height: 400px;
  overflow-x: hidden;
  overflow-y: scroll;
  background-color: rgba(0,0,0,0.1);
}
#el .content {
  width: 300px;
  height: auto;
}
.block {
  width: 300px;
  height: 200px;
  margin-top: 10px;
  background-color: rgba(0,0,0,0.2);
}
.button {
  background-color: blue;
  color: #fff;
  cursor: pointer;
  text-align: center;
  margin-top: 90px;
  float: left;
}
.blocktoopen {
  width: 50px;
  height: 50px;
  position: absolute;
  margin-left: 300px;
  background-color: red;
  display: none;
}

JS: JS:

$(function(){
    $(".button").click(function(){
    $(this).parent(".block").children(".blocktoopen").show();
  });
  $("#el").scroll(function(){
     $(".blocktoopen").hide(); });
});

The set height of #el was causing the red box to appear in the incorrect location. #el的设置高度导致红色框出现在错误的位置。 I have removed this. 我已经删除了。 See the example below: 请参阅以下示例:

Change: 更改:

#el {
  width: 300px;
  height: 400px;
  overflow-x: hidden;
  overflow-y: scroll;
  background-color: rgba(0,0,0,0.1);
}

To: 至:

#el {
  width: 300px;
  overflow-x: hidden;
  overflow-y: scroll;
  background-color: rgba(0,0,0,0.1);
}

And then you're good to go. 然后您就可以开始了。

To make your life simpler make the parent .bloc relative so the blocktoopen will be computed relatively. 为了使您的生活更简单,请将父.bloc作为相对对象,以便将blocktoopen相对地进行计算。 Will help with the responsiveness. 将有助于响应。

.block {
  width: 300px;
  height: 200px;
  margin-top: 10px;
  background-color: rgba(0,0,0,0.2);
position: relative; 
}

.blocktoopen {
  width: 50px;
  height: 50px;
  position: absolute;
  top: 50%; 
bottom: 50%;  
  background-color: red;
  display: none;
 right: 0;
}

I can't post comment so here is another try with jsfiddle . 我无法发表评论,因此这是jsfiddle的另一种尝试。 I am not sure if you have horizontal scroll as well. 我不确定您是否也有水平滚动。 remove margin-right from .blocktoopen and add right:0; 从.blocktoopen删除margin-right并添加right:0; Also wrap all your internal content inside a div and set the width to maybe 225px 还要将所有内部内容包装在div中,并将宽度设置为225px

#el {
  width: 300px;
  height: 400px;
  overflow-x: hidden;
  overflow-y: scroll;
  background-color: rgba(0,0,0,0.1);
}
#el .content {
  width: 300px;
  height: auto;
}
.block {
  width: 300px;
  height: 200px;
  margin-top: 10px;
  background-color: rgba(0,0,0,0.2);
  position: relative; 
}
.button {
  background-color: blue;
  color: #fff;
  cursor: pointer;
  text-align: center;
  margin-top: 90px;
  float: left;
}
.blocktoopen {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
  display: none;
  top: 50%; 
  bottom: 50%; 
  right: 0;
}

.internal{
  width: 225px;
  }

Have you tried to click on 2 buttons without scrolling? 您是否尝试过单击2个按钮而不滚动? Try it. 试试吧。 Looks like you were using visibility: hidden; 好像您在使用visibility: hidden; and not display: none; 而不display: none; . Maybe trying to set the position: relative; 也许试图设置position: relative; ... ...

Just seen the jquery script. 刚看过jquery脚本。 Show() and hide() appears to work as visibility css property. Show()hide()似乎可以用作visibility CSS属性。 If u look with Chrome DevTools the jsFiddle example you will see that you can't see the red boxes but they are still there. 如果您使用Chrome DevTools的jsFiddle示例,您将看到看不到红色框,但它们仍然存在。

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

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