UI小姐姐说我用CSS实现毛玻璃效果的样子很帅

点击上方 前端阳光,关注公众号

回复加群,加入技术交流群交流群

前言

UI小姐姐问我,能不能做出透明加模糊的背景,而我当然是二话不说就说可以。因为我觉得没有什么是css实现不了的。更何况我要在她面前展现得我很厉害的样子。e57d7fb23922b068acf396b4462d86bd.png

开发起来

果不其然,在我打开蓝湖后,发现属性都给我提供好了

1170040108aed518d5122575c0e2dbe7.png

于是我立即将这份代码ctr c,然后ctr v,一番丰功伟绩立马就完成了,效果也是杠杠滴。

10b351d4a645b4f4e354b5f3a1ad82bd.png

然后兴高采烈地交付给UI小姐姐查看了。小姐姐也说可以。

bbaa89cd02d1a658a7f3ba468b54bc7a.png 79804d20fcf6aa1d45d8c69fad600ca5.gif

出于职业素养,我马上拿起我在pdd上9.9买的iphone13手机(当时也就邀请了我老家整个镇子的人来帮我砍一刀吧)查看效果,哇塞真机效果不错啊

但是等到验收,UI小姐姐就告诉我安卓显示有问题。

708daac95a3cb1857ed0e15f257ae4c4.gif

然后我马上查看了一下backdrop-filter的兼容性。

9cfeb52b8908872b27317728c0f69bd5.png 120a0116af2ee856ffa53ab101605283.png

果然,又是一个兼容性问题的属性。

只能放弃了,好可惜,本来用一个属性就能完成需求了。

好的,问题不大,印象中还有一个属性也可以实现模糊效果,叫filter来着,试试看怎么用它来实现吧。

建立一个html文件来模拟一下吧

<head>
 <style>
  html,
  body {
   margin: 0;
   width: 100%;
   height: 100%;
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0);
   background-size: 100% 100%;
   overflow: hidden;
  }

  .card {
   margin: 100px auto;
   width: 300px;
   height: 300px;
   position: relative;
   border: 1px solid #000;
   color: white;
  }

 </style>

</head>

<body>
 <div class="card">
  123123123123123123123123123123123123123123123123123123123123123123123123123123123123
 </div>
</body>

</html>
e9656c27d480fca565ecabb590fd8847.png

如图所示,现在是只有一个框框。

然后我,稍加修饰,用下fitler。

<html lang="en">

 <style>
  html,
  body {
   margin: 0;
   width: 100%;
   height: 100%;
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0);
   background-size: 100% 100%;
   overflow: hidden;
  }

  .card {
   margin: 100px auto;
   width: 300px;
   height: 300px;
   position: relative;
   border: 1px solid #000;
   color: white;
   filter: blur(10px);
   background-color: rgba(0,0,0,.3);
  }

 </style>


<body>
 <div class="card">
  123123123123123123123123123123123123123123123123123123123123123123123123123123123123
 </div>
</body>

</html>

110d8eb33d0e04d1bd6c60b5f939d87d.png

模糊了吗?确实模糊了。但是有毛玻璃效果吗?没有,毛都没有。我们看下 用backdrop-filter是什么效果的。

.card {
  margin: 100px auto;
  width: 300px;
  height: 300px;
  position: relative;
  border: 1px solid #000;
  color: white;
  backdrop-filter: blur(10px);
  background-color: rgba(0,0,0,.3);
}

ca794084b1c568d64ac80ed687addbeb.png

看,这才是毛玻璃的效果好吧?为什么filter就不行了?google一下下。

5cc7544221b1425fa250416f119bbe0e.png

果然,和我猜测的一样(马后炮)

那该怎么样才能模拟呢?

因为是对图片效果才能模糊,想到一个好方法,就是我们要是能取父盒子的背景图片的一块 做为背景就好了。

b29887902ca8fa0aa9173a2d0b0e5f9d.png

例如,在这里我们取索隆的半张脸作为card盒子的背景。

该怎么取?

这就有一个属性要登场啦。

background-attachment:fixed

背景图片相对于视口固定,就算元素有了滚动条,背景图也不随内容移动。

fixed用法如下:

<style>
body{
    background-image: url(img/cartooncat.png);
    background-position: bottom left;
    background-attachment: fixed;
    background-repeat: no-repeat;
    height: 1000px;
}
</style>
</head>
<body>
    <h1>下拉看效果:</h1>
</body>
aad0c280091711fe01f822d2bc17e7f7.png

另一个作用是,它可以「近似于」取父元素背景图的某一块区域。

也就是我们上面所做的假设 「就是我们要是能取父盒子的背景图片的一块 做为背景就好了」

