模糊整个 HTML 当子菜单打开时

huangapple go评论70阅读模式
英文:

Blur the entire html when the sub-menu is open

问题

我使用HTML和CSS创建了一个子菜单。当鼠标悬停在导航列表元素上时,子菜单的元素会打开。我需要帮助解决如何在导航列表悬停和子菜单打开时模糊HTML的主内容。打开时,子菜单下方会有一张图片以及多张图片和类。当前的代码确实模糊了HTML,但问题是,为了模糊整个HTML,我需要逐个将子菜单下方的每个类添加到CSS中。

/* 开始代码片段: js 隐藏: false 控制台: true Babel: false */

/* CSS语言: lang-css */
nav {
  display: inline-flex;
  width: 100%;
}

.nav-list {
  display: flex;
  width: 100%;
  margin-top: .7rem;
  padding-left: 1.1rem;
}

.nav-list li {
  position: relative;
}

.nav-list>li>a {
  color: black;
  display: block;
  font-size: 1rem;
  padding: 1.3rem 1rem;
  text-transform: uppercase;
}

.sub-menu {
  display: flex;
  position: absolute;
  box-sizing: border-box;
  background-color: black;
  visibility: hidden;
  top: 3.89rem;
  left: -4rem;
  width: 82.5rem;
  height: 35rem;
  z-index: 5000;
}

.sub-menu a {
  position: relative;
  top: 2rem;
  color: white;
  font-size: 1.1rem;
  font-weight: 200;
  padding: 3rem 40px 0 40px;
}

.nav-list>li:hover .sub-menu {
  visibility: visible;
}

.nav-list>li:hover>a::after {
  width: 100%;
}

/* 模糊效果的CSS */

.blurred::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.8);
  filter: blur(5px);
  /* 调整模糊程度 */
  z-index: 900;
  visibility: hidden;
}
<!-- CSS语言: lang-html -->
<div class="main" id="navbar">
  <div class="logo">
    <a href="">XYZ<br>123</a>
  </div>
  <nav class="menu">
    <ul class="nav-list">
      <li>
        <a href="">Men</a>
        <ul class="sub-menu">
          <li><a href="#">shirts</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</div>

<div class="main">
  <img></img>
  <img></img>
  <img></img>
  <img></img>
</div>

<footer>
  1
</footer>

<script>
  var subMenu = document.querySelector('.sub-menu');
  var main = document.querySelectorAll('main');
  subMenu.addEventListener('mouseover', function() {
    main.forEach(function(elem) {
      elem.classList.add('blurred');
    });
  });
  subMenu.addEventListener('mouseout', function() {
    main.forEach(function(elem) {
      elem.classList.remove('blurred');
    });
  });
</script>

英文:

I created a submenu using HTML and CSS. The elements of the submenu open when hovered on the elements of nav-list. I need help figuring out how to blur the HTML's main content when the nav-list hovers and sub-menu is open. When open, the submenu will have a picture below it and multiple pictures and classes further down. The current code does blur the HTML, but the problem is, to blur the entire HTML, I need to add each class below the submenu to CSS individually.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
padding-left: 1.1rem;
}
.nav-list li {
position: relative;
}
.nav-list&gt;li&gt;a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.nav-list&gt;li:hover .sub-menu {
visibility: visible;
}
.nav-list&gt;li:hover&gt;a::after {
width: 100%;
}
/* CSS for blur effect */
.blurred::before {
content: &quot;&quot;;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
filter: blur(5px);
/* adjust the blur amount */
z-index: 900;
visibility: hidden;
}

<!-- language: lang-html -->

