将鼠标悬停在网页上展开问题

问题描述 投票:0回答:2

我的网站有这样的代码,其中的块应该扩展到中间,但是当我将鼠标悬停在右侧的块上时,它也会将其他块拖到中间。有谁知道这个问题的解决办法吗?

<!DOCTYPE html>
<html>
<head>
    <title>Website</title>
    <style>
        .container {
            display: flex;
            justify-content: space-between;
            height: 100vh;
        }
        .column {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .block {
            width: 100px;
            height: 100px;
            margin: 10px;
            text-align: center;
            line-height: 100px;
            transition: all 0.5s;
            background-color: lightgreen;
        }
        .block:hover {
            width: 150px;
        }
        .left .block:hover {
            margin-right: 50px;
        }
        .right .block:hover {
            margin-left: 50px;
        }
        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }
    </style>
</head>
<body>
    <div class="background">
        <!-- background -->
    </div>
    <div class="container">
        <div class="column left">
            <div class="block">Block 1</div>
            <div class="block">Block 2</div>
            <div class="block">Block 3</div>
        </div>
        <div class="column right">
            <div class="block">Block 4</div>
            <div class="block">Block 5</div>
            <div class="block">Block 6</div>
        </div>
    </div>
</body>
</html>

已经询问 ChatGPT 但无法解决问题。

html css hover
2个回答
0
投票

问题是您使用 margin-right 和 margin-left 属性来定位块悬停时的位置。这些属性是相对于父元素的,因此当您将鼠标悬停在右侧的块上时,它也会随之推动右侧的其他块。

要解决这个问题,可以使用transform属性来代替。变换属性允许您移动、旋转、缩放和倾斜元素。在这种情况下,您可以使用translateX()函数来水平移动块。

       .container {
            display: flex;
            justify-content: space-between;
            height: 100vh;
        }
        .column {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .block {
            width: 100px;
            height: 100px;
            margin: 10px;
            text-align: center;
            line-height: 100px;
            transition: all 0.5s;
            background-color: lightgreen;
        }
        .block:hover {
            width: 150px;
            transform: translateX(50px);
        }
        .left .block:hover {
            margin-right: 50px;
        }
        .right .block:hover {
            margin-left: 50px;
        }
        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }
  <div class="background">
        <!-- background -->
    </div>
    <div class="container">
        <div class="column left">
            <div class="block">Block 1</div>
            <div class="block">Block 2</div>
            <div class="block">Block 3</div>
        </div>
        <div class="column right">
            <div class="block">Block 4</div>
            <div class="block">Block 5</div>
            <div class="block">Block 6</div>
        </div>
    </div>


0
投票

使

.block
元素在悬停时变宽,也会增加放置这些块的容器的宽度。并且由于您的块在这两个容器中都是左对齐的,因此右侧的块似乎也会“移动”。 ”

您需要将右侧容器中的块对齐,右侧:

.column.right {
  align-items: flex-end;
}
© www.soinside.com 2019 - 2024. All rights reserved.