CSS:在悬停时更改div中的图像

时间:2016-08-03 07:39:05

标签: html css css3

目标:构建两个块,在悬停时应更改背景颜色,文本颜色和图标。

麻烦:我对背景和文字没有任何问题,但我发现图标有些困难。如何编辑我的HTML或CSS以达到目标?

以下是我的代码:https://jsfiddle.net/teyrq0ze/ 悬停时应使用的图像和图像:http://i.imgur.com/8MqehqH.pnghttp://i.imgur.com/pNgIqxQ.png

3 个答案:

答案 0 :(得分:2)

将鼠标悬停在filter上时,将divs添加到图片。



body {
  margin: 0;
  padding: 0;
  width: 100%;
  color: #333;
  font-family: 'Open Sans', sans-serif;
  font-size: 15px;
  background: #333;
}

#footerblurb {
  color: #fff;
}

#footerblurb-inner {
  margin: 0 auto;
  width: 650px;
}

#footerblurb .column1,
.column2 {
  float: left;
  display: table;
  width: 290px;
  padding: 15px;
  cursor: pointer;
  border: 1px dashed #fff;
  transition: all .1s ease-in-out;
  -webkit-transition: all .1s ease-in-out;
  -moz-transition: all .1s ease-in-out;
  -o-transition: all .1s ease-in-out;
}

#footerblurb .column1 {
  margin-right: 6px;
}

#footerblurb .column1:hover,
.column2:hover {
  color: #333;
  background: #fff;
}

#footerblurb .column1 img {
  float: left;
  vertical-align: middle;
  display: table-cell;
}

#footerblurb .column2 img {
  float: right;
  vertical-align: middle;
  display: table-cell;
}

#footerblurb .column1 span {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
}

#footerblurb .column2 span {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
}
#footerblurb .column1:hover img,
#footerblurb .column2:hover img{
	-webkit-filter: invert(100%); 
	filter: invert(100%);
}

<div id="footerblurb">
  <div id="footerblurb-inner">

    <div class="column1" onclick="location.href=''">
      <span><img class="previous" src="http://i.imgur.com/0tnecTN.png"></span>
      <span>text text text</span>
    </div>
    <div class="column2" onclick="location.href=''">
      <span>text text text</span>
      <span><img class="next" src="http://i.imgur.com/e86z3mD.png"></span>
    </div>

    <div class="clr"></div>
  </div>
</div>
&#13;
&#13;
&#13;

在此更新Demo

您可以使用不同的过滤来提供不同的颜色。

了解更多信息read this

答案 1 :(得分:0)

解决方案1:

添加额外的图像并默认隐藏它。在悬停时切换其可见性。这是updated fiddle

body {
  margin: 0;
  padding: 0;
  width: 100%;
  color: #333;
  font-family: 'Open Sans', sans-serif;
  font-size: 15px;
  background: #333;
}
#footerblurb {
  color: #fff;
}
#footerblurb-inner {
  margin: 0 auto;
  width: 650px;
}
#footerblurb .column1,
.column2 {
  float: left;
  display: table;
  width: 290px;
  padding: 15px;
  cursor: pointer;
  border: 1px dashed #fff;
  transition: all .1s ease-in-out;
  -webkit-transition: all .1s ease-in-out;
  -moz-transition: all .1s ease-in-out;
  -o-transition: all .1s ease-in-out;
}
#footerblurb .column1 {
  margin-right: 6px;
}
#footerblurb .column1:hover,
.column2:hover {
  color: #333;
  background: #fff;
}
#footerblurb .column1:hover img,
#footerblurb .column2:hover img {
  display: none;
}
#footerblurb .column1 img.hover,
#footerblurb .column2 img.hover {
  display: none;
}
#footerblurb .column1:hover img.hover,
#footerblurb .column2:hover img.hover {
  display: initial;
}
#footerblurb .column1 img {
  float: left;
  vertical-align: middle;
  display: table-cell;
}
#footerblurb .column2 img {
  float: right;
  vertical-align: middle;
  display: table-cell;
}
#footerblurb .column1 span {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
}
#footerblurb .column2 span {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
}
<div id="footerblurb">
  <div id="footerblurb-inner">

    <div class="column1" onclick="location.href=''">
      <span><img class="previous" src="http://i.imgur.com/0tnecTN.png"><img class="previous hover" src="http://i.imgur.com/8MqehqH.png"></span>
      <span>text text text</span>
    </div>
    <div class="column2" onclick="location.href=''">
      <span>text text text</span>
      <span><img class="next" src="http://i.imgur.com/e86z3mD.png">
      <img class="next hover" src="http://i.imgur.com/pNgIqxQ.png"></span>
    </div>

    <div class="clr"></div>
  </div>
</div>

解决方案2:

使用background-image并在悬停时切换图像来源。

答案 2 :(得分:0)

除非您出于某种原因必须使用图像。你可以使用像Font Awesome这样的图标库。您可以轻松更改图标的颜色,因为它们被视为文本,并且也具有很高的可扩展性。

.column1, .column2 {
  position:relative;
}
.column1:before {
  font-family: FontAwesome;
  content: "\f104";
  font-size:40px;
  position:absolute;
  left:70px;
  top:4px;
}
.column2:before {
  font-family: FontAwesome;
  content: "\f105";
  font-size:40px;
  position:absolute;
  left:70px;
  top:4px;
}

fiddle