javascript - 颜色叠加在不透明的div上

标签 javascript jquery html css

我有三个像这样彼此相邻的 div:

enter image description here

HTML 看起来像这样:

<div class="row">

<div class="col col--lg-4 col--md-6">
    <div class="reserved__inner">
        <img src="images/image1.png" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 1</a>
        </div>
    </div>
</div>

<div class="col col--lg-4 col--md-6">
    <div class="reserved__inner">
        <div class="overlay"></div>
        <img src="images/image2.png" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 2</a>
        </div>
    </div>
</div>

<div class="col col--lg-4 col--md-6">
    <div class="overlay"></div>
    <div class="reserved__inner">
        <img src="images/image3.png" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 2</a>
        </div>
    </div>
</div>

如您所见,我的 div 中还有一个 p 元素 和一个 a 元素,但它们是隐藏的。在我的 javascript 文件中,我有这个:

$('.reserved__inner').on({
    mouseenter: function() {
        $(this).find('h3').addClass('black');
        $(this).find('p').show();
        $(this).find('a').show();

    },
    mouseleave: function() {
        $(this).find('h3').removeClass('black');
        $(this).find('p').hide();
        $(this).find('a').hide();
    }
});

当你将鼠标悬停在上面时,你会看到:

enter image description here

但现在我还想在您将鼠标悬停在图像上时在图像上覆盖一层。像这样:

enter image description here

我的 CSS 目前看起来像这样:

.reserved__inner {
    width: 100%;
    min-height: 380px;
    position: relative;
    overflow: hidden;
}

.reserved__inner img {
    min-height: 100%;
    height: auto!important;
    min-width: 100%;
    width: auto!important;
    position: absolute;
    top: -9999px;
    bottom: -9999px;
    left: -9999px;
    right: -9999px;
    margin: auto;
}

.reserved__text {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 56px 54px 50px 58px;
    /*cursor: pointer;*/
}

.reserved__text h3 {
    margin-bottom: 12px;
    line-height: 35px;
}

.reserved__text h3 sup {
    font-size: 10px;
}

.reserved__text h3.first {
    color: #fff;
}

.reserved__text p {
    line-height: 24px;
    margin-bottom: 14px;
}

但我真的对如何创建颜色叠加感到困惑。有人可以帮我解决这个问题吗?

最佳答案

通过稍微不同的思考,您可以很容易地做到这一点。

只需将背景颜色添加到 .reserved_inner 类,然后降低图像本身的不透明度。

.reserved__inner {
    width: 100%;
    min-height: 380px;
    position: relative;
    overflow: hidden;
     background: #a00; /* add a background color here */
}

.reserved__inner img {
    min-height: 100%;
    height: auto!important;
    min-width: 100%;
    width: auto!important;
    position: absolute;
    top: -9999px;
    bottom: -9999px;
    left: -9999px;
    right: -9999px;
    margin: auto;
     opacity: .5; /* Lower the image opacity here */
}

$('.reserved__inner').on({
    mouseenter: function() {
        $(this).find('h3').addClass('black');
        $(this).find('p').show();
        $(this).find('a').show();
      $(this).children('img').css({opacity: '.3'}); /* Changes image opacity */

    },
    mouseleave: function() {
        $(this).find('h3').removeClass('black');
        $(this).find('p').hide();
        $(this).find('a').hide();
      $(this).children('img').css({opacity: '.5'});/* Changes image opacity */
    }
});
.reserved__inner {
    width: 100%;
    min-height: 380px;
    position: relative;
    overflow: hidden;
	 background: #a00; /* add a background color here */
}

.reserved__inner img {
    min-height: 100%;
    height: auto!important;
    min-width: 100%;
    width: auto!important;
    position: absolute;
    top: -9999px;
    bottom: -9999px;
    left: -9999px;
    right: -9999px;
    margin: auto;
	 opacity: .5; /* Lower the iamge opacity here */
}

.reserved__text {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 56px 54px 50px 58px;
    /*cursor: pointer;*/
}

.reserved__text h3 {
    margin-bottom: 12px;
    line-height: 35px;
}

.reserved__text h3 sup {
    font-size: 10px;
}

.reserved__text h3.first {
    color: #fff;
}

.reserved__text p {
    line-height: 24px;
    margin-bottom: 14px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">

<div class="col col--lg-4 col--md-6">
    <div class="reserved__inner">
        <img src="https://placekitten.com/380/200" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 1</a>
        </div>
    </div>
</div>

<div class="col col--lg-4 col--md-6">
    <div class="reserved__inner">
        <div class="overlay"></div>
        <img src="https://placekitten.com/380/200" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 2</a>
        </div>
    </div>
</div>

<div class="col col--lg-4 col--md-6">
    <div class="overlay"></div>
    <div class="reserved__inner">
        <img src="https://placekitten.com/380/200" alt="" />
        <div class="reserved__text">
            <h3 class="first">Lorem ipsum</h3>
            <p>lorem ipsum text</p>
            <a href="#" class="button">Link 2</a>
        </div>
    </div>
</div>

从技术上讲,它是颜色上的图像而不是图像上的颜色,但整体效果是一样的。

关于javascript - 颜色叠加在不透明的div上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40267513/

相关文章:

javascript - 使用 d3 和 topojson 绘制 map

javascript - 按值或 ID 选择更多复选框

javascript - 使用 JavaScript 更改 <a-box> 的 src

jquery - 使用 whiteSpace-nowrap 后如何删除水平滚动条并使文本区域具有固定高度

javascript - 编码 HTML 和 JavaScript

html - 使用 CSS 自定义圆形图像

javascript - 如何即时更新一个平滑更新的进度条?

javascript - 通过 jquery 更改多个标签上使用的特定 ID 的 CSS

javascript - 动态添加一个值 will-change on click/hover jQuery

javascript - 单击时如何停止下拉菜单子(monad)项向上滑动