纯 css 将鼠标悬停在缩略图上时更改大图
Pure css change Big image on hover over thumbnails
我有这个 html 代码:
<div class="main-img">
<img id="main" src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
<p class="single-thumbnail-p">
<img src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
</p>
</div>
我想做的是使用纯 css 更改主图像 bacground 或 src,我知道 src 无法更改但是有什么方法可以在悬停时更改大图像缩略图?
谢谢!
您可以使用三张图片上方的背景 image.Add 作为背景 image.then 悬停更改背景。
缩略图和主图都必须是同级图像(或 parent/child)才能使悬停起作用。
根据您的示例代码,我假设 thumbnail
应该在下面,所以在这里我将它们放在标记中的 main-image
之前,并使用 absolute 将它们移动到下面,所以 CSS 悬停将起作用。您也可以使用 Flexbox 及其顺序 属性 执行此操作,尽管它的浏览器支持较少。
.main-img {
position: relative;
padding-bottom: 10px;
}
.main-img .thumbnail {
position: absolute;
top: 100%;
width: 100px;
}
.main-img .main-image {
display: block;
height: 200px;
}
.main-img .thumbnail + .thumbnail {
left: 160px;
}
#main2 {
display: none;
}
.main-img .thumbnail + .thumbnail:hover ~ #main1 {
display: none;
}
.main-img .thumbnail + .thumbnail:hover ~ #main2 {
display: block;
}
<div class="main-img">
<img src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img id="main1" src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
<img id="main2" src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
</div>
我有这个 html 代码:
<div class="main-img">
<img id="main" src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
<p class="single-thumbnail-p">
<img src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
</p>
</div>
我想做的是使用纯 css 更改主图像 bacground 或 src,我知道 src 无法更改但是有什么方法可以在悬停时更改大图像缩略图?
谢谢!
您可以使用三张图片上方的背景 image.Add 作为背景 image.then 悬停更改背景。
缩略图和主图都必须是同级图像(或 parent/child)才能使悬停起作用。
根据您的示例代码,我假设 thumbnail
应该在下面,所以在这里我将它们放在标记中的 main-image
之前,并使用 absolute 将它们移动到下面,所以 CSS 悬停将起作用。您也可以使用 Flexbox 及其顺序 属性 执行此操作,尽管它的浏览器支持较少。
.main-img {
position: relative;
padding-bottom: 10px;
}
.main-img .thumbnail {
position: absolute;
top: 100%;
width: 100px;
}
.main-img .main-image {
display: block;
height: 200px;
}
.main-img .thumbnail + .thumbnail {
left: 160px;
}
#main2 {
display: none;
}
.main-img .thumbnail + .thumbnail:hover ~ #main1 {
display: none;
}
.main-img .thumbnail + .thumbnail:hover ~ #main2 {
display: block;
}
<div class="main-img">
<img src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" class="img-responsive thumbnail" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" />
<img id="main1" src="http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
<img id="main2" src="http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F" alt="Teak Cleaner - 1lt bring back the original colour of teak/hardwood - Teak care" class="img-responsive main-image">
</div>