html - 图像悬停效果

标签 html css hover

我想知道为什么我的悬停效果不起作用。 现在我有一张图片,当我将光标移到它上面时我想要这样的东西。

enter image description here

当然我已经为这个效果写了代码,但是它对悬停效果不起作用。 你能解释一下我做错了什么吗? 就像您看到的那样,我尝试过使用 display:none 然后使用 display:block 但没有任何反应。 非常感谢您的帮助。

这是一个 jsfiddle democodepen demo

HTML代码

<section class="products">
        <h1 class="products__title"><span>Wspierane</span> produkty</h1>
  <div class="products__wrap">
        <div class="products__box">
            <img src="http://i.imgur.com/Zrd6Mgu.png" alt="">
            <p>hera</p>
        </div>
        <div class="products__box">
            <img src="http://i.imgur.com/Zrd6Mgu.png" alt="">
            <p>elektra</p>
        </div>
        <div class="products__box">
            <img src="http://i.imgur.com/Zrd6Mgu.png" alt="">
            <p>rainbow</p>
        </div>
        <div class="products__box">
            <img src="http://i.imgur.com/Zrd6Mgu.png" alt="">
            <p>roboclean</p>
        </div>
        <div class="products__box">
            <img src="http://i.imgur.com/Zrd6Mgu.png" alt="">
            <p>rainbow d4</p>
        </div>
    </div>
    </section>

SCSS代码

$font: 'Lato', sans-serif;
$break-medium: 45em;
$break-large: 75em;
@function calculateRem($size) {
    $remSize: $size / 16px;
    @return $remSize * 1rem;
}
@mixin font-size($size) {
    font-size: $size;
    font-size: calculateRem($size);
}
* {
    margin: 0;
    padding: 0;
}
.products {
    width: 100%;
    height: 1600px;
    background-color: rgb(255,255,255);
    @media only screen and (min-width: $break-large) {
        height: 500px;
    }

    &__title {
        margin: 0;
        font-family: $font;
        @include font-size(35px);
        font-weight: 700;
        text-align: center;
        color: rgb(13,13,13);
        padding-top: 35px;
        padding-bottom: 45px;
        @media only screen and (min-width: $break-medium) {
             @include font-size(50px);
        }

        span {
            font-weight: 900;
        }
    }

    &__wrap {
        @media only screen and (min-width: $break-large) {
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
        }
    }

    &__box {
        width: 240px;
        background-color: rgb(255,255,255);
        margin: 0 auto;
        position: relative;
        margin-top: 30px;
    }

    p {
        font-family: $font;
        font-weight: 700;
        @include font-size(30px);
        text-transform: uppercase;
        color: rgb(247,248,249);
        text-align: center;
        white-space: nowrap;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        @media only screen and (min-width: $break-large) {
            display: none;

            &:hover {
                display: block;
            }
        }

        &::before {
            content: '';
            background-color: rgb(10,198,162);
            width: 240px;
            height: 60px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: -200;
        }
    }
}

最佳答案

你想要的是,当你将鼠标悬停在 .products__box 元素(它的父元素)上时,p 元素将获得 display: blocked ):

.products__box p{
  display: none;
}
.products__box:hover p{
  display: block;
}

检查此更新:
https://jsfiddle.net/dnttrb7q/1/

关于html - 图像悬停效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40872196/

相关文章:

html - 如何在图像中包装容器

javascript - 避免具有绝对位置的 HTML 元素之间的重叠

javascript - 在我的 Canvas 外菜单上添加滑动功能

javascript - 由于 Javascript 悬停效果,链接在移动设备上不起作用

javascript - 缩略图悬停缩放(放大)w/CSS3 和 javascript z 轴问题

CSS3 - 过渡

Javascript 过滤/排序系统故障

php - 点击 Google map 上的 HTML 表单

javascript - CSP - 如何解决 style-src unsafe-inline - 当具有动态定位的页面元素时

html - 悬停背景,前面有图像