CSS3 - 对角淡入 image/element

CSS3 - Fading an image/element diagonally

我正在尝试对角淡化图像。我知道有很多关于淡入淡出图像的问题,我已经经历了很多,但它们都是关于从左到右、从上到下淡入淡出,或者在淡入淡出时移动。

我正在努力使图像从一个角到另一个角具有淡入淡出效果。我能想到的最好办法是旋转一个白色块并增加它的宽度。

$(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>

我想知道是否有更简洁的方法来实现这种效果?似乎不​​可避免地需要一些 JavaScript。我添加了一个渐变以使其更清晰,但我不确定是否有办法使用 CSS3 动画来做到这一点?

我建议在 wrapper 上设置 heightwidth 属性,这样你就不会增加高度,

我还在一个带有线性渐变的选择器上设置了宽度动画,所以它更细,结果看起来不错!!!

$(document).ready(function() {
    setInterval(function(){

$( ".white-fade" ).animate({
width: "toggle",

   
  }, 2000);
});
  });
.wrapper {
    position: relative;
    width:100px;
    height:100px;
    overflow:hidden;
}
.block {
    width: 100px;
    height: 100px;
    opacity: 1;
    transition: opacity 0.5s ease-in-out;
}

.white-fade {
    position: absolute;
    top: 0;
    left: 0;
    
    top: -25px;
    left: -20px;
    transform: rotate(45deg);
    transform-origin: 75px 75px;
    width: 300px;
    height: 150px;
    background: white;
    background: linear-gradient(to right, rgba(255,255,255,1) 0%,rgba(255,255,255,1) 66%,rgba(255,255,255,0) 100%); /* W3C */
    opacity:1;
}

/*.block-hide {
    opacity: 0;
}

.white-fade-reveal {
    width: 100%;
}*/
<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>

您可以为此使用旋转的伪元素。

所以,实际上你会:

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

悬停时,将伪元素置于图像顶部,通过过渡伪元素的不透明度来实现 'fade' 的效果:

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>