简体   繁体   English

在网格中向下滑动div的底部,而不滑动整个行。

[英]Sliding down the bottom of a div in a grid, without sliding the entire row.

I'm working with a grid structure using CSS. 我正在使用CSS处理网格结构。 Within the grid there are panels within each panel there is a button and at the bottom there is a hidden div. 网格内有面板,每个面板内有一个按钮,底部有一个隐藏的div。

When the button is clicked the hidden div slides down (using jQuery slideToggle). 单击按钮时,隐藏的div向下滑动(使用jQuery slideToggle)。 There are multiple panels in the rows and columns. 行和列中有多个面板。

I have an issue that when the button is clicked to slide down then the entire row slides down - not just the row directly below! 我有一个问题,当单击按钮向下滑动时,整个行都会向下滑动-不仅是正下方的行!

How do I get it to only slide down the panel below it, rather than the whole next row? 我如何才能使其仅向下滑动其下方的面板,而不是整个下一行?

I have created a JSFiddle: http://jsfiddle.net/xpvt214o/873688/ 我创建了一个JSFiddle: http : //jsfiddle.net/xpvt214o/873688/

Here is the code I have been trying to get working! 这是我一直在尝试的代码!

Thanks! 谢谢!

HTML 的HTML

<div id="expand-box">
  <div class="wrapper">
    <div id="handlebars-sandbox">

      <div class="panel">
      <div class="title">HELLO WORLD</div>
      <div class="button"><button>OPEN</button></div>
      <div id='reveal-container'> BOTTOM</div>
      </div>

      <div class="panel">
      <div class="title">HELLO WORLD</div>
      <div class="button"><button>OPEN</button></div>
      <div id='reveal-container'> BOTTOM</div>
      </div>

      <div class="panel">
      <div class="title">HELLO WORLD</div>
      <div class="button"><button>OPEN</button></div>
      <div id='reveal-container'> BOTTOM</div>
      </div>

       <div class="panel">
      <div class="title">HELLO WORLD</div>
      <div class="button"><button>OPEN</button></div>
      <div id='reveal-container'> BOTTOM</div>
      </div>

    </div>  
  </div>
</div>

CSS 的CSS

.panel {
  border: 1px solid black; 
  margin-left: 5px;
  margin-right: 5px; 
  text-align: center;}

#reveal-container {
  display:none;
  height: 30px}

#expand-box {
    width: 65%;
    margin: 0 auto;}

.wrapper {
  margin: 0 auto;
  width: 100%;}

.title{
  height: 80px}

#handlebars-sandbox {
  padding: 8px;
  display: grid;
  margin: 0 auto;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)) ;
  grid-auto-rows: minmax(100px, auto);
  grid-gap: 20px;
  margin-top: 20px
}

@media (max-width: 1100px) {
  .wrapper {
    grid-template-columns: 1fr;
  }

  #handlebars-sandbox {
    width: 100%;
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr) ) ;
    grid-auto-rows: minmax(300px, auto);
  }
}

@supports (display: grid) {
  .wrapper > * {
    width: auto;
    margin: 0;
  }
}

JS JS

/* REVEALS REVEAL CONTAINER*/
$(document).on('click', '.button', function () {
  $(this).parents('.panel').find('#reveal-container').slideToggle()

});

All what you need to do is change id='reaveal-container' to class. 您需要做的就是将id ='reaveal-container'更改为class。 ID has to be unique. ID必须是唯一的。 It can perform once on the page. 它可以在页面上执行一次。 When I changed it on to class, everything works perfectly. 当我将其更改为课堂内容时,一切正常。

 /* REVEALS REVEAL CONTAINER*/ $(document).on('click', '.button', function () { $(this).parents('.panel').find('.reveal-container').slideToggle() }); 
 .panel { border: 1px solid black; margin-left: 5px; margin-right: 5px; text-align: center;} .reveal-container { display:none; height: 30px} #expand-box { width: 65%; margin: 0 auto;} .wrapper { margin: 0 auto; width: 100%;} .title{ height: 80px} #handlebars-sandbox { padding: 8px; display: grid; margin: 0 auto; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)) ; grid-auto-rows: minmax(100px, auto); grid-gap: 20px; margin-top: 20px } @media (max-width: 1100px) { .wrapper { grid-template-columns: 1fr; } #handlebars-sandbox { width: 100%; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr) ) ; grid-auto-rows: minmax(300px, auto); } } @supports (display: grid) { .wrapper > * { width: auto; margin: 0; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="expand-box"> <div class="wrapper"> <div id="handlebars-sandbox"> <div class="panel"> <div class="title">HELLO WORLD</div> <div class="button"><button>OPEN</button></div> <div class='reveal-container'> BOTTOM</div> </div> <div class="panel"> <div class="title">HELLO WORLD</div> <div class="button"><button>OPEN</button></div> <div class='reveal-container'> BOTTOM</div> </div> <div class="panel"> <div class="title">HELLO WORLD</div> <div class="button"><button>OPEN</button></div> <div class='reveal-container'> BOTTOM</div> </div> <div class="panel"> <div class="title">HELLO WORLD</div> <div class="button"><button>OPEN</button></div> <div class='reveal-container'> BOTTOM</div> </div> </div> </div> </div> 

Carsten Løvbo Andersen is right, ID'should be unique, an switched to classes, it can cause errors. 卡斯滕·洛夫博·安德森(CarstenLøvboAndersen)是对的,ID应该是唯一的,切换到类可能会导致错误。 What I think you are confusing for the next row opening is not the the ID or jQuery, but really the fault of the CSS you are using. 我认为您对下一行打开感到困惑的不是ID或jQuery,而是您所使用的CSS的真正错误。

#handlebars-sandbox {
  padding: 8px;
  display: grid;
  margin: 0 auto;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)) ;
  grid-auto-rows: minmax(100px, auto);
  grid-gap: 20px;
  margin-top: 20px
}

You are confusing revealing with boarders and margins, its not revealing the next one its just that the margin and borders of each panel are linked to each other because you are using display: grid; 您在使用边界和边缘显示时感到困惑,因为在使用display:grid;时,每个面板的边缘和边界都相互链接,而没有显示下一个。 on #handlebars-sandbox, it kinda like having inline-block. 在#handlebars-sandbox上,有点像具有内联块。

What I suggest is a putting a min-height: 130px; 我建议的是放一个最小高度:130像素; under .panel or just get rid of grid and do it with dispaly:block which it be a little harder. 在.panel下或只是摆脱网格并使用dispaly:block进行操作,这会有点困难。

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

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