Last active
April 21, 2020 18:41
-
-
Save taciara/2c0108cc0dd9a637d9876a16b7f3b9ed to your computer and use it in GitHub Desktop.
Como utilizar imagem Webp em sites. Isso é muito útil para otimização de sites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- OPÇÃO 1: Site Estático, de forma bem manual --> | |
| <picture> | |
| <source srcset="image.webp" type="image/webp"> | |
| <img src="image.png" alt="" title=""> | |
| </picture> | |
| <!-- TERMINA AQUI --> | |
| <!-- OPÇÃO2: Site estático, mas de forma automatia --> | |
| <!-- no css (caso seja necessário) --> | |
| <style> | |
| html.webp div{background-image: url(image.webp);} | |
| html div{background-image: url(image.jpg);} | |
| </style> | |
| <!-- no html --> | |
| <img data-jpg="image.jpg" data-webp="image.webp" class="myimg" alt="" title=""> | |
| <!-- chamar os js --> | |
| <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script> | |
| <script type="text/javascript"> | |
| $(document).ready(function() { | |
| Modernizr.on('webp', function (result) { | |
| var img = $('.myimg'); | |
| for (var i = 0; i < img.length; i++) { | |
| if (result) { | |
| img[i].src = img[i].getAttribute('data-webp'); | |
| } | |
| else { | |
| img[i].src = img[i].getAttribute('data-jpg'); | |
| } | |
| } | |
| }); | |
| }); | |
| </script> | |
| <!-- TERMINA AQUI --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment