<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                原文:[http://www.webdesignerwall.com/tutorials/html5-grayscale-image-hover/](http://www.webdesignerwall.com/tutorials/html5-grayscale-image-hover/) 譯者:蔣宇捷 轉載請標明出處:蔣宇捷(hfahe)http://blog.csdn.net/hfahe ?曾幾何時,網站上顯示的灰度圖像必須手動進行轉換。現在使用HTML5畫布,圖像可以被巧妙的轉換為灰色,而不必使用圖像編輯軟件。我下面有一個示例,展示如何使用HTML5和jQuery動態的將彩色圖像轉換為灰色。貢獻者:感謝達西·克拉克(我在[Themify](http://themify.me/)的合伙人)貢獻jQuery和Javascript代碼。 ![](https://box.kancloud.cn/2016-08-02_57a05bd59790a.gif) 示例:[HTML5灰度漸變](http://webdesignerwall.com/demo/html5-grayscale/) **目的** ?這個示例的目的是向你展示如何使用HTML5和jQuery創建一個灰度/彩色圖像的鼠標懸浮效果。在HTML5出現前,實現這個效果需要兩幅圖像,彩色的和灰度的版本。現在HTML5讓創建這個效果更加容易和高效,因為灰色圖像將會直接從原始文件生成。我希望你會發現這個腳本在例如陳列櫥或者相片冊的設計里相當有用。 ![](https://box.kancloud.cn/2016-08-02_57a05bd5abe9a.gif) ![](https://box.kancloud.cn/2016-08-02_57a05bd5c53cf.gif) ![](https://box.kancloud.cn/2016-08-02_57a05bd5f3785.gif) **jQuery代碼** ?下面的jQuery代碼會找尋目標圖像并生成一個灰度的版本。當鼠標懸浮在圖像上,代碼將會把灰度圖像漸變為彩色的。 <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>? **如何使用** 在你的站點上使用這個效果: - 引用jQuery.js - 粘貼以上的代碼 - 設置目標圖像(例如.post-img, img,.gallery img等等) - 你可以更改動畫的速度(例如1000=1秒) **兼容性** ?可以工作在任何支持HTML5和Javascript的瀏覽器上,例如Chrome、Safari和Firefox。如果瀏覽器不支持HTML5,這個效果將會退回到原始的彩色圖片。注意:如果本地文件在Firefox和Chrome上不工作,你必須要把HTML代碼放到一個Web服務器上。 **鳴謝** ?Javascript和HTML5灰度漸變代碼來自于達西·克拉克。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看