如何在div上填充背景颜色的动画

时间:2018-06-05 13:30:49

标签: javascript html css css-animations

我正在开发一个自定义Angular组件,目前看起来如下所示:

Slider

您在左侧看到的绿色小部分是我尝试单独div重叠,以便当我点击其中一个白色圆形选择器时,绿色会被填满,直到该点。

这是我的代码:

var shell = document.querySelector('.shell');
var inner = document.querySelector('.inner-shell');

shell.addEventListener('click', function(ev) {
  alert("in");
  if (ev.path && ev.path[0].className === 'indicator') {
    var targetNode = ev.srcElement;
    var centerX = targetNode.offsetLeft + targetNode.offsetWidth;
    var centerY = targetNode.offsetTop + targetNode.offsetHeight;

    inner.style.width = `${centerX + 40}px`;
  }
});
.inner-shell {
  background-color: #6FBC92;
  width: 50px;
  transition: 1s;
}

.shell {
  background-color: #DCDCDC;
  border-radius: 20px;
  display: inline-flex;
}

.indicator {
  background-color: #FFFFFF;
  border: 1px solid #FFFFFF;
  border-radius: 20px;
  height: 13px;
  margin: 3px 28px;
  width: 13px;
}
<div class="inner-shell">
    <div class="shell">
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
      <div class="indicator"></div>
    </div>

以下是我使用上面的代码获得的内容(我点击了最后一个指示符以达到此状态):

Slider2

如何将inner-shell置于shell之上但仍位于indicators之下?

由于

2 个答案:

答案 0 :(得分:1)

您可以使用linear-gradient,这是一个可以调整的简化示例:

var shell = document.querySelectorAll('.container div');

for(var i=0;i<shell.length;i++) {
  shell[i].addEventListener('click', function(ev) {
      var centerX = ev.target.offsetLeft - ev.target.parentNode.offsetLeft + 15;
      ev.target.parentNode.style.backgroundSize =centerX+'px 100%';
  });
}
.container {
  margin:5px auto;
  height:30px;
  width:300px;
  border-radius:30px;
  background-image:linear-gradient(green,green);
  background-position:left;
  background-size:0px 100%;
  background-repeat:no-repeat;
  background-color:grey;
  display:flex;
  justify-content:space-around;
  transition:1s all;
}
.container > div {
  height:30px;
  width:30px;
  border-radius:50%;
  background:#fff;
}
<div class="container">
<div></div><div></div><div></div><div></div>
</div>

答案 1 :(得分:1)

您可以将inner-shell置于shell内并使用position:absolute对指标进行重叠

请参阅代码段:

&#13;
&#13;
var shell = document.querySelector('.shell');
var inner = document.querySelector('.inner-shell');

shell.addEventListener('click', function(ev) {
  if (ev.path && ev.path[0].className === 'indicator') {
    var targetNode = ev.srcElement;
    var centerX = targetNode.offsetLeft + targetNode.offsetWidth;
    var centerY = targetNode.offsetTop + targetNode.offsetHeight;

    inner.style.width = `${centerX + 40}px`;
  }
});
&#13;
.shell .inner-shell {
  background-color: #6FBC92;
  width: 10px;
  transition: 1s;
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  height: 100%;
  border-radius: 20px;
}

.shell {
  background-color: #DCDCDC;
  border-radius: 20px;
  display: inline-flex;
  position: relative;
}

.indicator {
  background-color: #FFFFFF;
  border: 1px solid #FFFFFF;
  border-radius: 20px;
  height: 13px;
  margin: 3px 28px;
  width: 13px;
  position:relative;
  z-index:111;
}
&#13;
<div class="shell">
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="indicator"></div>
  <div class="inner-shell"></div>
</div>
&#13;
&#13;
&#13;