CSS3实现毛玻璃效果_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:25:54
原创
1788人浏览过

**首先看效果**

**引入下题**

HTML:

<img  id="blur" src="img/back.jpg" alt="CSS3实现毛玻璃效果_html/css_WEB-ITnose" >
登录后复制

CSS3:

立即学习前端免费学习笔记(深入)”;

火龙果写作
火龙果写作

用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。

火龙果写作 106
查看详情 火龙果写作
#blur{filter:blur(10px);-webkit-filter:blur(10px);-moz-filter:blur(10px);-ms-filter:blur(10px);-o-filter:blur(10px);}
登录后复制

**看个复杂点的例子**

查看演示

结 合CSS3毛玻璃实现 微信版的发红包看完整照片效果。可以完美的兼容移动端与PC端。代码如下:

html:

<div id="content"><!--模糊图片--><img  id="blur" src="img/4.jpg" alt="CSS3实现毛玻璃效果_html/css_WEB-ITnose" ><!--使用canvas绘制图片--><canvas id="canvas"></canvas><a href="javascript:reset()" rel="external nofollow"  class="button" id="button-reset">reset</a><a href="javascript:show()" rel="external nofollow"  class="button" id="button-show">show</a></div>
登录后复制

css3:

body,html{margin:0;padding:0;overflow:hidden}#content{position:relative;margin:0 auto;overflow:hidden}#blur{position:absolute;margin:0 auto;left:0;top:0;filter:blur(10px);-webkit-filter:blur(18px);-moz-filter:blur(18px);-ms-filter:blur(18px);-o-filter:blur(18px);z-index:0;display:block}#canvas{position:absolute;margin:0 auto;left:0;top:0;z-index:1;display:block}.button{padding:5px 10px;color:#fff;position:absolute;display:block;text-align:center;border-radius:4px;text-decoration:none;z-index:3}#button-reset{left:10%;bottom:5%;background-color:red}#button-show{right:10%;bottom:5%;background-color:green}
登录后复制

javascript:

var canWidth = window.innerWidth-20;var canHeight = window.innerHeight-20;var canvas=document.getElementById("canvas");var ctx = canvas.getContext("2d");canvas.width = canWidth;canvas.height = canHeight;var img = new Image();img.src = "img/4.jpg";var radius = 50;var leftMargin = 0,topMargin = 0;img.onload = function(){//初始化父容器的宽高使窗口与canvas宽高相同$("#content").css({"width":canvas.width + "px","height":canvas.height + "px"});//模糊图片与canva中绘制的图片(隐藏的清楚图片)宽高相同,别忘加px,否则计算出来的只是个值,不带px$("#blur").css("width",img.width + "px");$("#blur").css("height",img.height + "px");//左边距:(图片宽高 - canvas宽高) / 2;等于一边的边距宽高leftMargin = (img.width - canvas.width)/2;topMargin = (img.height - canvas.height)/2;//模糊图片的上边距与左边距$("#blur").css("top", String(-topMargin) + "px");$("#blur").css("left", String(-leftMargin) + "px");initCanvas();}function initCanvas(){var theleft = leftMargin < 0 ? -leftMargin : 0;var thetop = topMargin < 0 ? -topMargin : 0;//创建圆region = {x:Math.random() * (canvas.width-2 * radius - 2 * theleft) + radius + theleft,y:Math.random() * (canvas.height-2 * radius - 2 * thetop) + radius + thetop, r : radius};draw(img,region);}function draw(img){ctx.clearRect(0,0,canvas.width,canvas.height);ctx.save();setRegion(region);/*绘制清楚图片,如leftMargin<0那么取0,图片宽度与canvas宽度哪个最小取哪个值*/ctx.drawImage(img, Math.max(leftMargin,0), Math.max(topMargin,0),Math.min(canvas.width,img.width), Math.min(canvas.height,img.height),leftMargin < 0 ? -leftMargin : 0, topMargin < 0 ? -topMargin : 0,Math.min(canvas.width,img.width),Math.min(canvas.height,img.height));ctx.restore();}function setRegion(region){ctx.beginPath();ctx.arc(region.x,region.y,region.r,0,Math.PI * 2,false);//进行裁减圆ctx.clip();}function reset(){initCanvas();}function show(){var animation = setInterval(function(){region.r += 20;if(region.r > Math.max(canvas.width,canvas.height)){clearInterval(animation);}draw(img,region);},30);}
登录后复制

查看演示

知识点:

  • h5头部别忘引入viewport:
  • filter : blur CSS3默认的滤镜模糊效果。
  • canvas绘制图片,drawImage();
  • canvas arc();   用canvas绘制圆形;
  • canvas clip(); 剪切区域;
  • setinterval();  与 clearInterval();
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号