CSS3 - 对角淡化图像/元素

时间:2015-03-12 21:02:50

标签: css3 animation fadein css-animations fadeout

我试图对角淡化图像。我知道有很多关于褪色图像的问题我已经经历了很多但是它们都要么是从左到右,从上到下褪色,要么在褪色时移动。

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

$(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动画做到这一点?

2 个答案:

答案 0 :(得分:4)

您可以使用旋转的Pseudo元素。

所以,实际上你会有:

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

on on hover,将伪元素放在图像的顶部,通过转换伪的不透明度来提供“淡入淡出”的效果:

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>

答案 1 :(得分:1)

我建议的一件事是在包装器上设置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>