【问题标题】:Change background image on link hover pure CSS更改链接悬停纯 CSS 上的背景图像
【发布时间】:2019-09-14 03:58:31
【问题描述】:

我正在尝试在我的网站上获得这种效果。例如:https://rno1.com/

(向下滚动到“我们专注于”)

我对 HTML 和 CSS 非常陌生,而且我正在研究 WordPress,这使得它有点困难。 开始在 StackOverflow 上用这个问题编写代码:

Change body background image on hover with pure html/css

但我想在交换图像之间添加一个过渡,我还需要在哪里更改字体属性?

div.link>a {
  displya: inline-block !important;
  position: relative !important;
  z-index: 100;
}

.bg1:hover+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
}

.bg2:hover+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
}

.background {
  background: transparent;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
<div class=container>
  <div class="link">
    <a href="#" class="bg1">bg1</a>
    <div class=background></div>
    <a href="#" class="bg2">bg2</a>
    <div class=background>
    </div>
  </div>
</div>

【问题讨论】:

    标签: html css image hover


    【解决方案1】:

    您可以像下面这样添加一些不透明度过渡:

    div.link>a {
      display: inline-block;
      position: relative;
      z-index: 100;
    }
    
    .bg1+.background {
      background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
    }
    .bg1:hover +.background {
      opacity:1;
    }
    
    .bg2+.background {
      background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
    }
    .bg2:hover +.background {
      opacity:1;
    }
    
    .background {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      transition:1s all;
      opacity:0;
    }
    <div class=container>
      <div class="link">
        <a href="#" class="bg1">bg1</a>
        <div class=background></div>
        <a href="#" class="bg2">bg2</a>
        <div class=background>
        </div>
      </div>
    </div>

    您还可以像下面这样优化您的代码:

    div.link>a {
      display: inline-block;
    }
    
    .bg1:before {
      background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
    }
    .bg1:hover:before {
      opacity:1;
    }
    
    .bg2:before {
      background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
    }
    .bg2:hover:before {
      opacity:1;
    }
    
    .bg:before {
      content:"";
      position: fixed;
      z-index:-1;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      transition:1s all;
      opacity:0;
    }
    <div class=container>
      <div class="link">
        <a href="#" class="bg1 bg">bg1</a>
        <a href="#" class="bg2 bg">bg2</a>
      </div>
    </div>

    【讨论】:

    • 嘿,非常感谢!它真的有帮助!但你能再给我解释一下吗?我想要一些其他的悬停效果可能只在我发布的示例页面之类的文本上。像增长......还有不在页面中间的图像,试图改变z-index,但它破坏了效果。最后一件事(真的打扰你了:S)如果我将它粘贴到 wordpress 页面的部分中,它会改变整个页面的背景还是只改变部分的背景?
    • @BenZikri 1) 如果你想对文本产生一些影响,考虑变换(类似:transform:translateX(10px))(2)调整图像,考虑背景大小和背景位置能够将图像放置在您想要的位置 (3) 全部取决于代码,正如您在第一个代码中看到的那样,div 设置为 position:absolute 所以如果您需要它们仅覆盖特定部分,请添加 position:relative 到该部分(例如:jsfiddle.net/t2b57s4r
    • 谢谢!!您所有的编码工作都很棒!我可以再问 1 件事吗?如果我想更改悬停时的文本(或实际添加图标或类似示例的内容,我该怎么做?我知道我会拉伸它:D
    • @BenZikri 例如,您可以从此处选择一个图标:fontawesome.com/start 并将其与文本一起添加,如果您想更改文本,请查看transform,您将能够在悬停时为文本设置动画
    • 是的,我想结合两件事,一是链接悬停时的背景变化。其次,链接本身应该增长并改变为其他东西。例如:“建筑可视化”到“建筑可视化>>”我正在尝试结合我在论坛上找到的另一个小提琴但没有成功:jsfiddle.net/pq3x0qvc/2
    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2013-02-09
    • 2011-06-10
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    相关资源
    最近更新 更多