悬停+ div:显示无法正常工作

时间:2016-04-14 15:08:03

标签: html css hover display

所以当我只使用btn悬停在设计现在按钮上时,我试图显示一个图像:hover + img {display:block}仅使用css,但它不起作用我可以&真的弄明白为什么。



.ca-sub a {
    text-decoration: none;
}
.ca-sub p {
    color: black;
    background-color: white;
    margin-bottom: 0px;
    padding-top: 7px;
    padding-bottom: 11px;
    font-size: 1.2em;
}
.ca-menu {
    padding:0;
    /*margin:20px auto;*/
    width: 100%;
    margin-top: 0px;
    list-style-type: none;
}
.ca-menu li {
    width: 18.5%;
    height: 215px;
    overflow: hidden;
    position: relative;
    float:left;
    background: white;
    margin-right: 10px;
    margin-bottom: 15px;
    -webkit-transition: all 300ms linear;
    -moz-transition: all 300ms linear;
    -o-transition: all 300ms linear;
    -ms-transition: all 300ms linear;
    transition: all 300ms linear;
}
.ca-menu li:last-child {
    margin-right: 0px;
}
.ca-menu li a {
    width: 100%;
    height: 100%;
    display: block;
    position: relative;
}
.ca-menu li a img {
    text-align: left;
    width: auto;
    height: 70%;
    position: relative;
    margin: 0 auto;
    padding-top: 20px;
}
.ca-content {
    position: absolute;
    left: 0px;
    width: 100%;
    height: 50%;
    top: 50%;
}
.ca-main {
    font-size: 30px;
    opacity: 0.8;
    text-align: center;
    -webkit-transition: all 200ms linear;
    -moz-transition: all 200ms linear;
    -o-transition: all 200ms linear;
    -ms-transition: all 200ms linear;
    transition: all 200ms linear;
}
.ca-sub {
    text-align:center;
    font-size: 14px;
    opacity: 0.8;
    position: absolute;
    bottom: -35px;
    width: 100%; 
    -webkit-transition: all 200ms linear;
    -moz-transition: all 200ms linear;
    -o-transition: all 200ms linear;
    -ms-transition: all 200ms linear;
    transition: all 200ms linear;
}
/*.ca-sub {
	display: none;
}*/
.ca-menu li:hover { /* enlarges box */
    z-index:999;
    -webkit-transform: scale(1.1);  
    -moz-transform: scale(1.1);  
    -ms-transform: scale(1.1);   
    -o-transform: scale(1.1);   
    transform: scale(1.1);  
}
.ca-menu li:hover .ca-main {
    -webkit-animation: smallToBig 300ms ease;
    -moz-animation: smallToBig 300ms ease;
    -ms-animation: smallToBig 300ms ease;
}
.ca-menu li:hover .ca-sub { /* brings up box from bottom */
    display: block;
    bottom: 0 !important;
    /*background-color: #ccc;*/
    -webkit-animation: moveUp 500ms ease;
    -moz-animation: moveUp 500ms ease;
    -ms-animation: moveUp 500ms ease;
}
.quotebtn {
    width: 49.5% !important;
    background-color: #0fbac1;
    float: left;
    color: white !important;
    text-align: center;
    line-height: 25px;
    font-family: geo;
}
.designbtn {
    width: 49.5% !important;
    background-color: #EE3443;
    float: right !important;
    color: white !important;
    text-align: center;
    line-height: 25px;
    font-family: geo;
}
.not {
    color: gray;
    background-color: gainsboro;
    pointer-events: none;
}
.not:hover {
    color: gray !important;
}
.quotebtn:hover, .designbtn:hover {
    color: black !important;
}
.designhere {
    display: none;
    z-index: 1;
    margin-bottom: -150px !important;
}
.designbtn:hover + .designhere {
    display: block !important;
}




@-webkit-keyframes smallToBig{
    from {
        -webkit-transform: scale(0.1);
    }
    to {
        -webkit-transform: scale(1);
    }
}
@-moz-keyframes smallToBig{
    from {
        -moz-transform: scale(0.1);
    }
    to {
        -moz-transform: scale(1);
    }
}
@-ms-keyframes smallToBig{
    from {
        -ms-transform: scale(0.1);
    }
    to {
        -ms-transform: scale(1);
    }
}

