【问题标题】:How do I change the opacity of a slider when hovering over an image above?将鼠标悬停在上面的图像上时,如何更改滑块的不透明度?
【发布时间】:2019-06-15 02:49:29
【问题描述】:

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

当悬停在滑块本身而不是图像上时,会出现滑块。我尝试将不透明度设置为变量并通过 JavaScript 更改它,但这似乎没有任何作用。

    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;
      top: 130px;
      opacity:0;
    }

    .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;
    }
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <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">


    <audio id="CoffeeAudio" loop>
       <source src="//music.wixstatic.com/preview/38d0c2_02a2ea74abbc49a29ae7bdfabd1091d0-128.mp3" type="audio/wav" />
    </audio>

我已经添加了整个代码,以明确我是如何做我迄今为止所做的事情的,以防万一有任何事情可能会阻止它工作。

【问题讨论】:

    标签: html css opacity


    【解决方案1】:

    你需要颠倒代码中&lt;img&gt;和.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>

    【讨论】:

    • 老兄,非常感谢!有效。我是 CSS 和 HTML 的新手,所以这是一个学习曲线。谢谢!
    【解决方案2】:

    好吧,您可以做的一件事是将&lt;img&gt; 和 .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;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 2023-01-20
      • 2018-05-23
      • 2013-10-31
      • 2013-10-30
      • 2010-10-20
      • 1970-01-01
      • 2015-05-11
      • 2015-06-04
      相关资源
      最近更新 更多