【问题标题】:Width and Height 100% doesn't work on pop up image宽度和高度 100% 不适用于弹出图像
【发布时间】:2020-03-16 02:23:29
【问题描述】:

我有一个与我联系的按钮,但是,每当有人将鼠标悬停在该按钮上时,我希望出现一个全屏图像背景。但是,此图像不是全屏的。

a {
  text-decoration: none;
  color: white;
  /*Let's Chat colour*/
}

.Oval {
  position: relative;
  font-family: 'Open Sans', sans-serif;
  top: 510px;
  text-align: center;
  font-size: 25px;
  text-color: white;
  left: 30%;
  z-index: 75;
  background-color: orange;
  width: 140px;
  height: auto;
  background: #162129;
  border-radius: 40px;
  padding: 0px;
  text-decoration: none;
}

.Oval:hover {
  position: relative;
}

.Oval:hover:after {
  content: url(Lightning1.JPG);
  display: block;
  position: absolute;
  center: 100%;
  top: -510px;
  opacity: 0.5;
  z-index: -10;
  height: 100%;
  width: 100%;
}
<div class="Oval"><a href="mailto:">Let's Chat</a></div>

【问题讨论】:

标签: html css image frontend visual-web-developer


【解决方案1】:

您需要在.Oval:hover:after 上添加pointer-events: none;+z-index: -1; 属性,类似于下面的sn-p。

希望下面的sn-p能帮到你。

a {
  text-decoration: none;
  color: white;
}
.Oval {
  position: relative;
  font-family: 'Open Sans', sans-serif;
  top: 100px;
  text-align: center;
  font-size: 25px;
  color: white;
  left: 50%;
  margin-left: -70px;
  background-color: orange;
  width: 140px;
  height: auto;
  background: #162129;
  border-radius: 40px;
  padding: 0px;
  text-decoration: none;
}
.Oval:hover:after {
  content:'';
  background: url(https://i.imgur.com/QgfTfOU.jpg);
  background-size: cover;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  height: 100%;
  width: 100%;
  pointer-events: none;
}
<div class="Oval"><a href="mailto:">Let's Chat</a></div>

【讨论】:

    【解决方案2】:

    CSS 属性position:absolute 确切地说:

    相对于其最近的祖先定位

    1. 如果你想让背景图片占据所有屏幕空间, 那么它可以通过CSS属性position:fixed来实现 使用它,您可以将其放置在相对于视口的位置。 然后将顶部、底部、左侧、右侧值精确到0 以获得 元素到所有视口。

    例如:

    a {
      text-decoration: none;
      color: white;
      /*Let's Chat colour*/
    }
    
    .Oval {
      position: relative;
      font-family: 'Open Sans', sans-serif;
      top: 510px;
      text-align: center;
      font-size: 25px;
      text-color: white;
      left: 30%;
      z-index: 75;
      background-color: orange;
      width: 140px;
      height: auto;
      background: #162129;
      border-radius: 40px;
      padding: 0px;
      text-decoration: none;
    }
    
    .Oval:hover {
      position: relative;
    }
    
    /* Original
    .Oval:hover:after {
      content: url(Lightning1.JPG);
      display: block;
      position: absolute;
      center: 100%;
      top: -510px;
      opacity: 0.5;
      z-index: -10;
      height: 100%;
      width: 100%;
    }
    */
    
    .Oval:hover::after {
    
        content: url(https://i.imgur.com/QgfTfOU.jpg);
        display: block;
        position: fixed;
        opacity: 0.5;
        z-index: -10;
        top: 0;
        border: 0;
        left: 0;
        right: 0;
    
    }
    <div class="Oval"><a href="mailto:">Let's Chat</a></div>


    1. 如果你只想在div.Oval上使用背景图片,那么你应该使用CSS属性position:absolute

    a {
      text-decoration: none;
      color: white;
      /*Let's Chat colour*/
    }
    
    .Oval {
      position: relative;
      font-family: 'Open Sans', sans-serif;
      top: 510px;
      text-align: center;
      font-size: 25px;
      text-color: white;
      left: 30%;
      z-index: 75;
      background-color: orange;
      width: 140px;
      height: auto;
      background: #162129;
      border-radius: 40px;
      padding: 0px;
      text-decoration: none;
      clip-path: border-box; /* Added */
      overflow: hidden; /* Added */
    }
    
    /* Original
    .Oval:hover {
      position: relative;
    }
    */
    
    /* Original
    .Oval:hover:after {
      content: url(Lightning1.JPG);
      display: block;
      position: absolute;
      center: 100%;
      top: -510px;
      opacity: 0.5;
      z-index: -10;
      height: 100%;
      width: 100%;
    }
    */
    
    /* Added */
    .Oval:hover::after {
      content: '';
      background: url(https://i.imgur.com/QgfTfOU.jpg);
      background-size: auto;
      background-size: cover;
      display: block;
      position: absolute;
      opacity: 0.5;
      z-index: -10;
      top: 0;
      border: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    <div class="Oval"><a href="mailto:">Let's Chat</a></div>

    【讨论】:

      【解决方案3】:

      a {
        text-decoration: none;
        color: white;
        /*Let's Chat colour*/
      }
      
      .Oval {
        position: relative;
        font-family: 'Open Sans', sans-serif;
        top: 510px;
        text-align: center;
        font-size: 25px;
        text-color: white;
        left: 30%;
        z-index: 75;
        background-color: orange;
        width: 140px;
        height: auto;
        background: #162129;
        border-radius: 40px;
        padding: 0px;
        text-decoration: none;
      }
      
      .Oval:hover {
        position: inherit;
        left:0;
      }
      .Oval:hover a{display:none;}
      
      .Oval:hover:after {
        content:'';
        background: url(https://i.imgur.com/QgfTfOU.jpg);
        background-size:cover;
        display: block;
        position: absolute;
        center: 100%;
        top:0;
        left:0;
        opacity: 0.5;
        z-index: 1;
        height: 100%;
        width: 100%;
      }
      <div class="Oval"><a href="mailto:">Let's Chat</a></div>

      【讨论】:

      • 谢谢,但我怎样才能让我们聊天椭圆在悬停后不消失?
      • 你期望的截图分享
      • 当我将鼠标悬停在 Let's Chat 上时,我希望背景出现,但我也不希望该按钮消失。无法截图,因为我不知道如何使它工作。抱歉编辑:当您运行代码并将鼠标悬停在按钮上时,背景会出现并且按钮会消失。但是,我希望两者都出现。
      • 嗯...删除第一个.Oval:hover a{display:none;}
      • 做到了,但我也不希望按钮是透明的,我希望它保持在原来的位置,并且希望一旦我停止悬停,背景图像就会消失按钮。
      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多