@-webkit-keyframes moveFromBottom {
    from {
        -webkit-transform: translateY(100%);
    }
    to {
        -webkit-transform: translateY(0%);
    }
}
@-moz-keyframes moveFromBottom {
    from {
        -moz-transform: translateY(100%);
    }
    to {
        -moz-transform: translateY(0%);
    }
}
@-ms-keyframes moveFromBottom {
    from {
        -ms-transform: translateY(100%);
    }
    to {
        -ms-transform: translateY(0%);
    }
}


/* fixes for chrome */
@media screen and (-webkit-min-device-pixel-ratio:0) {

}
/* /fixes for chrome */

<ul class="ca-menu">
    <li>
        <a href="https://www.brokenarrowwear.com/q/i.aspx?b=Fruit%20of%20the%20Loom&m=3930R">
            <img src="http://www.embroiderywear.com/q/img/FRU3930R.jpg" class="designhere" /><img src="http://www.embroiderywear.com/q/img/FRU3930R.png" />
            <div class="ca-content">
                <div class="ca-sub">
                    <p>Fruit of the Loom <br />Cotton T-Shirt</p>
                    <div>
                        <a href="https://www.brokenarrowwear.com/q/i.aspx?b=Fruit%20of%20the%20Loom&m=3930R" class="quotebtn">Get Quote</a>
                        <a href="https://www.brokenarrowwear.com/designer/designer.aspx?name=Fruit%20of%20the%20Loom&sku=3930R#/artLocations" class="designbtn">Design Now</a>
                    </div>
                </div>
            </div>
        </a>
    </li>
</ul>
&#13;
&#13;
&#13;

http://www.embroiderywear.com/q/hovertest.html这里是我设置的测试网站,如果您想要在线观看

1 个答案:

答案 0 :(得分:1)

当元素是共享同一父级的直接(相邻)兄弟元素时,CSS +选择器仅 。另一个相关的选择器是波浪号~,它不太严格,允许其他兄弟姐妹介于其中。

在您的情况下,.designbtn.designmore 不是兄弟姐妹,因此您的CSS规则无效。当前的CSS规范不允许更改以前的元素或不相关的元素。

此时,您将看到两个选项:更改HTML结构或使用JavaScript来处理事件。

更改HTML

您只需将.designhere img放在HTML结构中的.designbtn链接下即可。结果看起来像这样(我为演示添加了一些CSS规则):

.ca-sub a {
  text-decoration: none;
}

.ca-sub p {
  color: black;
  background-color: white;
  margin-bottom: 0px;
  padding-top: 7px;
  padding-bottom: 11px;
  font-size: 1.2em;
}

.ca-menu {
  padding: 0;
  /*margin:20px auto;*/
  width: 100%;
  margin-top: 0px;
  list-style-type: none;
}

.ca-menu li {
  width: 18.5%;
  height: 215px;
  overflow: hidden;
  position: relative;
  float: left;
  background: white;
  margin-right: 10px;
  margin-bottom: 15px;
  -webkit-transition: all 300ms linear;
  -moz-transition: all 300ms linear;
  -o-transition: all 300ms linear;
  -ms-transition: all 300ms linear;
  transition: all 300ms linear;
}

.ca-menu li:last-child {
  margin-right: 0px;
}

.ca-menu li a {
  width: 100%;
  height: 100%;
  display: block;
  position: relative;
}

.ca-menu li a img {
  text-align: left;
  width: auto;
  height: 70%;
  position: relative;
  margin: 0 auto;
  padding-top: 20px;
}

.ca-content {
  position: absolute;
  left: 0px;
  width: 100%;
  height: 50%;
  top: 50%;
}

.ca-main {
  font-size: 30px;
  opacity: 0.8;
  text-align: center;
  -webkit-transition: all 200ms linear;
  -moz-transition: all 200ms linear;
  -o-transition: all 200ms linear;
  -ms-transition: all 200ms linear;
  transition: all 200ms linear;
}

.ca-sub {
  text-align: center;
  font-size: 14px;
  opacity: 0.8;
  position: absolute;
  bottom: -35px;
  width: 100%;
  -webkit-transition: all 200ms linear;
  -moz-transition: all 200ms linear;
  -o-transition: all 200ms linear;
  -ms-transition: all 200ms linear;
  transition: all 200ms linear;
}


/*.ca-sub {
	display: none;
}*/

.ca-menu li:hover {
  /* enlarges box */
  z-index: 999;
  -webkit-transform: scale(1.1);
  -moz-transform: scale(1.1);
  -ms-transform: scale(1.1);
  -o-transform: scale(1.1);
  transform: scale(1.1);
}

.ca-menu li:hover .ca-main {
  -webkit-animation: smallToBig 300ms ease;
  -moz-animation: smallToBig 300ms ease;
  -ms-animation: smallToBig 300ms ease;
}

