【问题标题】:How to animate another element on hover of an element?如何在元素悬停时为另一个元素设置动画?
【发布时间】:2016-12-19 22:25:24
【问题描述】:

我有一个 HTML 5 range 元素。我需要做的是当用户将鼠标悬停在范围上时,拇指的高度和宽度应该增加到 12 像素。

CSS

.myrange::-webkit-slider-thumb{
     position:relative;
     top:-5px;
     appearance:none;
     -webkit-appearance:none;
     -webkit-transition: width 2s, height 4s;
     transition: width 2s, height 4s;
     border-radius:50px;
     background-color:rgb(9,90,0);
     border:0;
     cursor:pointer;
     visibility:hidden;
}

JavaScript

var skb_rhdaseek = $("<style>", {"type": "text/css"}).appendTo("head");
$('.myrange').hover(function(){
    skb_rhdaseek.text('.myrange::-webkit-slider-thumb{height:12px; width:12px;}');
});

【问题讨论】:

  • $(hoverelem).hover(function(){ $(anotherleelem).animate(function(){}); });
  • 您的代码有很多问题,而且从外观上看,您正在尝试更改同一元素的样式。
  • @RajshekarReddy 这就是为什么我自己做不到的原因,我的代码并不重要,如果您有更好的方法在 range itselft 悬停时为 range thumb 设置动画,我将不胜感激!

标签: javascript jquery css html css-transitions


【解决方案1】:

您还必须将 -webkit-appearance: none 添加到整个范围元素中,以便它的拇指设置样式。最后,您不需要 jquery 来执行此操作。

.myrange {
 -webkit-appearance: none; 
 height:10px;
 width:200px;
 background-color:#e3f2fd;
}

.myrange::-webkit-slider-thumb {
  height: 10px;
  width: 10px;
  background: #33aaff;
  cursor: pointer;
  -webkit-appearance: none;
  transition: height .2s ease-in-out;
}

input[type=range]:hover::-webkit-slider-thumb {
  height: 30px;
}
&lt;input type="range" class="myrange" value="50"&gt;

【讨论】:

    【解决方案2】:

    这应该可以帮助你!

    $('input').mouseenter(function() {
      $('input').toggleClass('over');
    });
    $('input').mouseleave(function() {
      $('input').toggleClass('over');
    })
    input[type=range]::-webkit-slider-thumb {
      -webkit-appearance: none;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
      margin-top: -14px;
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d; 
    }
    
    /* All the same stuff for Firefox */
    input[type=range]::-moz-range-thumb {
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
    }
    
    /* All the same stuff for IE */
    input[type=range]::-ms-thumb {
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
    }
    input[type=range].over::-webkit-slider-thumb {
      height: 12px;
      width: 12px;
    }
    
    /* All the same stuff for Firefox */
    input[type=range].over::-moz-range-thumb {
      height: 12px;
      width: 12px;
    }
    
    /* All the same stuff for IE */
    input[type=range].over::-ms-thumb {
      height: 12px;
      width: 12px;
    }
    input[type=range]::-webkit-slider-runnable-track {
      width: 100%;
      height: 2px;
      cursor: pointer;
      background: #3071a9;
    }
    input[type=range]::-moz-range-track {
      width: 100%;
      height: 2px;
      cursor: pointer;
      background: #3071a9;
    }
    input[type=range].over::-webkit-slider-runnable-track {
      height: 6px;
    }
    input[type=range].over::-moz-range-track {
      height: 6px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type='range'>

    【讨论】:

    • 对不起,我的问题是如何为范围拇指而不是范围跟踪器设置动画并且动画不改变高度。类似于 youtube 视频拇指!并感谢您的回复!
    猜你喜欢
    • 2021-04-04
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多