资源图片阴影
作者:互联网
<html> <head> <title>demo</title> <style> .nav { width:60px; height:60px; position: relative; background-color: #fff; padding: 20px; transition: 0.5s; border-radius: 80px; overflow: hidden; box-shadow: 0 8px 15px rgba(0,0,0,.2); background: url(data:image;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyAIIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAoorjviH8QtO8AaMLicCe/nyLW0DYLkdWPoo7n8KAOo1DUbLSrKS81C7gtbaP70szhFH4mvMNU+OulNdNZeF9JvteuR/FGpii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+CPvk9ewrb0fwR471RgdN0O38PxH/l91GTzJwP9kYJU/RR9a9B8KfBbQdDuxqerzS67qxbeZ7sZQN6hCTk+7E+2KlUZTd5aGtTMlTpKjh9lpdnCfDj4far408Sr438WxFLTeJba2dcecR9z5T0jHGP72O45P0NRRXSkkrI8YKKKKYBRRRQAUUUUAFFFFABRRRQAVRn0bSrpi1xptnMx6mSBW/mKKKAI4/D2iRNuj0fT0I7rbIP6VoJGkaBI0VVHQKMAUUUAOooooAKKKKACiiigAooooA//9k=) ; background-size: contain; background-position:50% 50%; background-size: contain; background-repeat:no-repeat; } </style> </head> <body> <div class="nav"></div> </body> </html>
显示效果图如下:
1.图片背景阴影
<html> <head> <title>demo</title> <style> .nav { width: 80px; height: 80px; position: relative; background-color: #fff; padding: 20px; transition: 0.5s; border-radius: 80px; overflow: hidden; box-shadow: 0 8px 15px rgba(0, 0, 0, .2); background-position: 50% 50%; background-repeat: no-repeat; } </style> </head> <body> <div class="nav" id="logo1"></div> <script type="text/javascript"> window.onload = function() { var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); var base64Img = "data:image;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyAIIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrK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igA70mTjqaKKAAk+tKPWiigBaKKKACiiigAooooA//9k="; document.getElementById('logo1').style.backgroundImage = "url(" + base64Img + ")"; var img = new Image(); img.src = base64Img; img.onload = function() { context.drawImage(img, 0, 0); let imgData = (context.getImageData(0, 0, img.width, img.height)).data; console.log("imgData:",imgData) //取(0,0)的色值 var alpha = Math.round(imgData[3] / 255 * 100) / 100; if (imgData[3] == 255) { //没有alpha值 var hex = gethex(imgData[0], imgData[1], imgData[2]); document.getElementById('logo1').style.backgroundColor = hex; } else if (alpha > 0) { //有alpha值 var rgba = imgData[0] + ',' + imgData[1] + ',' + imgData[2] + ',' + alpha; document.getElementById('logo1').style.backgroundColor = 'rgba(' + rgba + ')'; } } } // 获取16进制颜色 function gethex(r, g, b) { r = r.toString(16); g = g.toString(16); b = b.toString(16); r.length == 1 ? r = '0' + r : ''; g.length == 1 ? g = '0' + g : ''; b.length == 1 ? b = '0' + b : ''; var hex = r + g + b; if (r.slice(0, 1) == r.slice(1, 1) && g.slice(0, 1) == g.slice(1, 1) && b.slice(0, 1) == b.slice(1, 1)) { hex = r.slice(0, 1) + g.slice(0, 1) + b.slice(0, 1); } return hex; } </script> </body> </html>
显示效果图:
标签:slice,img,hex,阴影,background,var,imgData,资源,图片 来源: https://www.cnblogs.com/moon-yyl/p/15670403.html