将鼠标悬停在上方图像上时,如何更改滑块的不透明度?

时间:2019-06-14 13:48:22

标签: html css opacity

所以我有一个播放音频的图像,并在单击时更改图像。我在图像下方还有一个音量滑块。我的问题是,将鼠标悬停在图像上时如何使滑块淡入淡出?

当前,将鼠标悬停在滑块本身而不是图像上时,将出现滑块上方。我尝试将不透明度设置为变量,然后通过JavaScript进行更改,但这似乎无济于事。

val allTextViews = recyclerView.children
     .mapNotNull { it.findViewById<TextView>(R.id.myTitle) }
     .toList()
text = text.replace(word, str(word.upper()))
    function CoffeeChange() {
    if (Coffee.src == "https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png") {
        Coffee.src = "https://static.wixstatic.com/media/38d0c2_37f01de3219a4804a3cb29f39e57b48c~mv2.png"; 
    } else {
        Coffee.src = "https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png";
    }}

    document.getElementById("Coffee").onclick = function() {
        CoffeeChange()
        var audio = document.getElementById("CoffeeAudio");
        if (audio.paused) audio.play();
        else audio.pause();
    };

    volume.addEventListener("mousemove", coffeeVolume);

    function coffeeVolume(){
        document.getElementById("CoffeeAudio").volume = document.getElementById("volume").value / 100;
    }

我已经添加了完整的代码,以使我清楚自己到目前为止所做的事情,以防万一有可能阻止它正常工作。

2 个答案:

答案 0 :(得分:1)

您需要在代码中颠倒<img>.slidecontainer的顺序:

<img id="Coffee" src="https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png" height="100" width="100">
<div class="slidecontainer">
  <input type="range" min="0" max="100" value="100" class="slider" id="volume">
</div>

...并使用它

.slidecontainer {
  opacity: 0;
  transition: opacity .3s cubic-bezier(.4, 0, .2, 1); /* optional: fade transition */
}
.slidecontainer:hover, #Coffe:hover + .slidecontainer {
  opacity: 1;
  z-index: 1; /* optional: if img and the slider overlap, you want the slider on top */
}

此外,如果尚未定位.slidecontainer(具有position以外的static值),则需要给它position:relative。这涵盖了它们重叠的情况。

工作示例:

function CoffeeChange() {
  if (Coffee.src == "https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png") {
    Coffee.src = "https://static.wixstatic.com/media/38d0c2_37f01de3219a4804a3cb29f39e57b48c~mv2.png";
  } else {
    Coffee.src = "https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png";
  }
}

document.getElementById("Coffee").onclick = function() {
  CoffeeChange()
  var audio = document.getElementById("CoffeeAudio");
  if (audio.paused) audio.play();
  else audio.pause();
};

volume.addEventListener("mousemove", coffeeVolume);

function coffeeVolume() {
  document.getElementById("CoffeeAudio").volume = document.getElementById("volume").value / 100;
}
.slidecontainer {
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  width: 100px;
  height: 15px;
  border-radius: 5px;
  background: #d3d3d3;
  outline: none;
  -webkit-transition: .2s;
  transition: opacity .2s;
  position: relative;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slidecontainer {
  opacity: 0;
  transition: opacity .3s cubic-bezier(.4, 0, .2, 1);
}

.slidecontainer:hover,
#Coffee:hover+.slidecontainer {
  opacity: 1;
}
<img id="Coffee" src="https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png" height="100" width="100">
<div class="slidecontainer">
  <input type="range" min="0" max="100" value="100" class="slider" id="volume">
</div>
<audio id="CoffeeAudio" loop>
   <source src="//music.wixstatic.com/preview/38d0c2_02a2ea74abbc49a29ae7bdfabd1091d0-128.mp3" type="audio/wav" />
</audio>

答案 1 :(得分:0)

好吧,您可以做的一件事是将<img>和.slidecontainer包裹在div中,然后像

一样将鼠标移到该包裹器上
   <section id="CoffeeAndSlider" onmouseover="showSlider('unhide')"onmouseout="showSlider('hide')">
            <div class="slidecontainer">
              <input type="range" min="0" max="100" value="100" class="slider" id="volume">
            </div>

            <img id="Coffee" src="https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png"
            height="100" width="100">
     </section>

showSlider看起来像

function showSlider(flag){
 if(flag==='unhide'){
    document.getElementById('volume').classList.add("unhide");
 }
 else if(flag==='unhide'){
 document.getElementById('volume').classList.remove("unhide");
 }
}

最后将其添加到您的CSS并删除.slider:hover

.unhide{
    opacity: 1;
}