【问题标题】:CSS3 - Fading an image/element diagonallyCSS3 - 对角淡化图像/元素
【发布时间】:2015-03-13 18:15:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: css animation fadein css-animations fadeout


    【解决方案1】:

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

    所以,实际上你会:

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

    在悬停时,将伪元素置于图像顶部,同时通过转换伪元素的不透明度来产生“淡入淡出”的效果:

    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;
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

    • 哦,这是一个有趣的想法。非常感谢:)
    【解决方案2】:

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

    【讨论】:

    • 感谢您的提示,我完全同意,但我相信这更多的是作为评论而不是作为答案。我正在寻找可能不同的解决方案/方法来解决这个问题。
    • 我也在研究 .white-fade 上带有过渡效果的动画工具...
    • 啊,这将是一个有趣的想法。不是故意放下你的答案!我刚刚用渐变更新了我的问题,它看起来更好,但总的来说我仍然对更清洁的方法感到好奇。
    • 研究起来真的很有趣,我通常使用 vanillaJS,但 jquery 也很酷;它仍然可以改进......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多