如何在元素悬停时为另一个元素设置动画?

时间:2016-12-19 12:22:47

标签: javascript jquery css html5 css-transitions

我有一个HTML 5范围元素。我需要做的是当用户将鼠标悬停在范围上时,拇指的高度和宽度应增加到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;}');
});

2 个答案:

答案 0 :(得分:2)

你必须在你的整个范围元素中添加-webkit-appearance:none,以便它的拇指可以被设置样式。最后你不需要jquery来做到这一点。

&#13;
&#13;
.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;
}
&#13;
<input type="range" class="myrange" value="50">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这应该会帮助你!

&#13;
&#13;
$('input').mouseenter(function() {
  $('input').toggleClass('over');
});
$('input').mouseleave(function() {
  $('input').toggleClass('over');
})
&#13;
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;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='range'>
&#13;
&#13;
&#13;