CSS3 之高级动画(8)HTML5和CSS3实现的自定义3D滑杆控件

原文链接地址

更多优秀动画

 

动画效果:

JS分析:

function initInputs() {
  // 获取 input:range NodeList
  let allInputs = document.body.querySelectorAll(".bar-input");

  for (let i = 0; i < allInputs.length; i++) {
    // 获取 单独的 input
    let input = allInputs[i];
    // 获取 单独的 input 父节点的 ID
    let barId = input.parentNode.id;
    // 在 head 标签添加 style 标签
    let styleEl = document.head.appendChild(document.createElement("style"));

    if (i == allInputs.length - 1) {
      //set indicator 设置第三个进度的值 89%
      let indicator=input.parentNode.querySelector('.bar .indicator');
      setBarIndicator(barId, input, styleEl, indicator);
      // 方法在 oninput onchange 事件触发时调用,并传递参数
      input.oninput = setBarIndicator.bind(this, barId, input, styleEl, indicator);
      input.onchange = setBarIndicator.bind(this, barId, input, styleEl, indicator);
    } else {
      setBar(barId, input, styleEl);
      input.oninput = setBar.bind(this, barId, input, styleEl);
      input.onchange = setBar.bind(this, barId, input, styleEl);
    }
  }
}

function setBar(barId, input, styleEl) {
  // 在 style 标签中添加 进度 width
  styleEl.innerHTML = "#" + barId + " .bar-face.percentage:before {width:" + input.value + "%;}";
}

function setBarIndicator(barId, input, styleEl, indicatorEl) {
  // 在 style 标签中添加 进度 width
  styleEl.innerHTML = "#" + barId + " .bar-face.percentage:before {width:" + input.value + "%;}";
  // 显示的 89% 的样式
  indicatorEl.style.marginLeft = (input.value - 10) + '%';
  indicatorEl.textContent = input.value + '%';
}

initInputs();

 总结

  1. css 使用 transform 来“画”长方体
  2. 使用 input:range 来完成进度
  3. 使用 input 的 onchange 和 oninput 事件来控制进度

转载于:https://www.cnblogs.com/houfee/p/10762361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值