html - 悬停时更改图像(带列)

标签 html css

我有四列(每列宽度为 25%),它们占据屏幕的 100% 宽度和高度。这个想法是让一个图像与每一列相关联,当用户将鼠标悬停在每一列上时,图像会发生变化以与列中的文本/图标相对应(图像本身应占据 100% 的宽度/高度)。

仅使用 HTML + CSS 就可以实现这样的功能吗?我假设我需要一些 JS。

到目前为止,除了横跨所有列的图像外,我已经将它设置为一切都“有效”的地方。我试过改变:

.col:hover { width: 100%; }

这对于第一列似乎工作正常,但其他列在悬停时闪烁和故障。

查看下面的代码(我现在只是使用色 block 作为图像)/ 或者在 CodePen 上查看:https://codepen.io/sdorr/pen/VqLzBQ

<!doctype html>
            <head></head>
            <body>
                <div class="container">
                    <a class="button" href="#">learn more</a>
                    <div class="col col-1">
                        <div class="vertical-align">
                            <h1 class="hero-text">data</h1>
                        </div>
                    </div>
                    <div class="col col-2">
                        <div class="vertical-align">
                            <h1 class="hero-text">intelligence</h1>
                        </div>
                    </div>
                    <div class="col col-3">
                        <div class="vertical-align">
                            <h1 class="hero-text">experience</h1>
                        </div>
                    </div>
                    <div class="col col-4">
                        <div class="vertical-align">
                            <h1 class="hero-text">activation</h1>
                        </div>
                    </div>
                </div>
            </body>

            <style>
            * {
            margin: 0;
            padding: 0;
            font-family: sans-serif;
            }

            .container {
                width: 100%;
                height: 100vh;
                position: relative;
            }

            .col {
                display: inline;
                float: left;
                width: 25%;
                height: 100vh;
                background-color: red;
                position: relative;
                text-align: center;
                z-index: 0;
                overflow: hidden;
            }

            .button {
                padding: 20px 0;
                width: 100%;
                background-color: purple;
                color: #ffffff;
                text-decoration: none;
                text-align: center;
                position: absolute;
                bottom: 0;
                left: 50%;
                transform: translateX(-50%);
                z-index: 1;
            }

            .button:hover {
                background-color: orange;
            }

            .col-1:hover {
                background-color: pink;
            }

            .col-2:hover {
                background-color: blue;
            }

            .col-3:hover {
                background-color: green;
            }

            .col-4:hover {
                background-color: yellow;
            }

            .vertical-align {
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                width: 100%;
            }
            </style>
            </html>

最佳答案

使用图像而不是颜色,并让它覆盖整个元素:

.col-1:hover {
  background-image: url(...);
  background-repeat: no-repeat;
  background-size: cover;
}

关于html - 悬停时更改图像(带列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53731966/

相关文章:

html - CSS全高等距垂直列表

html - 固定位置元素的绝对定位子元素不滚动

html - 如何制作水平导航栏 100% 或我的包装器的大小

html - 如何在页面顶部放置两个固定的div?

javascript - JQuery 中的目标问题

html - 如何布局 3 个 div,使一个在 div 旁边,另一个在下面?

html - 多列布局中的阵容元素

PHP 下拉排序

CSS 不适用于 Razor 页面中的 asp-page 标签

html - 如何在标签下移动内联 radio 输入,