&lt;div class=&quot;main&quot; id=&quot;navbar&quot;&gt;
&lt;div class=&quot;logo&quot;&gt;
&lt;a href=&quot;&quot;&gt;XYZ&lt;br&gt;123&lt;/a&gt;
&lt;/div&gt;
&lt;nav class=&quot;menu&quot;&gt;
&lt;ul class=&quot;nav-list&quot;&gt;
&lt;li&gt;
&lt;a href=&quot;&quot;&gt;Men&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;shirts&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;div class=&quot;main&quot;&gt;
&lt;img&gt;&lt;/img&gt;
&lt;img&gt;&lt;/img&gt;
&lt;img&gt;&lt;/img&gt;
&lt;img&gt;&lt;/img&gt;
&lt;/div&gt;
&lt;footer&gt;
1
&lt;/footer&gt;
&lt;script&gt;
var subMenu = document.querySelector(&#39;.sub-menu&#39;);
var main = document.querySelectorAll(&#39;main&#39;);
subMenu.addEventListener(&#39;mouseover&#39;, function() {
main.forEach(function(elem) {
elem.classList.add(&#39;blurred&#39;);
});
});
subMenu.addEventListener(&#39;mouseout&#39;, function() {
main.forEach(function(elem) {
elem.classList.remove(&#39;blurred&#39;);
});
});
&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

你只需要将"blurred"类添加到主容器div上,所有内容都将在其中模糊。

鼠标悬停在汉堡包上的模糊演示(您可以更改为仅在子菜单悬停时进行模糊处理)。鼠标移出后有2秒的延迟,这样您就有时间向下滚动以查看模糊图像:

<div class="main" id="navbar">
  NAVBAR 鼠标悬停以模糊 ==&gt; &nbsp; <span id="hamburger">☰</span>
  <hr />
</div>

<div class="main" id="mainContainer">
  <h2>Lorem Ipsum</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam iaculis blandit felis vel hendrerit. Morbi nunc augue, pellentesque eu laoreet in, bibendum nec sem.</p>
  <ul><li>在欧洲自由贸易区,库存持有零售商和网络的儿童母婴用品的现货供货商
一家领先的国际贸易公司。公司携 dizem ter saído da conversa lançou uma grande variedade de produtos com desconto (LOJA DE

const hamburger = document.getElementById('hamburger');
const main = document.getElementById('mainContainer');
hamburger.addEventListener('mouseover', function() {
  main.classList.add('blurred');
});
hamburger.addEventListener('mouseout', function() {
  setTimeout(() => main.classList.remove('blurred'), 2000);
});
.blurred {
    -webkit-filter: blur(5px);
    -moz-filter: blur(5px);
    -o-filter: blur(5px);
    -ms-filter: blur(5px);
    filter: blur(5px);
    background-color: #eee;
}

请注意,上述示例是HTML、JavaScript和CSS的代码示例,用于在鼠标悬停在"汉堡包"图标上时添加模糊效果到主容器中的内容。

英文:

You only need to add the blurred class to the main container div, all content will be blurred in it.

Demo for blur on hover over hamburger (you can change that to blur only on sub-menu hover). There is a 2 sec delay on mouse-out to that you have time to scroll down to see the blurred image:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const hamburger = document.getElementById(&#39;hamburger&#39;);
const main = document.getElementById(&#39;mainContainer&#39;);
hamburger.addEventListener(&#39;mouseover&#39;, function() {
main.classList.add(&#39;blurred&#39;);
});
hamburger.addEventListener(&#39;mouseout&#39;, function() {
setTimeout(() =&gt; main.classList.remove(&#39;blurred&#39;), 2000);
});

<!-- language: lang-css -->

.blurred {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
background-color: #eee;
}

<!-- language: lang-html -->

&lt;div class=&quot;main&quot; id=&quot;navbar&quot;&gt;
NAVBAR mouse over to blur ==&gt; &amp;nbsp; &lt;span id=&quot;hamburger&quot;&gt;☰&lt;/span&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div class=&quot;main&quot; id=&quot;mainContainer&quot;&gt;
&lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam iaculis blandit felis vel hendrerit. Morbi nunc augue, pellentesque eu laoreet in, bibendum nec sem.&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;In eu libero eget nulla laoreet aliquet in quis augue.&lt;/li&gt;&lt;li&gt;Praesent vulputate leo tellus, quis eleifend purus viverra vitae.&lt;/li&gt;&lt;li&gt;Phasellus eget augue venenatis, facilisis nulla sed, consequat lorem.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Duis sapien diam, dignissim quis imperdiet eget, hendrerit nec nulla. Nam nisi purus, ultrices nec fringilla vitae, interdum vitae nunc.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://via.placeholder.com/500x90/000000/FFFFFF.png?text=This+is+an+embedded+image&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Integer lobortis tellus a orci ultrices, at varius risus fermentum. Maecenas maximus elit sit amet varius facilisis. Morbi molestie rutrum eros, id molestie dui maximus in. Etiam fermentum felis eu vehicula euismod.&lt;/p&gt;
&lt;/div&gt;
&lt;footer&gt;
&lt;hr /&gt;
FOOTER
&lt;/footer&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 22:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定