0

在我的 wordpress 主题中,我正在尝试为图像(博客帖子缩略图)上的鼠标悬停实现图像灰度动画。我的第一个博客帖子图像有问题,它最初是淡出的,也没有着色动画正在工作仅将鼠标悬停在第一个博客文章图像上后,它仅保持灰色状态。在此之后,每个图像都以 margin-top(大约 10-20 像素)缩小。请帮助我纠正这些错误。

我的 CSS:

.thumbimage {
position:relative;
width:450px;
height:250px;
margin:10px 0px 0px 0px;
position:relative;
overflow:hidden;
}

.thumbimage .inner_shadow{
position:absolute;
top:0px;left:0px;z-index:22;
background:url(images/transparent-inner-shadow-frame.png)
}

.thumbimage img{
width:100%;
position:relative;
}

我的 wordpress 帖子包含特色图片,即获取 wordpress 功能
“the_post_thumbnail()”

我的模板.php:

<div class="thumbimage">
<div class="inner_shadow"></div> 
<a class="bimage"><?php the_post_thumbnail(); ?></a>
</div>

我的 jQuery:

<script type="text/javascript">

// On window load. This waits until images have loaded which is essential
    $(window).load(function(){

// Fade in images so there isn't a color "pop" document load and then on window load


// clone image
        $('.thumbimage a.bimage img').each(function(){
            var el = $(this);
            el.css({"position":"relative"}).wrap("<div class='thumbimage'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"10","opacity":"0"}).insertBefore(el).queue(function(){
                var el = $(this);
                el.parent().css({"width":this.width,"height":this.height});
                el.dequeue();
            });
            this.src = grayscale(this.src);
        });

        // Fade image


        $('.thumbimage').mouseover(function(){
        $(this).find('a.bimage img').stop().animate({opacity:1}, 1000);
            $(this).find('a.bimage img:first').stop().animate({opacity:1}, 1000);
        })
        $('.thumbimage').mouseout(function(){
        $(this).find('a.bimage img:first').stop().animate({opacity:0}, 1000);
        });
    });

// Grayscale w canvas method
    function grayscale(src){
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        var imgObj = new Image();
        imgObj.src = src;
        canvas.width = imgObj.width;
        canvas.height = imgObj.height;
        ctx.drawImage(imgObj, 0, 0);
        var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
        for(var y = 0; y < imgPixels.height; y++){
            for(var x = 0; x < imgPixels.width; x++){
                var i = (y * 4) * imgPixels.width + x * 4;
                var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                imgPixels.data[i] = avg;
                imgPixels.data[i + 1] = avg;
                imgPixels.data[i + 2] = avg;
            }
        }
        ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
        return canvas.toDataURL();
    }

</script>
4

2 回答 2

2

也许这可以通过 CSS3 过渡来解决。尚未测试跨浏览器兼容性,在 Chrome OSX 中运行良好。

img.attachment-thumbnail {  
    -webkit-transition: -webkit-filter .5s ease-in-out;  
    -webkit-filter: grayscale(100%);  
}  
img.attachment-thumbnail:hover {  
    -webkit-filter: grayscale(0%);  
}  

来自Tut+的想法。

关于 CSS 灰度的跨浏览器文章。

于 2013-01-12T20:19:09.547 回答
0

我通过用以下代码替换以前的代码来解决它:

// clone image
    $('.thumbimage a.bimage img').each(function(){
    var el = $(this);
    el.clone().addClass('img_grayscale').css({"position":"absolute","z-index":"11","opacity":"0"}).insertBefore(el).queue(function(){
    var el = $(this);
    el.parent().css({"width":this.width,"height":this.height});
    el.dequeue();
            });
       this.src = grayscale(this.src);
        });

///////////////////////////////////////// ////////////////////////

于 2013-01-13T16:29:12.907 回答