【问题标题】:Background img blur - css背景图像模糊 - css
【发布时间】:2020-12-27 15:15:09
【问题描述】:

亲爱的开发者你好,

我想在悬停时模糊背景图像。但它不起作用。有谁能够帮我?我尝试了很多方法。 这是一个框,我想模糊背景并显示带有悬停效果的文本。 当前,当鼠标悬停在框上时,会发生以下情况: 任何一个: 背景变得模糊,但文字没有出现 或者: 文字出现,但背景不会模糊

HTML

<div class="kursContainer">
        <div class="BodyKurs">
                <h1>Body</h1>
                <picture>
                    <img src="../Kurse/img/body.webp" alt="zumba">
                </picture>
            <div class="overlay">
                <h2>Body</h2>
                <h3>Abnehmen - 6 Wochen</h3>
                <h4>Lorem ipsum dolor sit amet, consectetur adipiscing     elit</h4>
            </div>
        </div>

CSS

.kursContainer {
  display: grid;
  justify-content: space-evenly;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em;
  justify-items: center;
  width: 122vmin;
  margin: 2vmin auto;
  align-items: center;

  grid-template-areas:
    "body body fatburn"
    "body body zumba"
    "yoga sixpack sixpack"
    "step sixpack sixpack"
    "backwork backwork tourde"
    "backwork backwork booty";
 }
 .BodyKurs{
   position: relative;
   width: 80vmin;
   height: 80vmin;
   border-radius: 10px;
   background-color: var(--schwarz);
   transition: transform 0.2s;
 }

.BodyKurs img{
   width: 80vmin;
   border-radius: 10px;
}
 .BodyKurs h1 {
   position: absolute;
   text-align: start;
   padding-left: 3vmin;
   bottom: 1vmin;
   color: var(--hellGrau);
   font-size: 8vmin;
 }

 .BodyKurs:hover {
    box-shadow: 0 0px 4.6px rgba(0, 0, 0, 0.278),
     0 0px 15.4px rgba(0, 0, 0, 0.411), 0 0px 69px rgba(0, 0, 0, 0.69);
   transform: scale(1.05);
   z-index: 2;
   cursor: pointer;
 }

  .overlay {
  width: 80vmin;
  height: 80vmin;
  position: absolute;
  text-align: center;
  bottom: 0;
  border-radius: 10px;
  color: var(--hellGrau);
  transition: 0.3s all;
  /* background: rgba(0, 0, 0, 0.2); */
  visibility: hidden;
   cursor: pointer;
  z-index: 5;
}
 .overlay:hover {
  visibility: visible;
}

picture:hover {
  filter: blur(5px);
}

【问题讨论】:

标签: html css hover


【解决方案1】:

你可以使用:not(:hover)风格

示例 1:

.wrap:hover img,
.wrap:hover img:not(:hover) {
    filter: blur(3px);
}

.wrap {
    width: 300px;
}

span {
    position: absolute;
    color: #ffffff;
    z-index: 1;
    text-align: center;
    width: 300px;
    display: none;
}

.wrap:hover span {
    display: block;
}
<div class='wrap'>
    <span>
        <h1>Lorem</h1>
        <p>Text</p>
    </span>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRK7bvAryKdY7JSlepTHatE1A4nAVU5lJXkdw&usqp=CAU"
        width="300px">
</div>

这个例子有点 CSS,但我认为它更容易理解。再次使用 :not(:hover) 但这次是在 span 元素上。

示例 2:

.wrap:hover img,
.wrap:hover span:not(:hover) {
    filter: blur(3px);
}

.wrap {
    position: relative;
    width: 300px;
    overflow: hidden;
}

span {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    color: #ffffff;
    z-index: 1;
    text-align: center;
    display: none;
}

.wrap:hover span {
    display: block;
}
<div class='wrap'>
    <span>
        <h1>Lorem</h1>
        <p>Text</p>
    </span>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRK7bvAryKdY7JSlepTHatE1A4nAVU5lJXkdw&usqp=CAU"
        width="300px">
</div>

【讨论】:

  • 很高兴我能帮上忙! :) 感谢您接受的答案,如果您愿意,可以对我的回答投赞成票。
【解决方案2】:

在img 标签的 CSS 中使用 filter: blur() 伪类 :hover:before。我在文本内容上使用了p 标签,并在这两个元素上也设置了z-index。有效地将元素堆叠在一起。

