How to have an image with css filter:blur and sharp edges?

后端 未结 5 1486
暖寄归人
暖寄归人 2021-02-06 06:04

I would like to blur an image when hovering.

The problem is that the edges of the image also blur unpleasantly. In the Fiddle you can see it clearly with the green back

5条回答
  •  温柔的废话
    2021-02-06 06:35

    Seems that setting a clip solves more or less the issue. However, there is an strange effect at the end of the transition. Forcing th GPU (via a translateZ hack) seems to solve the problem.

    CSS

    div {
        width:300px;
        height:300px;
        position: absolute;
    }
    .no {
        left: 400px;
        top: 10px;
    }
    
    .box img {
       transition: 2s -webkit-filter;
       position:absolute;
       left: -3px;
       top:-3px;
        clip: rect(5px 295px 295px 5px);
        -webkit-transform: translateZ(0px);
    }
    div img {
       transition: 2s -webkit-filter;
       position:absolute;
       left: -3px;
       top:-3px;
        clip: rect(5px 295px 295px 5px);
    }
    
    div img:hover{
        -webkit-filter: blur(8px);
    }
    

    demo with a comparative with and without the transform

提交回复
热议问题