html - 如何使用 jQuery 在 body 中而不是在特定的 div 中产生效果

标签 html css blur css-filters

我有一个页面,在这个页面中,我有切换菜单。当我打开菜单时,整个 body 变得模糊,但我不希望菜单变得模糊。

我试过:

$(document).ready(function() {
  $("button").click(function() {
    $("body").toggleClass("blur-body")
    $(".menu-ul").slideToggle("slow")
  });
});
.blur-body {
  -webkit-filter: blur(5px);
  filter: blur(5px);
}
.menu-ul {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<p>This is a paragraph.</p>
<span>This is a another paragraph.</span>
<br/>
<br/>
<button>Menu</button>
<ul class="menu-ul">
  <li>1</li>
  <li>11</li>
  <li>111</li>
  <li>1111</li>
  <li>11111</li>
</ul>

Note: This is only a demo. The full website has a lot more content compared to this.

最佳答案

When I open menu, Whole body gets, blured but, I dont want menu Blur.

根据上述说法,我的理解是您希望页面(或正文)除菜单以外的所有部分都被模糊化。为此,您可以使用 not 选择器,然后仅将模糊应用于没有 class='menu-ul' 的子元素。这意味着菜单本身不会模糊。

$(document).ready(function() {
  $("button").click(function() {
    $("body").toggleClass("blur-body")
    $(".menu-ul").slideToggle("slow")
  });
});
.blur-body >:not(.menu-ul) {
  -webkit-filter: blur(5px);
  filter: blur(5px);
}
.menu-ul {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<p>This is a paragraph.</p>
<span>This is a another paragraph.</span>
<div>Some extra content to mimick a page.
  <div>Some other extra content</div>
</div>
<br/>
<br/>
<button>Menu</button>
<ul class="menu-ul">
  <li>1</li>
  <li>11</li>
  <li>111</li>
  <li>1111</li>
  <li>11111</li>
</ul>


如果您也希望菜单按钮不模糊,则向 button 元素添加一个额外的类,如下面的代码片段所示,并使用属性选择器和 :not 而不是类选择器。

$(document).ready(function() {
  $("button").click(function() {
    $("body").toggleClass("blur-body")
    $(".menu-ul").slideToggle("slow")
  });
});
.blur-body > *:not([class^='menu-']) {
  -webkit-filter: blur(5px);
  filter: blur(5px);
}
.menu-ul {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<p>This is a paragraph.</p>
<span>This is a another paragraph.</span>
<div>Some extra content to mimick a page.
  <div>Some other extra content</div>
</div>
<br/>
<br/>
<button class='menu-btn'>Menu</button>
<ul class="menu-ul">
  <li>1</li>
  <li>11</li>
  <li>111</li>
  <li>1111</li>
  <li>11111</li>
</ul>

关于html - 如何使用 jQuery 在 body 中而不是在特定的 div 中产生效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523739/

相关文章:

Android - 选择性地关注位图

php - WordPress - 上传时模糊图像

html - 有没有办法将每行的高度固定为 10%?

css 动画比例不适用于 chrome

php - 带有进度条错误的 HTML5 AJAX 文件 uploader php

css - 背景颜色在 IE7 中不起作用

html - 如何使表列可滚动并在 Angular 2 中卡住多列?

.net - 模糊 WPF 容器的背景

javascript - 使一个(图像)向右移动并旋转 45 度,同时将鼠标悬停在另一个(图像)上

javascript - 使用 jQuery 按 id 更新占位符