.ca-menu li:hover .ca-sub {
  /* brings up box from bottom */
  display: block;
  bottom: 0 !important;
  /*background-color: #ccc;*/
  -webkit-animation: moveUp 500ms ease;
  -moz-animation: moveUp 500ms ease;
  -ms-animation: moveUp 500ms ease;
}

.quotebtn {
  width: 49.5% !important;
  background-color: #0fbac1;
  float: left;
  color: white !important;
  text-align: center;
  line-height: 25px;
  font-family: geo;
}

.designbtn {
  width: 49.5% !important;
  background-color: #EE3443;
  float: right !important;
  color: white !important;
  text-align: center;
  line-height: 25px;
  font-family: geo;
}

.not {
  color: gray;
  background-color: gainsboro;
  pointer-events: none;
}

.not:hover {
  color: gray !important;
}

.quotebtn:hover,
.designbtn:hover {
  color: black !important;
}

.designhere {
  display: none;
  z-index: 1;
  margin-bottom: -150px !important;  
}

/* demo purpose */

.designhere {
    position: absolute;
    border: 1px solid red;
    top: -95px;
    left: 0px;
    width: 100%;
    box-sizing: border-box;
}

.designbtn:hover + .designhere {
  display: block !important;
}

@-webkit-keyframes smallToBig {
  from {
    -webkit-transform: scale(0.1);
  }
  to {
    -webkit-transform: scale(1);
  }
}

@-moz-keyframes smallToBig {
  from {
    -moz-transform: scale(0.1);
  }
  to {
    -moz-transform: scale(1);
  }
}

@-ms-keyframes smallToBig {
  from {
    -ms-transform: scale(0.1);
  }
  to {
    -ms-transform: scale(1);
  }
}

@-webkit-keyframes moveFromBottom {
  from {
    -webkit-transform: translateY(100%);
  }
  to {
    -webkit-transform: translateY(0%);
  }
}

@-moz-keyframes moveFromBottom {
  from {
    -moz-transform: translateY(100%);
  }
  to {
    -moz-transform: translateY(0%);
  }
}

@-ms-keyframes moveFromBottom {
  from {
    -ms-transform: translateY(100%);
  }
  to {
    -ms-transform: translateY(0%);
  }
}


/* fixes for chrome */

@media screen and (-webkit-min-device-pixel-ratio:0) {}


/* /fixes for chrome */
<ul class="ca-menu">
  <li>
    <a href="#">
      <!-- old place -->
      <img src="http://www.embroiderywear.com/q/img/FRU3930R.png" />
      <div class="ca-content">
        <div class="ca-sub">
          <p>Fruit of the Loom
            <br />Cotton T-Shirt</p>
          <div>
            <a href="#" class="quotebtn">Get Quote</a>
            <a href="#" class="designbtn">Design Now</a>
            <!-- new place -->
            <img src="http://www.embroiderywear.com/q/img/FRU3930R.jpg" class="designhere" />
          </div>
        </div>
      </div>
    </a>
  </li>
</ul>

jsFiddle:https://jsfiddle.net/azizn/3em6nz9c/

的jQuery

如果您想保留HTML结构,那么您将无法使用纯CSS解决问题,您需要一个JavaScript解决方案,如下所示:

$(".designbtn").on({
    mouseenter: function () {
        $(this).parents().eq(3).find('.designhere').show();
    },
    mouseleave: function () {
        $(this).parents().eq(3).find('.designhere').hide();
    }
});

$(".designbtn").on({
    mouseenter: function () {
        $(this).parents().eq(3).find('.designhere').show();
    },
    mouseleave: function () {
        $(this).parents().eq(3).find('.designhere').hide();
    }
});
.ca-sub a {
  text-decoration: none;
}

.ca-sub p {
  color: black;
  background-color: white;
  margin-bottom: 0px;
  padding-top: 7px;
  padding-bottom: 11px;
  font-size: 1.2em;
}

.ca-menu {
  padding: 0;
  /*margin:20px auto;*/
  width: 100%;
  margin-top: 0px;
  list-style-type: none;
}

.ca-menu li {
  width: 18.5%;
  height: 215px;
  overflow: hidden;
  position: relative;
  float: left;
  background: white;
  margin-right: 10px;
  margin-bottom: 15px;
  -webkit-transition: all 300ms linear;
  -moz-transition: all 300ms linear;
  -o-transition: all 300ms linear;
  -ms-transition: all 300ms linear;
  transition: all 300ms linear;
}