<html lang="en">

 <style>
  html,
  body {
   margin: 0;
   width: 100%;
   height: 100%;
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0);
   background-size: 100% 100%;
   overflow: hidden;
  }

  .card {
   margin: 200px auto;
   width: 300px;
   height: 300px;
   position: relative;
   border: 1px solid #000;
   color: white;
   backdrop-filter: blur(10px);
   background-color: rgba(0,0,0,.3);
  }

  .card::before {
   content: ' ';
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   z-index: 0;
   /* filter: blur(10px); */
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0) no-repeat center;
   /* background-attachment: fixed; */
   background-size: cover; 
   margin: -20px;
  }

 </style>


<body>
 <div class="card">
  123123123123123123123123123123123123123123123123123123123123123123123123123123123123
 </div>
</body>

</html>

看看没有使用background-attachment: fixed的情况

6db4a77bbb44e5286c9e66fec0b66ddb.png

再看看使用的情况

.card::before {
   content: ' ';
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   z-index: 0;
   /* filter: blur(10px); */
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0) no-repeat center;
   background-attachment: fixed;
   background-size: cover; 
   margin: -20px;
  }
496054577b66c5927bb2bef8833c38b6.png

可以看取到的差不多就是那块区域。

再看看模糊效果啦。

38b0bd5bfde9fb07d4138b1b6a5cc5d6.png

可以看到 毛玻璃还原度 已经很明显了,但是呢因为设计稿的背景原本是颜色的,现在换成了图片,我们需要换成颜色的话,就需要再添加一个伪元素来模拟颜色。

.card::after {
   content: ' ';
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   z-index: 0;
   background-color: rgba(0, 0, 0, 0.2);
  }
e35480cea30a7e5ca0b820d7ab0a0390.png

看,很完美了。

但是文字被覆盖了,所以我们需要提升文字的层级

<html lang="en">

 <style>
  html,
  body {
   margin: 0;
   width: 100%;
   height: 100%;
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0);
   background-size: 100% 100%;
   overflow: hidden;
  }

  .card {
   margin: 100px auto;
   width: 300px;
   height: 300px;
   position: relative;
   border: 1px solid #000;
   color: white;
   backdrop-filter: blur(10px);
   background-color: rgba(0,0,0,.3);
  }

  .text {
   position: relative;
   z-index: 1;
  }

  .card::before {
   content: ' ';
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   z-index: 0;
   filter: blur(10px);
   background: url(https://puui.qpic.cn/qqvideo_ori/0/x3311cyuqaf_496_280/0) no-repeat center;
   background-attachment: fixed;
   background-size: cover; 
   margin: -20px;
  }

  .card::after {
   content: ' ';
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   z-index: 0;
   background-color: rgba(0, 0, 0, 0.2);
  }
 </style>


<body>
 <div class="card">
  <div class="text">123123123123123123123123123123123123123123123123123123123123123123123123123123123123</div>
 </div>
</body>

</html>
f9de3e2957cd1f93d9ae4b57fd7519ec.png

完美。

然后,我马上跟UI小姐姐说我做好了。验收通过,不知道UI小姐姐爱上我了没有。5cd7c9d1e49b0f03360ae04c1c2aab0f.png

结尾

已上大多数内容是来自一个渴望爱情又得不到爱情,敲代码还总报错的小傻瓜的 yy,真实故事与UI小姐姐无关

技术交流群

我组建了技术交流群,里面有很多 大佬,欢迎进来交流、学习、共建。回复 加群 即可。后台回复「电子书」即可免费获取 27本 精选的前端电子书!

0239e6786065dc682bf752ea5c8edf2a.png

   “分享、点赞、在看” 支持一波👍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤在Android中实现毛玻璃效果的对话框: 1. 首先,在你的项目中添加依赖项,以使用GlassActionBar库。在app级别的build.gradle文件中添加以下代码: ```groovy implementation 'com.github.glomadrian:GlassActionBar:0.5@aar' ``` 2. 在你的布局文件中,使用`FrameLayout`作为根布局,并将`GlassFrameLayout`作为子布局。例如: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.glaucous.androidframework.ui.widget.GlassFrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 在这里添加对话框的内容 --> </com.github.glaucous.androidframework.ui.widget.GlassFrameLayout> </FrameLayout> ``` 3. 接下来,在你的Activity中,设置透明状态栏和将ActionBar隐藏。例如: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置透明状态栏 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } // 隐藏ActionBar if (getSupportActionBar() != null) { getSupportActionBar().hide(); } setContentView(R.layout.activity_main); } ``` 4. 最后,在Activity的`onCreateOptionsMenu()`方法中,将对话框的背景设置为毛玻璃效果。例如: ```java @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); // 获取对话框的背景视图 View glassView = findViewById(R.id.glass_view); // 设置毛玻璃效果 GlassActionBarHelper.makeTransparentActionBar(this, glassView, R.drawable.your_dialog_background); return true; } ``` 这样,你就可以在Android中实现毛玻璃效果的对话框了。记得替换代码中的布局文件和背景资源为你自己的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值