CSS/Javascript 解决方案根据图像方向自动应用高度/宽度属性
所以我正在设计一个Wordpress主题,我有四个div(380x400),我使用帖子中的缩略图来填充作为背景,问题是,如果图片是横向的,它会在底部留下一个间隙,这样我就可以不要在 CSS 中使用 width:100%
,如果我使用 height:100%
,肖像图像的高度也会出现同样的问题。
我想做的是,如果图像的高度大于宽度,则将 width
属性指定为 100%(例如 style.width='100%'
) 和高度
(如果宽度大于高度)。我正在寻找简单性,我不介意使用 jQuery 解决方案,但我真的不熟悉 jQuery。如果图像从边缘被裁剪掉,只要纵横比保持不变,我也没有问题。
附件是我描述的情况的屏幕截图。
这是相关代码:
<?php $thumbnail = '';
$post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
}
if (!empty($thumbnail)) { ?>
<div class="item" id="post-<?php the_ID(); ?>">
<img class="background" src="<?php echo $thumbnail; ?>" onload="changeWidth(this);">
<div class="date">
<?php the_date(); ?>
</div>
CSS:
.item, .no-thumbnail{
width:380px;
height:400px;
margin:5px 0px;
border:1px solid black;
float:left;
position:relative;
margin:5px;
display:inline;
}
.item img.background{
position:absolute;
left:0px;
z-index:-500;
width:150%;
}
So I'm designing a Wordpress theme and I have four divs (380x400) that I'm using thumbnails from the posts to fill as a background, the problem is, if the picture is landscape it leaves a gap at the bottom so I can't use width:100%
in the CSS, same problem with the height on a portrait image if I use height:100%
.
What I was looking to do was to assign a width
property of 100% if the height of the images is greater than the width(e.g. style.width='100%'
) and the height
if the width is greater than the height. I'm looking for simplicity, I don't mind using a jQuery solution but I'm really not familiar with jQuery. I also have no problem if the image is cropped off the edges just so long as the aspect ratio remains intact.
Attached is the Screenshot of the situation I'm describing.
Here's the relevant code:
<?php $thumbnail = '';
$post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
}
if (!empty($thumbnail)) { ?>
<div class="item" id="post-<?php the_ID(); ?>">
<img class="background" src="<?php echo $thumbnail; ?>" onload="changeWidth(this);">
<div class="date">
<?php the_date(); ?>
</div>
CSS:
.item, .no-thumbnail{
width:380px;
height:400px;
margin:5px 0px;
border:1px solid black;
float:left;
position:relative;
margin:5px;
display:inline;
}
.item img.background{
position:absolute;
left:0px;
z-index:-500;
width:150%;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在网格中显示缩略图和文本,因此好的且简单的方法是使所有缩略图具有相同的大小。您可以在生成缩略图时修剪照片,也可以使用 CSS 隐藏其余部分。这将使您的网格看起来更干净,并且如果文本和照片全部对齐,眼睛会更舒服。
如果您仍然想显示整个缩略图,可以使用 CSS/javascript/jQuery 在将鼠标悬停在网格中的“固定大小”缩略图上时显示它。
编辑:创建了一个示例来显示剪切不同方向/宽高比的居中缩略图。所需要的只是一个容器(主要是设置图库的宽度),以及
< /a>
显示缩略图并提供完整照片的链接。在这个例子中我没有包含标题或摄影师。
标签充当容器 - 应该可以在其中添加文本,然后根据需要定位它。
Since you display thumbnails and text in a grid, the both good and easy way out is to make all the thumbnails the same size. You can and either trim the photos when you generate the thumbnails, or hide the rest with css. This will make your grid look cleaner and it is easier on the eyes if text and photos all line up.
If you still want to show the entire thumbnail, you could use CSS/javascript/jQuery to show it when hovering over the "fixed size" thumbnail in the grid.
Edit: created an example to show clipping centered thumbnails of different orientation/aspect ratios. All it takes is a container (mainly to set the width of the gallery), and
<a href="..." style="background-image: url(...);"></a>
to both show the thumbnail and provide a link for the full photo.I didn't include title nor photographer in this example. The
<a />
tag acts as a container - should be possible to add text within it and then position it as needed.