猿问

悬停时背景图像缩放而不更改文本大小

我想在悬停时缩放背景图像但不更改文本大小。我该怎么做?


* { margin: 0; padding: 0; }

body {

}

.article-container {

    width: 300px;

    height: 200px;

    border: 1px solid #000000;

    overflow: hidden;

    position: relative;

}


.article-img-holder {

    width: 100%;

    height: 100%;

    background: url(https://awik.io/demo/background-image-zoom/traffic2.jpg);

    background-position: center;

    background-size: cover;

    background-repeat: no-repeat;

    transition: all 1s;

}


.article-img-holder:hover {

    transform: scale(1.2);

}


.split-image-links {

    width: 100%;

    height: 100vh;

    display: flex;

}

.split-image-links .split-image-link {

    width: 25%;

    height: 100%;

    overflow: hidden;

    position: relative;

}

.split-image-links .split-image-link .zoom-image {

    width: 100%;

    height: 100%;

    cursor: pointer;

    position: relative;

}

.split-image-links .split-image-link .zoom-image .split-image-header {

    z-index: 1;

    position: absolute;

    left: 0;

    right: 0;

    bottom: 0;

    top: 0;

    margin: auto;

    color: #fff;

}

.split-image-links .split-image-link .zoom-image .zoom-image-headline {

    color: #fff;

    text-align: center;

    line-height: 100vh;

    font-family: 'Poppins';

    text-transform: uppercase;

    font-size: 45px;

    font-weight: 600;

}

.split-image-links .split-image-link .zoom-image.zoom-image-first {

    background: linear-gradient(

          rgba(0, 0, 0, 0.4), 

          rgba(0, 0, 0, 0.4)

        ), url(https://api.time.com/wp-content/uploads/2020/07/jeff-bezos.jpg?quality=85&w=1024&h=628&crop=1);

    background-position: center;

    background-size: cover;

    background-repeat: no-repeat;

    transition: all 0.5s;

}

.split-image-links .split-image-link .zoom-image:hover {

    transform: scale(1.1);

}

<body>

    <div class="split-image-links">

        <div class="split-image-link">

            <div class="zoom-image zoom-image-first">

                <h1 class="zoom-image-headline">who</h1>

            </div>

        </div>

    </div>

</body>


MYYA
浏览 133回答 3
3回答

守着星空守着你

不要使用scale()整个元素,而是使用background-size属性,这种方式font-size将保持不变,让我知道它是否适合你。.bg {&nbsp; width: 400px;&nbsp; height: 240px;&nbsp; margin: 30px auto;&nbsp; background: url(https://api.time.com/wp-content/uploads/2020/07/jeff-bezos.jpg?quality=85&w=1024&h=628&crop=1) no-repeat center center;&nbsp; background-size: 100%; /* work with the background-size */&nbsp; transition: background-size 1s ease-in-out;&nbsp; position: relative;&nbsp; z-index: 1;}.bg:hover {&nbsp; background-size: 120% /* work with the background-size */}.bg::before {&nbsp; position: absolute;&nbsp; content: '';&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; background: rgba(0,0,0,.5);&nbsp; top: 0;&nbsp; left: 0;&nbsp; z-index: -1;}.bg h2 {&nbsp; text-align: center;&nbsp; font-size: 60px;&nbsp; line-height: 230px;&nbsp; font-family: sans-serif;&nbsp; color: #fff;}<div class="bg">&nbsp; <h2>WHO</h2></div>

犯罪嫌疑人X

那是 的预期行为scale,它也扩展了每个孩子,只改变 bg 也许你应该使用其他方法。我提供了一个,我希望它适合你的需要。在这种方法中,BG 是一个绝对元素,不是的容器,h1悬停现在是 onsplit-image-link而不是zoom-image。* { margin: 0; padding: 0; }body {}.article-container {&nbsp; &nbsp; width: 300px;&nbsp; &nbsp; height: 200px;&nbsp; &nbsp; border: 1px solid #000000;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; position: relative;}.article-img-holder {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; background: url(https://awik.io/demo/background-image-zoom/traffic2.jpg);&nbsp; &nbsp; background-position: center;&nbsp; &nbsp; background-size: cover;&nbsp; &nbsp; background-repeat: no-repeat;&nbsp; &nbsp; transition: all 1s;}.article-img-holder:hover {&nbsp; &nbsp; transform: scale(1.2);}.split-image-links {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100vh;&nbsp; &nbsp; display: flex;}.split-image-links .split-image-link {&nbsp; &nbsp; width: 25%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; position: relative;}.split-image-links .split-image-link .zoom-image {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; position: relative;}.split-image-links .split-image-link .zoom-image .split-image-header {&nbsp; &nbsp; z-index: 1;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; right: 0;&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; top: 0;&nbsp; &nbsp; margin: auto;&nbsp; &nbsp; color: #fff;}.split-image-links .split-image-link .zoom-image-headline {&nbsp; &nbsp; position: relative;&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; line-height: 100vh;&nbsp; &nbsp; font-family: 'Poppins';&nbsp; &nbsp; text-transform: uppercase;&nbsp; &nbsp; font-size: 45px;&nbsp; &nbsp; font-weight: 600;}.split-image-links .split-image-link .zoom-image.zoom-image-first {&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; right: 0;&nbsp; &nbsp; top: 0;&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; margin: auto;&nbsp; &nbsp; background: linear-gradient(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rgba(0, 0, 0, 0.4),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rgba(0, 0, 0, 0.4)&nbsp; &nbsp; &nbsp; &nbsp; ), url(https://api.time.com/wp-content/uploads/2020/07/jeff-bezos.jpg?quality=85&w=1024&h=628&crop=1);&nbsp; &nbsp; background-position: center;&nbsp; &nbsp; background-size: cover;&nbsp; &nbsp; background-repeat: no-repeat;&nbsp; &nbsp; transition: all 0.5s;}.split-image-links .split-image-link:hover .zoom-image {&nbsp; &nbsp; transform: scale(1.1);}<body>&nbsp; &nbsp; <div class="split-image-links">&nbsp; &nbsp; &nbsp; &nbsp; <div class="split-image-link">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="zoom-image zoom-image-first"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1 class="zoom-image-headline">who</h1>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></body>

手掌心

这是您需要的基本版本。.content {&nbsp; position: relative;&nbsp; /* border: 1px solid red; */&nbsp; display: inline-block;}h1 {&nbsp; color: #FFF;&nbsp; margin: 0;&nbsp; position: absolute;&nbsp; top: 50%;&nbsp;&nbsp;&nbsp; left: 50%;&nbsp; transform: translate(-50%, -50%);&nbsp; z-index: 999;}.bg:hover, h1:hover + .bg {&nbsp; transform: scale(1.1);&nbsp; transition: all 1s;}<div class="content">&nbsp; <h1>WHO</h1>&nbsp; <img class="bg" src="https://awik.io/demo/background-image-zoom/traffic2.jpg" alt="background image"></div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答