请选择 进入手机版 | 继续访问电脑版

技术控

    今日:0| 主题:61403
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Having Fun With Tomorrow’s CSS: Backdrop Filters

[复制链接]
可爱情人〆、 发表于 2016-10-6 00:13:24
398 3
In this tutorial we will look into an emerging CSS toy going by the name of Backdrop Filter .
   The backdrop-filter proposal is heavily influenced by the Apple design language, particularly that frosty-transparent-blurry-background seen in their most recent iOS and macOS interfaces. You’ll have noticed it in the Dock,the Contextual Menu, and the Notification centre.
     

Having Fun With Tomorrow’s CSS: Backdrop Filters

Having Fun With Tomorrow’s CSS: Backdrop Filters-1-技术控-currently,property,familiar,included,noticed
     Backdrop filter is currently baked under CSS Filters Level 2, and at the time of the writing, it only works work in Safari 9 with the  -webkit- prefix, and Chrome as well as Opera by enabling the “Experimental Web Platform Features” under the  chrome://flags menu.
     

Having Fun With Tomorrow’s CSS: Backdrop Filters

Having Fun With Tomorrow’s CSS: Backdrop Filters-2-技术控-currently,property,familiar,included,noticed
    Using Backdrop Filter

   Backdrop-filter is included as part of the CSS Filters specification and thus, similarly to the filter property, it inherits all the familiar filtering functions such  grayscale() ,  blur() , and  sepia() . The only difference here is that it will affect the elements underneath the selected element.
  To use it, we will need at least two elements; the first element will be the target, while the second will lie underneath it. For example:
  1. <div id="first-element" class="filter"></div>
  2. <a id="second-element" href="https://example.com"><img src="images/name.jpg" alt=""></a>
复制代码
  Set a background in the first element with low opacity so we can add backdrop-filter with a function, for example,  grayscale() to turn the backdrop black and white.
  1. .filter {
  2.     -webkit-backdrop-filter: blur(10px); /* Safari 9+ */
  3.     backdrop-filter: blur(10px); /* Chrome and Opera */
  4.     background-color: rgba(255, 255, 255, 0.3);
  5. }
复制代码
  Notice in the following demo that descendants of the .filter elements (a button in this case) remain crisp despite the  blur() function addition. This is an advantage  backdrop-filter has over the  filter property; where all descendants are impacted.
   Note: image here taken from tutorial  How to Create a Little Red Riding Hood Inspired Fairytale Illustration in Paint Tool SAI .
  Combining Multiple Filters

   Combining two filter functions in a single declaration is also acceptable. For example, keeping our blur() , but adding the  grayscale() function to strip the backdrop of color.
  1. .filter {
  2.     -webkit-backdrop-filter: grayscale() blur(10px); /* Safari 9+ */
  3.     backdrop-filter: grayscale() blur(10px); /* Chrome and Opera */
  4.     background-color: rgba(255, 255, 255, 0.3);
  5. }
复制代码
Other functions you can add into the mix include:
  
       
  • brightness() : accepting a number from 0 to infinity, or percentage. Any number below  1 , e.g.  0.8 , will darken the element, and specifying any number above  1  will lighten the element.   
  • contrast() : accepts a number and percentage which works similar to the  brightness()  function, except this function defines the element contrast.   
  • invert() : this function generates the opposite or negative colors of the backdrop. Likewise, the function also accepts a number and percentage as the value.  
  Advanced Filter With SVG

   If you can’t find the desired effect from these default functions, you can always add a more advanced filter through SVG. Create an SVG filter with a unique ID, and save it in an .svg file:
  1. <filter id="morph">
  2. <feMorphology id="morph-value" operator="erode" in="SourceGraphic" radius="3"/>
  3. </filter>
复制代码
  Refer the filter with the url() function, like so.
  1. .filter {
  2.     -webkit-backdrop-filter: url(../images/filter.svg#morph); /* Safari 9 */
  3.     backdrop-filter: url(../images/filter.svg#morph); /* Chrome and Opera */
  4. }
复制代码
  Unfortunately, adding filter with the url() function does not seem to work in any browser at the persent time. But once properly implemented, the image should look like this.
     

Having Fun With Tomorrow’s CSS: Backdrop Filters

Having Fun With Tomorrow’s CSS: Backdrop Filters-3-技术控-currently,property,familiar,included,noticed
      Custom filter with SVG.      What to Build?

  There are a number of practical implementations on the web where we could apply backdrop filters in a more meaningful way than just being a garnish. One good example is Medium, which blurs the post image cover as well as images within the post content before the image is fully loaded.
     

Having Fun With Tomorrow’s CSS: Backdrop Filters

Having Fun With Tomorrow’s CSS: Backdrop Filters-4-技术控-currently,property,familiar,included,noticed
      Blurry Image in Medium.com      Another great real-world example is Facebook, which blurs images with mature, inappropriate, or disturbing content, before users have consented to viewing the image in full.
     

Having Fun With Tomorrow’s CSS: Backdrop Filters

Having Fun With Tomorrow’s CSS: Backdrop Filters-5-技术控-currently,property,familiar,included,noticed
       Facebook blurring a violent image following their    Facebook Community Standard guideline.       Conclusion

  Over the years, CSS has been enhanced with new specifications that enable designers to build more compelling interfaces on the web. Transforms, transitions, and animations have all played their part, and now Backdrop Filters are coming our way too.
   Although it maybe too early to use backdrop-filter in production, these demos show that we will soon be able to apply filter effects to elements in a more simplified way and ultimately less reliant on heavy JavaScript or Canvas libraries. Until then, follow the Webkit Blog for updates on this and other projects.
  Further Resources

  
       
  • Backdrop Filter Draft : W3C Draft   
  • Backdrop Filter Browser Support     
  • Backdrop Filter Pens  on CodePen  
道听途说 发表于 2016-10-6 10:09:13
来,来,干了这杯翔!
回复 支持 反对

使用道具 举报

bw344 发表于 2016-10-7 02:40:09
走,MM,咱们化蝶去……  
回复 支持 反对

使用道具 举报

zyxin 发表于 2016-10-8 07:51:55
冷艳高贵接地气
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表