.ca-menu li:last-child {
  margin-right: 0px;
}

.ca-menu li a {
  width: 100%;
  height: 100%;
  display: block;
  position: relative;
}

.ca-menu li a img {
  text-align: left;
  width: auto;
  height: 70%;
  position: relative;
  margin: 0 auto;
  padding-top: 20px;
}

.ca-content {
  position: absolute;
  left: 0px;
  width: 100%;
  height: 50%;
  top: 50%;
}

.ca-main {
  font-size: 30px;
  opacity: 0.8;
  text-align: center;
  -webkit-transition: all 200ms linear;
  -moz-transition: all 200ms linear;
  -o-transition: all 200ms linear;
  -ms-transition: all 200ms linear;
  transition: all 200ms linear;
}

.ca-sub {
  text-align: center;
  font-size: 14px;
  opacity: 0.8;
  position: absolute;
  bottom: -35px;
  width: 100%;
  -webkit-transition: all 200ms linear;
  -moz-transition: all 200ms linear;
  -o-transition: all 200ms linear;
  -ms-transition: all 200ms linear;
  transition: all 200ms linear;
}


/*.ca-sub {
	display: none;
}*/

.ca-menu li:hover {
  /* enlarges box */
  z-index: 999;
  -webkit-transform: scale(1.1);
  -moz-transform: scale(1.1);
  -ms-transform: scale(1.1);
  -o-transform: scale(1.1);
  transform: scale(1.1);
}

.ca-menu li:hover .ca-main {
  -webkit-animation: smallToBig 300ms ease;
  -moz-animation: smallToBig 300ms ease;
  -ms-animation: smallToBig 300ms ease;
}

.ca-menu li:hover .ca-sub {
  /* brings up box from bottom */
  display: block;
  bottom: 0 !important;
  /*background-color: #ccc;*/
  -webkit-animation: moveUp 500ms ease;
  -moz-animation: moveUp 500ms ease;
  -ms-animation: moveUp 500ms ease;
}

.quotebtn {
  width: 49.5% !important;
  background-color: #0fbac1;
  float: left;
  color: white !important;
  text-align: center;
  line-height: 25px;
  font-family: geo;
}

.designbtn {
  width: 49.5% !important;
  background-color: #EE3443;
  float: right !important;
  color: white !important;
  text-align: center;
  line-height: 25px;
  font-family: geo;
}

.not {
  color: gray;
  background-color: gainsboro;
  pointer-events: none;
}

.not:hover {
  color: gray !important;
}

.quotebtn:hover,
.designbtn:hover {
  color: black !important;
}

.designhere {
  display: none;
  z-index: 1;
  margin-bottom: -150px !important;  
}

.designbtn:hover + .designhere {
  display: block !important;
}

@-webkit-keyframes smallToBig {
  from {
    -webkit-transform: scale(0.1);
  }
  to {
    -webkit-transform: scale(1);
  }
}

@-moz-keyframes smallToBig {
  from {
    -moz-transform: scale(0.1);
  }
  to {
    -moz-transform: scale(1);
  }
}

@-ms-keyframes smallToBig {
  from {
    -ms-transform: scale(0.1);
  }
  to {
    -ms-transform: scale(1);
  }
}

@-webkit-keyframes moveFromBottom {
  from {
    -webkit-transform: translateY(100%);
  }
  to {
    -webkit-transform: translateY(0%);
  }
}

@-moz-keyframes moveFromBottom {
  from {
    -moz-transform: translateY(100%);
  }
  to {
    -moz-transform: translateY(0%);
  }
}

@-ms-keyframes moveFromBottom {
  from {
    -ms-transform: translateY(100%);
  }
  to {
    -ms-transform: translateY(0%);
  }
}


/* fixes for chrome */

@media screen and (-webkit-min-device-pixel-ratio:0) {}


/* /fixes for chrome */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="ca-menu">
  <li>
    <a href="#">
      <img src="http://www.embroiderywear.com/q/img/FRU3930R.jpg" class="designhere" />
      <img src="http://www.embroiderywear.com/q/img/FRU3930R.png" />
      <div class="ca-content">
        <div class="ca-sub">
          <p>Fruit of the Loom
            <br />Cotton T-Shirt</p>
          <div>
            <a href="#" class="quotebtn">Get Quote</a>
            <a href="#" class="designbtn">Design Now</a>
          </div>
        </div>
      </div>
    </a>
  </li>
</ul>

jsFiddle:https://jsfiddle.net/azizn/jk6Lqu6q/