HTML 5聯(lián)手jQuery實(shí)現(xiàn)超酷圖像灰度漸變效果
曾幾何時(shí),網(wǎng)站上顯示的灰度圖像必須手動(dòng)進(jìn)行轉(zhuǎn)換。現(xiàn)在使用HTML 5畫(huà)布,圖像可以被巧妙的轉(zhuǎn)換為灰色,而不必使用圖像編輯軟件。我下面有一個(gè)示例,展示如何使用HTML 5和jQuery動(dòng)態(tài)的將彩色圖像轉(zhuǎn)換為灰色。貢獻(xiàn)者:感謝達(dá)西·克拉克(我在Themify的合伙人)貢獻(xiàn)jQuery和Javascript代碼。
51CTO推薦專題:HTML 5 下一代Web開(kāi)發(fā)標(biāo)準(zhǔn)詳解
示例:HTML5灰度漸變(http://webdesignerwall.com/demo/html5-grayscale/)
目的
這個(gè)示例的目的是向你展示如何使用HTML5和jQuery創(chuàng)建一個(gè)灰度/彩色圖像的鼠標(biāo)懸浮效果。在HTML5出現(xiàn)前,實(shí)現(xiàn)這個(gè)效果需要兩幅圖像,彩色的和灰度的版本?,F(xiàn)在HTML5讓創(chuàng)建這個(gè)效果更加容易和高效,因?yàn)榛疑珗D像將會(huì)直接從原始文件生成。我希望你會(huì)發(fā)現(xiàn)這個(gè)腳本在例如陳列櫥或者相片冊(cè)的設(shè)計(jì)里相當(dāng)有用。
jQuery代碼
下面的jQuery代碼會(huì)找尋目標(biāo)圖像并生成一個(gè)灰度的版本。當(dāng)鼠標(biāo)懸浮在圖像上,代碼將會(huì)把灰度圖像漸變?yōu)椴噬摹?/p>
- <mce:script src="jquery.min.js" mce_src="jquery.min.js" type="text/javascript"></mce:script>
- <mce: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
- $(".item img").fadeIn(500);
- // clone image
- $('.item img').each(function(){
- var el = $(this);
- el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style="display: inline-block" mce_style="display: inline-block">").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","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
- $('.item img').mouseover(function(){
- $(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
- })
- $('.img_grayscale').mouseout(function(){
- $(this).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();
- }
- // --></mce:script>
如何使用
在你的站點(diǎn)上使用這個(gè)效果:
◆引用jQuery.js
◆粘貼以上的代碼
◆設(shè)置目標(biāo)圖像(例如.post-img, img, .gallery img等等)
◆你可以更改動(dòng)畫(huà)的速度(例如1000=1秒)
兼容性
可以工作在任何支持HTML5和Javascript的瀏覽器上,例如Chrome、Safari和Firefox。如果瀏覽器不支持HTML5,這個(gè)效果將會(huì)退回到原始的彩色圖片。注意:如果本地文件在Firefox和Chrome上不工作,你必須要把HTML代碼放到一個(gè)Web服務(wù)器上。
原文鏈接:http://blog.csdn.net/hfahe/archive/2011/02/25/6208765.aspx
【編輯推薦】