jquery - using jscript to create greyscale to colour effect on images -
i using following technique make images go greyscale colour on hover: http://solemone.de/code/code-examples/demos-grayscale-hover-effect-with-html5-canvas-and-jquery/
the issue i'm having when hover image goes original size instead of fitting in the sytle width has been set to.
jscript code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> <script> $(window).load(function() { $('.imglist img').each(function() { $(this).wrap('<div style="display:inline-block;width:' + this.width + 'px;height:' + this.height + 'px;">').clone().addclass('gotcolors').css('position', 'absolute').insertbefore(this); this.src = grayscale(this.src); }).animate({opacity: 1}, 500); }); $(document).ready(function() { $("#imglist1 a").hover( function() { $(this).find('.gotcolors').stop().animate({opacity: 1}, 200); }, function() { $(this).find('.gotcolors').stop().animate({opacity: 0}, 500); } ); }); function grayscale(src) { var supportscanvas = !!document.createelement('canvas').getcontext; if (supportscanvas) { var canvas = document.createelement('canvas'), context = canvas.getcontext('2d'), imagedata, px, length, = 0, gray, img = new image(); img.src = src; canvas.width = img.width; canvas.height = img.height; context.drawimage(img, 0, 0); imagedata = context.getimagedata(0, 0, canvas.width, canvas.height); px = imagedata.data; length = px.length; (; < length; += 4) { gray = px[i] * .3 + px[i + 1] * .59 + px[i + 2] * .11; px[i] = px[i + 1] = px[i + 2] = gray; } context.putimagedata(imagedata, 0, 0); return canvas.todataurl(); } else { return src; } } window.requestanimframe = (function() { return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimationframe || function(callback, element) { window.settimeout(callback, 1000 / 60); }; })(); </script>
cs code:
.imglist { margin:0; padding:0; list-style:none; margin:0 0 0 -15px; width:100%; display:block; } .imglist li { margin: 0 0 0 15px; float: left; } .imglist { display:block } .imglist img { opacity:0; }
html code:
<li><a title="it's me!"><img src="images/profile.jpeg" alt="profile picture" style="width:100%;"></a></li>
any or advice welcome. maybe there better , easier way of doing this? tried doing css didn't have luck. thanks.
why don't use css3? can use grey-scale filter animation on hover (source):
.animated-grayscale { -webkit-transition: .5s ease; -moz-transition: .5s ease; -ms-transition: .5s ease; -o-transition: .5s ease; transition: .5s ease; -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); filter: grayscale(100%); filter: url(grayscale.svg); /* firefox 4+ */ filter: gray; /* ie 6-9 */ } .animated-greyscale:hover { -webkit-filter: grayscale(0%); -moz-filter: grayscale(0%); -ms-filter: grayscale(0%); -o-filter: grayscale(0%); filter: grayscale(0%); filter: none; }
this removes greyscale on hover, you've requested in original question. here's fiddle - http://jsfiddle.net/j0wm1oj5/
Comments
Post a Comment