html - 试图在缩略图悬停时显示大图像

标签 html css wordpress image hover

我正在尝试创建缩略图库,将鼠标悬停在缩略图上后,它会在单独的框中显示完整尺寸的版本。

现在的问题是,当鼠标悬停在相应的缩略图上时,我根本无法显示图像。

这是 HTML:

<img class="thumb1" src="image.jpg">

而实际的全尺寸图像在另一个部分:

<img class="fullsize1" src="image.jpg>

这是 CSS,但它不起作用:

img.fullsize1 { display:none; }
img.thumb1:hover + img.fullsize1 {display:block;}

我做错了什么?

最佳答案

确保你的大 <img>直接跟随标记中的缩略图。

+代表 adjacent sibling selector .

img.fullsize1 {
  display: none;
}
img.thumb1:hover + img.fullsize1 {
  display: block;
}
<img class="thumb1" src="http://placehold.it/50x50">
<img class="fullsize1" src="http://placehold.it/100x100">


once the thumbnail is hovered over it will display the full size version in a seperate box.

要实现这一点,取决于标记的呈现方式。

您可以尝试以下方法:

.box {
  background-color: purple;
  margin: 1em;
  text-align: center;
}
.box img {
  vertical-align: middle;
}
.fullsize {
  display: none;
}
.box--thumb:hover + .box--fullsize > .fullsize {
  display: initial;
}
<div class="box box--thumb">
  <img class="thumb" src="http://placehold.it/50x50">
</div>
<div class="box box--fullsize">
  <img class="fullsize" src="http://placehold.it/100x100">
</div>


使用该标记,您可以实现如下目的:

body {
  margin: 0;
}
.gallery {
  height: 100vh;
  background-color: coral;
  position: relative;
}
.gallery__item--fullsize {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 80px;
  visibility: hidden;
}
.gallery__item--fullsize > img {
  width: 100%;
  height: 100%;
}
.gallery__item--thumb {
  position: absolute;
  left: 1em;
  bottom: 0;
}
.gallery__item--thumb:nth-child(3) {
  left: 50%;
  transform: translateX(-50%);
}
.gallery__item--thumb:nth-child(5) {
  left: initial;
  right: 1em;
}
.gallery__item--thumb:hover + .gallery__item--fullsize {
  visibility: visible;
}
<div class="gallery">
  <div class="gallery__item gallery__item--thumb">
    <img src="http://placehold.it/50x50">
  </div>
  <div class="gallery__item gallery__item--fullsize">
    <img src="http://placehold.it/1000x500?text=1">
  </div>
  <div class="gallery__item gallery__item--thumb">
    <img src="http://placehold.it/50x50">
  </div>
  <div class="gallery__item gallery__item--fullsize">
    <img src="http://placehold.it/1000x500?text=2">
  </div>
  <div class="gallery__item gallery__item--thumb">
    <img src="http://placehold.it/50x50">
  </div>
  <div class="gallery__item gallery__item--fullsize">
    <img src="http://placehold.it/1000x500?text=3">
  </div>
</div>

关于html - 试图在缩略图悬停时显示大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40639519/

相关文章:

javascript - 如何打印 HTML 页面中的特定部分 Angular JS

javascript - 从多个页面获取html内容并写入新页面

css - 设置点击链接的样式 - 任何仅 CSS 的解决方案?

css - 下拉菜单中呈现蓝色边框选择 [chrome]

html - 使外部元素不可见,html css

javascript - Button 创建的上传图像预览代码不会预览图像。 JavaScript/HTML

javascript - 如何将所有选定的复选框附加到跨度中

php - 锁定/禁用/隐藏 WordPress 主题选项,直到用户注册新闻通讯

javascript - Wordpress 帖子/页面编辑器无法工作 - 无法读取属性错误

html - CSS3 多个背景,一个在 ul 中重复