Javascript HTML/CSS - 在图像上创建 alpha 蒙版

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3362175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:19:01  来源:igfitidea点击:

HTML/CSS - create alpha mask over image

javascripthtmlcssfademasking

提问by sa125

I'd like to create an effect in a site I'm building where an image is masked by an overlay. The overlay should create a “fade out” effect — I don't actually want anything animated, but the overlay should make the image look as if it's fading to the background colour at the edges.

我想在我正在构建的站点中创建一个效果,其中图像被覆盖层掩盖。覆盖应该创建一个“淡出”效果——我实际上并不想要任何动画,但覆盖应该使图像看起来好像它正在边缘的背景颜色褪色。

Something like this: http://imgur.com/fqtc9.png

像这样的东西:http: //imgur.com/fqtc9.png

I prefer to do this with CSS/HTML/JS - not images. Any thoughts on where to start? Thanks.

我更喜欢用 CSS/HTML/JS 来做这件事——而不是图像。关于从哪里开始的任何想法?谢谢。

采纳答案by Nate Nolting

you could do something like this for example : Example

你可以做这样的事情,例如:示例

html

html

<div class="photo_container">
        <div class="photo">
<img src="/lakenwoods/images/mockup-photo.png" width="540" height="463" /></div>
        <div class="overlay">
            <div class="content">
                <h1>Welcome to Lake-N-Woods Realty</h1>
                <p>
                We are a Diverse and Highly Effective Real Estate Company, Dedicated to Satisfying all of our Clients Needs. We Specialize in Recreational, Rural and Investment Property throughout Bemidji and North Central Minnesota.
                </p>
                </div>
        </div>
        <div class="clear">
       </div>
   </div>

CSS

CSS

@charset "utf-8";
/* CSS Document */

.clear {
    clear:both;     
}

.photo_container {
    position: relative;
    width: 540px;
    height: 463px;
    overflow:hidden;
    margin: 0; padding:0;
}

.photo_container .photo {
    z-index:1;

}


.photo_container .overlay {
    width: 540px;
    height: 463px;
    background: url(/lakenwoods/images/mockup-overlay.png) no-repeat top center;
    position:absolute;
    bottom: 0;
    left: 0;
    z-index:10;

}

.photo_container .overlay .content h1 {

    position:absolute;
    top: 310px;
    left: 34px;
    font-family: Helvetica, Helvetica Neue, Arial, sans-serif;
    font-size:18px;
    color: #fff;
    font-weight: 700;
    width: 315px;

}


.photo_container .overlay .content p {


    position:absolute;
    top: 335px;
    left: 34px;
    font-family: Helvetica, Helvetica Neue, Arial, sans-serif;
    font-size:12px;
    color: #fff;
    width: 315px;
    line-height: 1.4em; 

}

回答by Paul D. Waite

Ah — based on your comment on antyrat's answer, your best bet is to create a PNG image that contains the effect (i.e. a semi-transparent white shape), and position it over the top of your actual image using CSS (position:absoluteand z-indexwill be involved).

啊——根据你对 antyrat 的回答的评论,你最好的办法是创建一个包含效果的 PNG 图像(即半透明的白色形状),并使用 CSS将其放置在实际图像的顶部(position:absolute并且z-index将涉及)。

You can't currently create non-square shapes using HTML and CSS alone.

您目前无法单独使用 HTML 和 CSS 创建非方形形状。

回答by JohnB

I know that you are asking specifically for a CSS/JavaScript solution, and I'm sure you have your reasons.

我知道您特别要求 CSS/JavaScript 解决方案,我相信您有自己的理由。

Nevertheless, I just wanted to throw out the simple solution of a single faded image file like you already posted in your question with no fancy programmed effects.

尽管如此,我只是想抛出一个简单的褪色图像文件的简单解决方案,就像你已经在你的问题中发布的那样,没有花哨的编程效果。

回答by antyrat

Start with jQuery. For example see fadeInfunction.

jQuery开始。例如见淡入功能。

For static alpha mask over image use opacity css property:

对于图像上的静态 alpha 蒙版,请使用 opacity css 属性:

.myimage {
  filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50); 
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5;
}

回答by smdrager

The jQuery animatefunction (with opacity) should/may be able to handle that, though antyrat's answer should work as well if you simply fadein the overlay.

jQueryanimate函数(具有不透明度)应该/可能能够处理这个问题,尽管如果您只是淡入叠加层,antyrat 的答案也应该有效。