简体   繁体   English

悬停按钮Css时在页面上显示背景图像

[英]Display background-image on page when hovering button Css

I've been trying to set the background image of my outer container by having the image transition into the background of my website. 我一直在尝试通过将图像过渡到网站背景中来设置外部容器的背景图像。 But nothing displays, is there something im missing in the css that would allow me to display the background image? 但是什么也没显示,css中是否缺少我可以显示背景图像的东西?

Pen: https://codepen.io/chriskaram/pen/BwVXGq Page: https://mydietgoal.com/mydietgoal-features-and-plans/ 笔: https : //codepen.io/chriskaram/pen/BwVXGq页面: https : //mydietgoal.com/mydietgoal-features-and-plans/

<div class="outer-container" id="backgroundDiv" style="width: max-content; height: max-content;">

  <div class="container container1">
        <div class="w-table">
        <div class="w-table-cell">
          <div class="w-container">
            <div class="w-card color-green">

                        <div class="card-header">
                <div class="w-title" style="cursor:default">
                  <h2><font size="7px">Novice</font></h2>
                </div>
                <div class="w-price" style="cursor:default">
                  $4.99
                  <h3>/ Week</h3>
                </div>
                <div class="container-button">
                  <a class="w-button" id="btn1" href="/meal-planner" target="_blank">Sign up </a>
                </div>
              </div>

              <div class="card-content" style="cursor:default">
                <div class="w-item" style="cursor:default">
                  <span>Weekly </span>Meal Plans
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Personal </span>Dietary Assessment and Advice
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Full </span>Access to the <a href="/mydietgoal-dietary-catalogue">Food Catalogue</a>
                </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
  </div>

    <div class="container container2">
        <div class="w-table">
        <div class="w-table-cell">
          <div class="w-container">
            <div class="w-card color-blue">

              <div class="card-header">
                <div class="w-title" style="cursor:default">
                  <h2><font size="7px">Novice</font></h2>
                </div>
                <div class="w-price" style="cursor:default">
                  $9.99
                  <h3>/ Week</h3>
                </div>
                <div class="container-button">
                  <a class="w-button" href="/meal-planner" target="_blank">Sign up </a>
                </div>
              </div>

              <div class="card-content" style="cursor:default">
                <div class="w-item" style="cursor:default">
                  <span>Weekly </span>Meal Plans
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Personal </span>Dietary Assessment and Advice
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Full </span>Access to the <a href="/mydietgoal-dietary-catalogue">Food Catalogue</a>
                </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="container container3">
        <div class="w-table">
            <div class="w-table-cell">
          <div class="w-container">
            <div class="w-card color-orange">

              <div class="card-header">
                <div class="w-title" style="cursor:default">
                  <h2><font size="7px">Novice</font></h2>
                </div>
                <div class="w-price" style="cursor:default">
                  $4.99
                  <h3>/ Week</h3>
                </div>
                <div class="container-button">
                  <a class="w-button" href="/meal-planner" target="_blank">Sign up </a>
                </div>
              </div>

              <div class="card-content" style="cursor:default">
                <div class="w-item" style="cursor:default">
                  <span>Weekly </span>Meal Plans
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Personal </span>Dietary Assessment and Advice
                </div>
                <div class="w-item" style="cursor:default">
                  <span>Full </span>Access to the <a href="/mydietgoal-dietary-catalogue">Food Catalogue</a>
                </div>
              </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

This block of code 此代码块

#btn1:hover + .outer-container {
    background-image: url(https://static1.squarespace.com/static/59a7820e2994ca11766093d3/t/59dbd720f5e2317170edb5bf/1507579681913/vegetables-fresh-healthy-food-my-diet-goal-hd.jpg);
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

means when #btn is :hover , the next direct sibling with .outer-container gets background-image . 意味着当#btn:hover下一个.outer-container 直接同级将获得background-image

However, according to your code, .outer-container is the grand grand grand grand grand grand grand grandparent of #btn . 然而,根据你的代码, .outer-container是隆重盛大隆重盛大隆重盛大隆重的祖父母#btn So it will not work. 因此它将不起作用。

Bad news is, CSS has no way to select ancestors. 坏消息是,CSS无法选择祖先。

So I suggest you use JavaScript to do it. 因此,我建议您使用JavaScript来实现。

Working Example 工作实例

CSS only allows descendant and sibling selectors, it does not support applying styles to a parent. CSS仅允许后代和兄弟选择器,它不支持将样式应用于父项。 https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors https://developer.mozilla.org/zh-CN/docs/Web/CSS/Adjacent_sibling_selectors

To achieve this effect, you would want a div at the same level as the button with the hover trigger that is positioned, fixed or absolute (you will need to change your markup to support absolute) 要实现此效果,您需要将div与具有定位,固定或绝对位置的悬停触发器的按钮处于同一级别(您需要更改标记以支持绝对值)

Here is an example: 这是一个例子:

.bg,
.bg2 {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  height: 100%;
  z-index: -1;
}

#btn1:hover + .bg,
.bg2 {
  background-image: url(https://static1.squarespace.com/static/59a7820e2994ca11766093d3/t/59dbd720f5e2317170edb5bf/1507579681913/vegetables-fresh-healthy-food-my-diet-goal-hd.jpg);
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  transition: opacity 0.5s;
}

.bg2 {
  opacity: 0;
}

#btn2:hover ~ .bg2 {
  opacity: 1;
}

I also added an opacity transform for the second button as I feel it is less jarring. 我还为第二个按钮添加了不透明度转换,因为我感觉它不那么刺耳。

https://codepen.io/jaylandro/pen/qPypmb https://codepen.io/jaylandro/pen/qPypmb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM