Search code examples
cssanimationfadeincss-animationsfadeout

CSS3 - Fading an image/element diagonally


I am trying to fade an image diagonally. I know there are dozens of questions regarding fading images and I've gone through a lot of them but they all are either about fading left-to-right, top-to-bottom, or moving while fading.

I'm trying to make it so that an image will have a fade effect from one corner to another. The best I could come up with was rotating a white block and increasing the width of it in place.

$(document).ready(function() {
    setInterval(function(){
        $('.white-fade').toggleClass('white-fade-reveal');
        $('.block').toggleClass('block-hide');
    }, 2000);
});
.wrapper {
    position: relative;
}
.block {
    width: 100px;
    height: 100px;
    opacity: 1;
    transition-delay: 1s;
    transition: opacity 0.75s ease-in-out;
}

.white-fade {
    position: absolute;
    top: -25px;
    left: -20px;
    transform: rotate(45deg);
    transform-origin: 75px 75px;
    transition: width 1s ease-in-out;
    width: 0%;
    height: 150px;
background: -moz-linear-gradient(left,  rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* IE10+ */
background: linear-gradient(to right,  rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#00ffffff',GradientType=1 ); /* IE6-9 */

}

.block-hide {
    opacity: 0;
}

.white-fade-reveal {
    width: 150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
    <img class="block" src="http://images.clipartpanda.com/smiley-face-transparent-background-awesome-smiley-background-2549-hd-wallpapers.png" alt="smiley"/>
    <div class="white-fade"></div>
</div>

I was wondering if there was a cleaner approach to achieving this effect? It seems inevitable that some JavaScript is necessary. I added a gradient to make it cleaner but I'm not sure if there's a way to do it with CSS3 animations?


Solution

  • You could use a rotated Pseudo element for this.

    So, in effect you would have:

     +---+
     |   | <-- pseudo element (rotated 45 deg)
     +---+
        +---+
        |   | <-- image element
        +---+
    

    And the on hover, bring the pseudo element over the top of the image, giving the effect of the 'fade' by transitioning the opacity of the pseudo as well:

    div {
      height: 300px;
      width: 300px;
      background: url(http://placekitten.com/g/300/300);
      position: relative;
      transition: all 0.8s;
      overflow:hidden;
    }
    div:before {
      content: "";
      position: absolute;
      height: 150%;
      width: 150%;
      top: -150%;
      left: -150%;
      transition: all 2.3s;
      background: white;
      opacity: 0;
      transform: rotate(45deg);
    }
    div:hover:before {
      top: -25%;
      left: -25%;
      opacity: 1;
    }
    <div></div>