How to dim an image keeping transparency untouched with CSS or JS?

前端 未结 4 869
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 18:14

Normal approach to dimming an image suggested everywhere is to change it\'s opacity attribute and display something dark under it. However, my image has transparency and is on w

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 18:21

    There is a relatively new CSS property filter which might achieve what you are after.

    The brightness option seems to be what you are after.

    EDIT - Added interim support for FF via URL

    JSFiddle Demo (with brightness and contrast options)

    CSS

    img {
    width:250px;
    }
    #one:hover {
        -webkit-filter:brightness(50%);
        -moz-filter:brightness(50%);
        filter: url(#brightness); /* required for FF */
        filter:brightness(50%);
    }
    #two:hover {
        -webkit-filter:contrast(50%);    
        -moz-filter:contrast(50%);
         filter: url(#contrast);
        filter:contrast(50%);
    }
    

    MDN on Filter

    Support is non-IE see CanIUse.com

    FF support (at the time of writing) requires definition of an SVG filter

    Brightness @ 50%

    
    
        
            
                
                
                
            
        
    
    
    

    Contrast @ 200%

    
        
            
                
                
                
            
        
    
    

提交回复
热议问题