用户可以通过上传图片或输入文字来创造灵感。
查看
<% if inspiration.image.present? %>
<div class="inspiration-image">
<%= link_to image_tag(inspiration.image.url(:medium)), inspiration %>
</div>
<% else %>
<div class="panel panel-default">
<div class="panel-body">
<%= inspiration.name %>
</div>
</div>
<% end %>
CSS
// for images
.inspiration-image {
display: inline;
img {
width: 50%;
margin-top: 2.5px;
margin-bottom: 2.5px;
position: relative;
overflow: hidden;
vertical-align: top;
height: 164px;
}
}
// for text
.panel-default {
box-sizing: border-box;
border-style: none;
position: relative;
width: 50%;
padding-bottom: 40%;
overflow: hidden;
background-color: #446CB3;
border-radius: 0;
display: inline-block;
margin-bottom: 0px;
border: 2.5px white solid;
}
//for text
.panel-body {
color: white;
width: 100%;
height: 100%;
text-align: center;
position: absolute;
font-size: 12px;
justify-content: center;
align-items: center;
display: flex;
}
设定高度看起来不错:
但是,如果用户增加或减少屏幕尺寸,则图像会出现白色间隙,这是我希望在您的帮助下避免的:
视图
<% if inspiration.image.present? %>
<div class="panel panel-default" id="conditions">
<div class="panel-body">
</div>
</div>
<% else %>
<div class="panel panel-default">
<div class="panel-body">
<div class="white-link"><%= inspiration.name %></div>
</div>
</div>
<% end %>
CSS
#conditions {
background-image: url('goal-setting-conditions.jpeg'); # instead of this image, which I just used to test if it works, how can I make the image reflect whatever inspiration it is: <%= link_to image_tag(inspiration.image.url(:medium)), inspiration %>
background-size: cover;
}
这是我希望的那种行为,无论屏幕尺寸图像和文本框的宽度和高度是否相等:
答案 0 :(得分:0)
使用jQuery将图像添加为父div中的背景。
jQuery(document).ready(function() {
jQuery('.inspiration-image').find('img').each(function(n, image) {
var image = jQuery(image);
var thisurl = jQuery(this).attr('src');
image.parents('.inspiration-image').css('background-image', 'url(' + thisurl + ')');
});
});
在CSS中设置background-size
上的div
来覆盖。隐藏容器中的图像。
.inspiration-image {background-size:cover;}
.inspiration-image img{opacity:0;}