基于webkit的图片遮盖效果

基于webkit的图片遮罩动画效果

为了实现例子中的效果,我们需要使用-webkit-mask 这个特殊的属性

实现步骤

1 html

<div id="examples">
<img class="type1" src="images/logo.png" />
<img class="type2" src="images/logo2.png" />
<img class="type3" src="images/logo3.png" />
<img class="type4" src="images/logo4.png" />
</div>

2 javascript 通过扩张径向渐变的梯度使其匀速到达图片的底部

$(document).ready(function(){
 $('#examples img').hover(function () { 
var $imgObj = $(this);
        // class name
var sClass = $(this).attr('class');
        // radius
var iRad = 0;
        // interval
var iInt;
if (iInt) window.clearInterval(iInt);
        // loop until end
iInt = window.setInterval(function() {
var iWidth = $imgObj.width();
var iHalfWidth = iWidth / 2;
var iHalfHeight = $imgObj.height() / 2;
            if (sClass == 'type1') {
$imgObj.css('-webkit-mask', '-webkit-gradient(radial, '+iHalfWidth+' '+iHalfHeight+', '+ iRad +', '+iHalfWidth+' '+iHalfHeight+', '+ (iRad + 30) +', from(rgb(0, 0, 0)), color-stop(0.5, rgba(0, 0, 0, 0.2)), to(rgb(0, 0, 0)))');
} else if (sClass == 'type2') {
$imgObj.css('-webkit-mask', '-webkit-gradient(radial, '+iHalfHeight+' '+iHalfHeight+', '+ iRad +', '+iHalfHeight+' '+iHalfHeight+', '+ (iRad + 30) +', from(rgb(0, 0, 0)), color-stop(0.5, rgba(0, 0, 0, 0.2)), to(rgb(0, 0, 0)))');
}
            // when radius is more than our width - stop loop
if (iRad > iWidth) {
window.clearInterval(iInt);
}
            iRad+=2;
}, 10);
});
});

3 CSS3 通过一张透明图片实现另一半光感的效果

.type3 {
    -webkit-mask: url(../images/mask.png) no-repeat center center;
}
.type3:hover{
    -webkit-animation: loop_frames 1s ease-in-out infinite;
     -webkit-animation-direction:alternate;
     -webkit-mask-size: auto 100%;
}
@-webkit-keyframes loop_frames {
     0% { -webkit-mask-size: auto 100%; }
     100% { -webkit-mask-size: auto 70%; }
}

.type4 {
    -webkit-transition: -webkit-mask-position 0.5s ease;
    -webkit-mask-size: 400px 300px;
    -webkit-mask-image: -webkit-gradient(linear, left top, right top, color-stop(0.00, rgba(0,0,0,1)), color-stop(0.90, rgba(0,0,0,1)), color-stop(1.00, rgba(0,0,0,0)));
    -webkit-mask-position-x: 400px;
}
.type4:hover {
     -webkit-mask-position-x: 0;
}

出处 http://www.script-tutorials.com/webkit-image-effects-with-masks/

例子 http://www.script-tutorials.com/demos/332/index.html

posted @ 2015-07-27 17:30  一渡  阅读(309)  评论(0)    收藏  举报