webp图片应用
<html> <head> <meta charset="UTF-8" /> <title>WEBP TEST</title> <script charset="utf-8" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> var testWebp = function(callback){ var image = new Image(); image.onerror = function() { callback(false); }; image.onload = function() { callback(image.width == 1); }; image.src = 'data:image/webp;base64,UklGRiwAAABXRUJQVlA4ICAAAAAUAgCdASoBAAEAL/3+/3+CAB/AAAFzrNsAAP5QAAAAAA=='; },webSrc = function(src){ var suffix = src.lastIndexOf('.'); suffix = src.substr(suffix); if (/png|jpg/.test(suffix)){ return src.substr(0,(src.length-3))+'webp'; }else{ return src; } } $(function(){ var $note = $('.note'); testWebp(function(SUP){ var $img = $('img[data-img]'); if(SUP){ $('body').addClass('webp'); $img.each(function(i,o){ var $o = $(o), src = $o.attr('data-img'); $o.attr('src',webSrc(src)); }); $note.html('你载入的是<strong>webp</strong>格式'); }else{ $('body').addClass('nowebp'); $img.each(function(i,o){ var $o = $(o), src = $o.attr('data-img'); $o.attr('src',src); }); $note.html('你载入的是<strong>png、jpg</strong>格式'); } }); }); </script> <style type="text/css"> *{margin:0;padding:0;} body{font:12px/1.6 arial,\5fae\8f6f\96c5\9ed1;color:#666;} strong{color:#000;padding:0 2px;} img{width:313px;height:219px;} .img{ margin-top:20px; width:313px;height:214px; } .webp .img{ background:url(http://demo.btorange.com/article/webp/demo2.webp) no-repeat; } .nowebp .img{ background:url(http://demo.btorange.com/article/webp/demo2.png) no-repeat; } </style> </head> <body> <p class="note"></p> <!-- img图片 --> <img alt="" data-img="http://demo.btorange.com/article/webp/demo1.jpg" data-pinit="registered" /> <!-- 样式中的图片 --> <div class="img"></div> </body> </html>
来源于:http://blog.csdn.net/xcysuccess3/article/details/38845625
相关推荐
islamabad 2020-06-04
islamabad 2020-06-04
jusewe 2019-12-18
jusewe 2019-11-17
mymyblog 2019-10-25
郭大路路 2019-10-11
jusewe 2017-12-15
wallowyou 2019-09-05
郭大路路 2019-02-28
zwblcz 2019-08-21
islamabad 2018-03-29
mymyblog 2019-07-01
islamabad 2017-12-15
mymyblog 2017-06-07
jusewe 2016-07-20
郭大路路 2019-06-28
jusewe 2019-06-27
islamabad 2019-06-26