【问题标题】:Equal width + height responsive divs with hover overlay具有悬停覆盖的等宽 + 高度响应 div
【发布时间】:2022-04-27 23:05:46
【问题描述】:

我无法让 div 的高度匹配。我需要它们将 div 的高度与最多的内容相匹配——这通常很容易,但我正在使用还包含更多文本的叠加层来做到这一点。我不能用固定的高度或宽度来做,因为我需要布局来响应。单元格基本上需要是 col-md-2,文本换行时高度最高,它更像一个正方形,col-sm-6 长而窄,col-xs-12 也长且狭窄。

它不必是引导程序,但它需要具有那种效果。另一个问题是我需要尽可能多地内联,因为我将其交付给客户以粘贴到他们的 drupal 站点,而没有太多访问后端的权限,所以请忽略我的总内联代码。

我几乎搞定了,叠加层起作用了,高度由内容决定,但我无法让高度与最长的高度相匹配。谁能看到我在这里做错了什么??

#table{ 
    display: table;
    border-collapse: collapse;
    width: 100%;
}
.tr{ 
    display: table-row; 
}
.td{ 
    display: table-cell;
    vertical-align: top;
    text-align: center;
    height: 100%;
    }
    

.overlay {
  position: absolute;
  vertical-align: middle;
  top: 50%;
  left: 50%;
  height: 100%;
  width: 100%;
  background-image: url('https://premium.wpmudev.org/blog/wp-content/uploads/2015/02/fullwidth-small.png');
  background-position: center top;
  background-size: 500% auto;
  opacity: 1;
  transition: .5s ease;
}

.overlay:hover {
  opacity: 0;
}

.text {
  color: white;
  height: 100%;
  top: 50%;
  left: 50%;
  font-size: 20px;
  position: absolute;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
  opacity: 1;
}

.text:hover {
  opacity: 0;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
 
}

.cell {
  font-size: 14px;
  
}
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<div id="table" style="width: 100%">
        <div class="td col-md-12" style="background-image: url('https://premium.wpmudev.org/blog/wp-content/uploads/2015/02/fullwidth-small.png');
  background-position: center top;
  background-size: 500% auto;  text-align: center; padding-top: 10%; padding-bottom: 10%">test</div>
    </div>
   
<div id="table">    
   

 
    
        <div class="td col-md-2 col-md-offset-1 col-sm-6 col-xs-12" 
        style="color: #0169A9;  -webkit-box-shadow:inset 0px 0px 0px 6px #0169A9; -moz-box-shadow:inset 0px 0px 0px 6px #0169A9;
    box-shadow:inset 0px 0px 0px 6px #0169A9;       font-size: 14px;
    font-weight: bold;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    line-height: 1.2;">To find out more about the making of this toolkit, click here
        <div class="overlay text">
         Test this
      </div>
      </div>
      
      
      
        <div class="td col-md-2 col-sm-6 col-xs-12"  style="color: #0169A9;  -webkit-box-shadow:inset 0px 0px 0px 6px #0169A9; -moz-box-shadow:inset 0px 0px 0px 6px #0169A9;
    box-shadow:inset 0px 0px 0px 6px #0169A9;       font-size: 14px;
    font-weight: bold;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    line-height: 1.2;">To find out more about the making of this toolkit
        <div class="overlay text">
         Test this
      </div>
      </div>
        
        
        <div class="td col-md-2 col-sm-6 col-xs-12"  style="color: #0169A9;  -webkit-box-shadow:inset 0px 0px 0px 6px #0169A9; -moz-box-shadow:inset 0px 0px 0px 6px #0169A9;
    box-shadow:inset 0px 0px 0px 6px #0169A9;       font-size: 14px;
    font-weight: bold;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    line-height: 1.2;">To find out more about the making of this toolkit, click here toolkit, click here
        <div class="overlay text">
         Test this
      </div>
      </div>
        
        
        
       <div class="td col-md-2 col-sm-6 col-xs-12"  style="color: #0169A9;  -webkit-box-shadow:inset 0px 0px 0px 6px #0169A9; -moz-box-shadow:inset 0px 0px 0px 6px #0169A9;
    box-shadow:inset 0px 0px 0px 6px #0169A9;       font-size: 14px;
    font-weight: bold;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    line-height: 1.2;">To find out more about the making of this toolkit, click here
        <div class="overlay text">
         Test this
      </div>
      </div>
        
        
        
       <div class="td col-md-2 col-sm-6 col-xs-12"  style="color: #0169A9;  -webkit-box-shadow:inset 0px 0px 0px 6px #0169A9; -moz-box-shadow:inset 0px 0px 0px 6px #0169A9;
    box-shadow:inset 0px 0px 0px 6px #0169A9;       font-size: 14px;
    font-weight: bold;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    line-height: 1.2;">To find out more about the making of this toolkit, click here
        <div class="overlay text">
         Test this
      </div>
      </div>
        
        
       
        <div class="clear-all">
         </div>

    
</div>
</body>

【问题讨论】:

  • 用jquery创建一个数组,存储一行的所有高度,然后动态设置一个固定高度等于最大值。您可以在 $(window).resize() 事件上重复此工作以保持响应能力。
  • 您能进一步解释一下吗?我对 jquery 不够熟悉,不知道如何实现这个建议。客户端将更改 div 中的文本,因此我不会随时知道其中任何一个的固定高度。另外请记住,我无权访问任何后端文件,我可以在客户的 drupal 站点上的 css 设计器中添加一些 css,但我不知道 jquery。有可能吗?
  • 如果有任何插件,也许。无论如何,您必须编辑公共方面,而不是后端。当你和你想要达到的目标之间有一个 CMS 时,总是会更加困难......
  • 我的意思是读取所有 .md-2 高度,然后选择最大值,并将其设置为所有其他高度的固定高度。您可以根据要触发它的事件侦听器重复此功能。如果你愿意,你甚至可以使用纯 JavaScript。
  • 你能举个例子吗?

标签: html css twitter-bootstrap hover responsive


【解决方案1】:

对不起,我很忙,这是一个工作示例:

//we need to iterate through row childs
$('.row').each(function() {
    //Set a var to store the highest height
    var maxHeight = 0;
    //Then, we iterate through all cols inside the row, searching for the highest one.
    $('.col-xs-4', this).each(function() {
        if($(this).height() > maxHeight) {
         maxHeight = $(this).height();  
        }
    });
    //Now, we have the height value of the highest one, so we can apply this height to all row childs.
    $('.col-xs-4', this).each(function() {
        $(this).height(maxHeight); 
     });
});
.col-xs-4{
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">


<div class="container-fluid">
  <div class="row">
    <div class="col-xs-4">
      <p>text</p>
      <p>text</p>
      <p>text</p>
    </div>
    <div class="col-xs-4">
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
    </div>
    <div class="col-xs-4">
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
    </div>
  </div>
</div>

【讨论】:

  • 我尝试将它与我当前的设置一起使用以适应悬停覆盖,但它似乎不起作用。看,在我的例子中,表格 div 适合正确的高度,但覆盖并没有延伸到整个长度。我尝试实现您的代码,但我认为我对 javascript 的了解还不够,无法正确完成。 jsfiddle.net/graphicfixations/0vhsu5ce/30
  • 首先你在 div 上重复 ID 标签,你的 IDE 应该指出这个大错误,然后,你使用 $('.table') 这是一个类选择器,而不是使用 $('#table') (id 选择器) 但它不起作用,因为我们需要类来处理它。然后,你的代码太整洁了,把你的 css 移到 css 部分,让 html 干净,我无法正确阅读它,我的眼睛在流血......:V
  • 我知道,我讨厌内联编码,但是客户端无法访问任何东西,所以我需要尽可能多地这样做。让我把它移回这个例子。我不在 IDE 中工作,我只是在 jsfiddle 中手动编码。我对此完全不屑一顾。等等。
  • 好的,代码已清理和分离,我修复了 ID 标签的问题。你介意现在看看吗?我保证,不会再流血了。 jsfiddle.net/graphicfixations/0vhsu5ce/70
  • 太棒了!有什么方法可以使用该修复使文本正确居中?
【解决方案2】:

【讨论】:

  • 正如@graphicfixations 解释的那样,它是一个CMS,您无法单独访问代码,并且通常编辑模板不是一个好选择,因为当它获得更新时,它会重写您的mod你必须一遍又一遍地寻找改变而不破坏它。
猜你喜欢
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 2020-11-24
  • 2013-07-12
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多