背景将继续:before,并确保将位置设置为绝对,内容设置为''(无)。高度和宽度设置为 100%,这样它就可以填满要堆叠的 div。

blur()

filter:

.image {
  height: 200px;
  width: 200px;
  display: flex;
  justify-content: center;
}
.image:before {
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background-image: url(https://image.shutterstock.com/image-photo/white-transparent-leaf-on-mirror-260nw-1029171697.jpg);
  z-index: -1;
}
.image:hover:before {
  -webkit-filter: blur(20px);
  filter: blur(20px);
}
.img {
  position: relative;
  width: 200px;
  height: 200px;
}
.image p {
  z-index: 9999999;
}
<div class="img">
  <div class="image">
    <p>Some dummy text here</p>
  </div>
</div>

【讨论】:

    【解决方案3】:
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body, html {
      height: 100%;
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    * {
      box-sizing: border-box;
    }
    
    .bg-image {
      /* The image used */
      background-image: url("photographer.jpg");
      
      /* Add the blur effect */
      filter: blur(8px);
      -webkit-filter: blur(8px);
      
      /* Full height */
      height: 100%; 
      
      /* Center and scale the image nicely */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    /* Position text in the middle of the page/image */
    .bg-text {
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */
      color: white;
      font-weight: bold;
      border: 3px solid #f1f1f1;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 2;
      width: 80%;
      padding: 20px;
      text-align: center;
    }
    </style>
    </head>
    <body>
    
    <div class="bg-image"></div>
    
    <div class="bg-text">
      <h2>Blurred Background</h2>
      <h1 style="font-size:50px">I am Sarvesh</h1>
      <p>Blurred image</p>
    </div>
    
    </body>
    </html>
    

    【讨论】:

      【解决方案4】:

      只需尝试从正文中删除图片标签,并简单地使用 CSS 背景图像属性。以 rgba 为单位设置悬停时覆盖的背景颜色,如下所示:

      <div class="kursContainer">
              <div class="BodyKurs">
                      <h1>Body</h1>
                  <div class="overlay">
                      <h2>Body</h2>
                      <h3>Abnehmen - 6 Wochen</h3>
                      <h4>Lorem ipsum dolor sit amet, consectetur adipiscing     elit</h4>
                  </div>
              </div>
      </div>
      

      CSS

      .kursContainer {
        display: grid;
        justify-content: space-evenly;
        grid-template-columns: repeat(3, 1fr);
        grid-gap: 1em;
        justify-items: center;
        width: 122vmin;
        margin: 2vmin auto;
        align-items: center;
      
        grid-template-areas:
          "body body fatburn"
          "body body zumba"
          "yoga sixpack sixpack"
          "step sixpack sixpack"
          "backwork backwork tourde"
          "backwork backwork booty";
       }
       .BodyKurs{
         position: relative;
         width: 80vmin;
         height: 80vmin;
         border-radius: 10px;
         background-color: var(--schwarz);
         transition: transform 0.2s;
         background-image: url("../Kurse/img/body.webp");
       }
      
      .BodyKurs img{
         width: 80vmin;
         border-radius: 10px;
      }
       .BodyKurs h1 {
         position: absolute;
         text-align: start;
         padding-left: 3vmin;
         bottom: 1vmin;
         color: var(--hellGrau);
         font-size: 8vmin;
       }
      
       .BodyKurs:hover {
          box-shadow: 0 0px 4.6px rgba(0, 0, 0, 0.278),
           0 0px 15.4px rgba(0, 0, 0, 0.411), 0 0px 69px rgba(0, 0, 0, 0.69);
         transform: scale(1.05);
         z-index: 2;
         cursor: pointer;
       }
      
        .overlay {
        width: 80vmin;
        height: 80vmin;
        position: absolute;
        text-align: center;
        bottom: 0;
        border-radius: 10px;
        color: var(--hellGrau);
        transition: 0.3s all;
        /* background: rgba(255, 255, 255, 0.3); */
         cursor: pointer;
        z-index: 5;
      }
       .overlay:hover {
        background-color: rgba(255, 255, 255, 0.5);
      }
      
      picture:hover {
        filter: blur(5px);
      }
      

      【讨论】:

      • 非常感谢,但这对我不起作用。我不知道为什么。
      猜你喜欢
      • 1970-01-01
      • 2021-06-08
      • 2020-07-13
      • 2023-01-13
      • 1970-01-01
      • 2013-10-22
      • 2014-09-02
      • 2018-12-17
      • 1970-01-01
      相关资源
      最近更新 更多