如何修复我的悬停效果以突出显示所选项目?

时间:2017-05-14 23:32:44

标签: javascript html css reactjs

我正在尝试在悬停时突出显示一个项目,同时降低其他项目的“亮度”,因此重点关注悬停项目。我的项目由图片,尺寸选择器和添加到购物车按钮组成。我想要的是这三个部分在鼠标悬停在任何这些部分上的同时突出显示。我只想用CSS做这件事,并设法得到我想要的工作,除非鼠标悬停添加到购物车按钮或选择器,只有那些项目将突出显示。这是我的CSS:

   #Productlist {
      list-style-type: none;
      position: relative;
      display: inline-block;
      margin-top: 100px;
    }

    .Product img {
      width: 350px; 
      height: 400px;
      display: inline-block;
      position: relative;
      left: 50px;
      margin-top: 100px;
    }

    .Product:hover img {
      opacity: 1;
    }

    #Productlist:hover img{
      opacity: 0.5;
      -webkit-transition: .3s ease-in-out;
      transition: .3s ease-in-out;
    } 

    .Product button {
      background-color: #141516;
      position: relative;
      right: 134px;
      display: inline-block;
      z-index: 1;
      padding: 15px 45px;
      line-height: 1.8;
      text-align:center;
      text-transform:uppercase;
      font-size:0.8rem;
      font-weight:600;
      -webkit-transition: all 0.3s;
      -moz-transition: all 0.3s;
      transition: all 0.3s;
      border: 3px solid white;
    }

    .Product button a {

      position:relative;
      padding:0px;
      text-align:center;
      text-transform:uppercase;
      color:#888888;
      font-size:0.8rem;
      font-weight:600;
      line-height:60px;
      -webkit-transition: all 0.3s;
      -moz-transition: all 0.3s;
      transition: all 0.3s;

    }

    .Product button:hover a {

      color:red;
      z-index: 1000;
    }

    .Product button:hover {

      color:black;
      background-color: white;
      border: 1px solid white;
    }

    #size{
      display: inline-block;
      z-index: 1;
      color: white;
      position: relative;
      right: 484px;
      border: 3px solid white;
      width: 120px;
      height: 40px;
      border-radius: 3px;
      overflow: hidden;
      background: #141516 url("img/icon-select.png") no-repeat 90% 50%;
    }

    #size:focus {
      outline: none;
    }

这是我的HTML:

    <div className='Webshop' id='Webshop'>
        <li id="Productlist">
            <div className='Product'>
              <img src={Seltzshirt}></img>
              <button onClick={this.handleClick} className="addit">Add to cart</button>
              <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
            </div>
            <div className='Product'>  
              <img src={Seltzshirt}></img>
              <button onClick={this.handleClick} className="addit">Add to cart</button>
              <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
            </div>
        </li>
    </div>

更新

我更新了我的CSS和HTML,以显示我所做的一些更改并应用Michael Coker的建议。现在,当我将图像,按钮或选择器悬停时,所有图像都会变暗,选择器和按钮都会保持高亮显示。为什么.Product img忽略了在悬停时更改opacity: 1;

我制作了此CodePen以显示我的问题:https://codepen.io/Feners4/pen/xdQmyQ

2 个答案:

答案 0 :(得分:0)

使用#Productlist:hover img { opacity: .5; }更改所有图片的不透明度,并使用#Product:hover img { opacity: 1; }

排除当前图片的不透明度

#Productlist {
  list-style-type: none;
  position: relative;
  display: inline-block;
  margin-top: 100px;
}

#Product img {
  width: 350px;
  height: 400px;
  display: inline-block;
  position: relative;
  left: 50px;
  margin-top: 100px;
}

#Productlist:hover img {
  opacity: 0.5;
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
}

#Product:hover img {
  opacity: 1;
}

#Webshop button {
  background-color: #141516;
  position: relative;
  right: 134px;
  display: inline-block;
  z-index: 1;
  padding: 15px 45px;
  line-height: 1.8;
  text-align: center;
  text-transform: uppercase;
  font-size: 0.8rem;
  font-weight: 600;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
  border: 3px solid white;
}

#Webshop button a {
  position: relative;
  padding: 0px;
  text-align: center;
  text-transform: uppercase;
  color: #888888;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 60px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}

#Webshop button:hover a {
  color: red;
  z-index: 1000;
}

#Webshop button:hover {
  color: black;
  background-color: white;
  border: 1px solid white;
}

#size {
  display: inline-block;
  z-index: 1;
  color: white;
  position: relative;
  right: 484px;
  border: 3px solid white;
  width: 120px;
  height: 40px;
  border-radius: 3px;
  overflow: hidden;
  background: #141516 url("img/icon-select.png") no-repeat 90% 50%;
}

#size:focus {
  outline: none;
}
<div className='Webshop' id='Webshop'>
  <li id="Productlist">
    <div id='Product'>
      <img src=http://kenwheeler.github.io/slick/img/lazyfonz2.png></img>
      <button onClick={this.handleClick} className="addit">Add to cart</button>
      <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
    </div>
    <div id='Product'>
      <img src=http://kenwheeler.github.io/slick/img/lazyfonz2.png></img>
      <button onClick={this.handleClick} className="addit">Add to cart</button>
      <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
    </div>
    <div id='Product'>
      <img src=http://kenwheeler.github.io/slick/img/lazyfonz2.png></img>
      <button onClick={this.handleClick} className="addit">Add to cart</button>
      <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
    </div>
    <div id='Product'>
      <img src=http://kenwheeler.github.io/slick/img/lazyfonz2.png></img>
      <button onClick={this.handleClick} className="addit">Add to cart</button>
      <select id="size" onChange={this.change} value={this.state.value}>
                <option value="medium">Medium</option>
                <option value="large">Large</option>
                <option value="x-large">X-large</option>
              </select>
    </div>
  </li>
</div>

答案 1 :(得分:0)

简单和基本的解释:

您需要像这样定位父母的悬停,并在所有儿童身上应用该风格。之后,在当前悬停元素上应用.caret:after { content: url('https://www.nknews.org/pro/wp-content/themes/NKNewsvizzPro/images/caret-icon.png') }

opacity: 1

Demo