简体   繁体   English

除了我刚刚单击的文本字段,如何使整个页面模糊?

[英]How to blur the whole page except the text field I just clicked?

I have a text field top of my website. 我在网站顶部有一个文本字段。 Now when someone focus on that text field, except that text field everything else available on body will be blur. 现在,当某人专注于该文本字段时,除了该文本字段外,身体上其他所有可用内容都将变得模糊。 How to do that? 怎么做? Thanks. 谢谢。

This is easy with jquery: 这对于jquery很简单:

 $(document).ready(function(){ $('#mytext').focus(function(){ $('body').find('*').not('#mytext').addClass('blur'); }); $('#mytext').blur(function(){ $('body').find('*').not('#mytext').removeClass('blur'); }); }); 
 li{ background: red; } .blur { filter: blur(10px); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="mytext"> <ul> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li>lorem</li> </ul> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis quaerat fugiat culpa animi voluptas at explicabo labore consectetur dignissimos tempora. Libero praesentium quibusdam tempora molestiae nesciunt, unde, molestias quam? Commodi.</p> 

CSS does offer a filter: blur(...) option, but it's experimental and subject to change. CSS确实提供了filter: blur(...)选项,但是它是实验性的,可能会发生变化。 Here's a blog post with example . 这是一个带有example博客文章

Note that once a parent element is blurred, a child element can't be un-blurred. 请注意,父元素一旦模糊,子元素就无法取消模糊。 So you'd have to apply the blur class to all of the elements that don't include your text box as a child. 因此,您必须将模糊类应用于所有不包含儿童文本框的元素。 This could be tricky to get right. 这可能很难解决。

A more common option on the web is to put a darkened overlay, for example a full-page element with background-color: rgba(42,42,42,0.7); 在网络上,更常见的选择是放置一个深色的叠加层,例如带有background-color: rgba(42,42,42,0.7);的整页元素background-color: rgba(42,42,42,0.7); on top of your page, and use z-index or similar to get your input element to pop on top of that. 在页面顶部,然后使用z-index或类似符号将您的输入元素弹出。 This is generally simpler and more widely-supported than selective blurring. 与选择性模糊相比,这通常更简单并且得到广泛支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM