如何将元素转换为里程表

时间:2019-03-11 17:38:07

标签: javascript css

我有代码:

  <div class="wrap2" id="wrap" data-num="0">
    <span>0</span><span>1</span>...

CSS:

.wrap2[data-num="0"] {
  transfom:translate(0, 0);
}
.wrap2[data-num="1"] {
  transform:translate(0, -30px);
  }

https://jsfiddle.net/9t4zsuov/2/

但是我想像里程表一样-数字只能滚动到顶部,而不是底部。有什么想法,该怎么做?

3 个答案:

答案 0 :(得分:4)

您可以使用两组数字和一些额外的javascript来达到这种效果。

如果新数字小于当前数字,请使用距离更远的第二组数字(数字0-9)。当css动画从第一组数字过渡到第二组数字时,它将显得好像里程表正在“翻转”。

动画制作完成后,切换回第一组数字而不设置动​​画(无过渡类)。

我已经根据您的原始jsfiddle制作了一个有效的示例。

注意:这利用了DOM元素的.classList属性和tranistionend事件。您可能需要添加供应商前缀(即webkitTransitionEnd)并实现自己的.classList版本,具体取决于您需要支持的浏览器。

document.getElementById("rand").addEventListener("click", randomize);
document.getElementById("cipa").addEventListener("transitionend", transitionEnd);

function randomize() {
  setNumber(Math.floor(Math.random() * 9));
}

function setNumber(newNumber) {
  let dupa = document.getElementById("cipa");

  // assumes dupa.dataset.num always be a valid int
  let selected = parseInt(dupa.dataset.num);

  if (newNumber === selected) return; // if same as existing, don't do anything

  // if the new number is less than the old number
  // use the second set of numbers to avoid moving "backwards"
  if (newNumber < selected) dupa.classList.add("rolledover");

  // animate to the new position
  dupa.classList.add("transitioning");
  dupa.dataset.num = "" + newNumber;
}

function transitionEnd() {
  let dupa = document.getElementById("cipa");
  // don't animate
  dupa.classList.remove("transitioning");
  dupa.classList.remove("rolledover");
}
#rand {
  margin-top: 50px;
}

.dupa1 {
  height: 30px;
  width: 30px;
  border: 1px solid #000;
  overflow: hidden;
}

.dupa2.transitioning {
  transition: all 1s ease;
}

.dupa2 span {
  height: 30px;
  width: 30px;
  display: block;
  text-align: center;
  line-height: 30px;
}

.dupa2[data-num="0"] {
  transform: translate(0, 0);
}

.dupa2[data-num="1"] {
  transform: translate(0, -30px);
}

.dupa2[data-num="2"] {
  transform: translate(0, -60px);
}

.dupa2[data-num="3"] {
  transform: translate(0, -90px);
}

.dupa2[data-num="4"] {
  transform: translate(0, -120px);
}

.dupa2[data-num="5"] {
  transform: translate(0, -150px);
}

.dupa2[data-num="6"] {
  transform: translate(0, -180px);
}

.dupa2[data-num="7"] {
  transform: translate(0, -210px);
}

.dupa2[data-num="8"] {
  transform: translate(0, -240px);
}

.dupa2[data-num="9"] {
  transform: translate(0, -270px);
}

.rolledover.dupa2[data-num="0"] {
  transform: translate(0, -300px);
}

.rolledover.dupa2[data-num="1"] {
  transform: translate(0, -330px);
}

.rolledover.dupa2[data-num="2"] {
  transform: translate(0, -360px);
}

.rolledover.dupa2[data-num="3"] {
  transform: translate(0, -390px);
}

.rolledover.dupa2[data-num="4"] {
  transform: translate(0, -420px);
}

.rolledover.dupa2[data-num="5"] {
  transform: translate(0, -450px);
}

.rolledover.dupa2[data-num="6"] {
  transform: translate(0, -480px);
}

.rolledover.dupa2[data-num="7"] {
  transform: translate(0, -510px);
}

.rolledover.dupa2[data-num="8"] {
  transform: translate(0, -540px);
}

.rolledover.dupa2[data-num="9"] {
  transform: translate(0, -570px);
}
<div class="dupa1">
  <div class="dupa2" id="cipa" data-num="0">
    <span>0</span>
    <span>1</span>
    <span>2</span>
    <span>3</span>
    <span>4</span>
    <span>5</span>
    <span>6</span>
    <span>7</span>
    <span>8</span>
    <span>9</span>
    <span>0</span>
    <span>1</span>
    <span>2</span>
    <span>3</span>
    <span>4</span>
    <span>5</span>
    <span>6</span>
    <span>7</span>
    <span>8</span>
    <span>9</span>
  </div>
</div>

<button id="rand">rand</button>

答案 1 :(得分:4)

正如@codyThompsonDev所说,翻转区域是实现此目标的最佳方法。我认为他想念的是,当您从一个过渡号码转到一个非过渡号码时会发生什么。

例如,假设里程表随机尝试滚动到 request.AddHeader("Content-Type", MimeType(filePath)); request.AddHeader("Content-Disposition", string.Format("file; filename=\"{0}\"", Path.GetFileNameWithoutExtension(filePath))); request.AddParameter(MimeType(filePath), File.ReadAllBytes(filePath), ParameterType.RequestBody); ,然后滚动到4,然后滚动到3。第一次,它可以滚动到4没问题。第二次,它必须在翻转区域中滚动到“ 13”。但随后,它尝试滚动到也在翻转区域中的“ 11”,导致它向后滚动。

要在这种情况下实现此效果,必须将拨盘从回滚区域中弹回,然后再次向前滚。我可以使用1来实现。

我做了一个小提琴来证明这一点:https://jsfiddle.net/tprobinson/8k125fmz/67/

window.requestAnimationFrame()类添加到debugBackground以便直观地查看过渡效果。

我建议使用Sass之类的预处理器生成CSS类,因为手工编写它们也容易出错。

dupa2
document.getElementById("rand").addEventListener("click", randomize);

const debug = document.getElementById("debug");
const dupa = document.getElementById("cipa");

let animationInProgress = null

function setDebug(num) {
  debug.textContent = 'Number is really: ' + num
}

function animateOdometer(newNum) {
  // Add the smooth class and set the number to let it roll.
  dupa.classList.add('smooth')
  setDebug(newNum)
  dupa.dataset.num = newNum

  // In 1000 ms, remove the smooth class
  animationInProgress = window.setTimeout(() => {
    dupa.classList.remove('smooth')
    animationInProgress = null
  }, 1000)
}

function randomize() {

  let oldNum = Number.parseInt(dupa.dataset.num)
  if (oldNum === undefined || oldNum === null) {
    oldNum = 0
  }

  let newNum = Math.floor(Math.random() * 9) + 0;

  // If an animation is already in progress, cancel it
  if (animationInProgress) {
    window.clearTimeout(animationInProgress)
    dupa.classList.remove('smooth')
    animationInProgress = null
  }

  // If the new number is before our old number
  // we have to force a roll forwards
  if (newNum < oldNum) {
    newNum += 10
  }

  if (oldNum > 9) {
    // The dial was already rolled over. We need to
    // snap the dial back before rolling again.
    // Wait for a frame so we can snap the dial back
    dupa.dataset.num = oldNum - 10
    setDebug(oldNum - 10)
    dupa.classList.remove('smooth')

    window.requestAnimationFrame(() => {
      // Wait for one frame to let the snapback happen
      window.requestAnimationFrame(() => {
        // Then roll forward
        animateOdometer(newNum)
      })
    })

    return
  }

  // Roll the dial
  animateOdometer(newNum)
}
#rand,
#debug {
  margin-top: 50px;
}

.dupa1 {
  height: 30px;
  width: 30px;
  border: 1px solid #000;
  overflow: hidden;
}

.dupa2.smooth {
  transition: all 1s ease;
}

.dupa2 span {
  height: 30px;
  width: 30px;
  display: block;
  text-align: center;
  line-height: 30px;
}

.dupa2.debugBackground {
  background: linear-gradient(to bottom, #ffffff 0%, #ffffff 50%, #207cca 51%, #207cca 100%);
}

.dupa2[data-num="0"] {
  transform: translate(0, 0);
}

.dupa2[data-num="1"] {
  transform: translate(0, -30px);
}

.dupa2[data-num="2"] {
  transform: translate(0, -60px);
}

.dupa2[data-num="3"] {
  transform: translate(0, -90px);
}

.dupa2[data-num="4"] {
  transform: translate(0, -120px);
}

.dupa2[data-num="5"] {
  transform: translate(0, -150px);
}

.dupa2[data-num="6"] {
  transform: translate(0, -180px);
}

.dupa2[data-num="7"] {
  transform: translate(0, -210px);
}

.dupa2[data-num="8"] {
  transform: translate(0, -240px);
}

.dupa2[data-num="9"] {
  transform: translate(0, -270px);
}

.dupa2[data-num="10"] {
  transform: translate(0, -300px);
}

.dupa2[data-num="11"] {
  transform: translate(0, -330px);
}

.dupa2[data-num="12"] {
  transform: translate(0, -360px);
}

.dupa2[data-num="13"] {
  transform: translate(0, -390px);
}

.dupa2[data-num="14"] {
  transform: translate(0, -420px);
}

.dupa2[data-num="15"] {
  transform: translate(0, -450px);
}

.dupa2[data-num="16"] {
  transform: translate(0, -480px);
}

.dupa2[data-num="17"] {
  transform: translate(0, -510px);
}

.dupa2[data-num="18"] {
  transform: translate(0, -540px);
}

.dupa2[data-num="19"] {
  transform: translate(0, -570px);
}

答案 2 :(得分:0)

非常感谢。

但是我陷入了另一个类似的问题,但是数组。

为了更好地了解问题,我做了一个小提琴:https://jsfiddle.net/zr2dLbge/

<div class="wrap" id="wrap"></div>

.wrap{
  border:1px solid #000;
  display: inline-block;
  height:30px;
  border-right: none;
}
.numbers{
   width:30px;
  height:30px;
  display:inline-block;
  overflow: hidden;
  border-right: 1px solid #000;
}
.numbers span{
  display: block;
  width:30px;
  height:30px;
  line-height: 30px;
  text-align: center;
}

.numbers[data-num="0"] div{
  transform: translate(0, 0);
  transition: all 1s ease;
}

.numbers[data-num="1"] div{
  transform: translate(0, -30px);
  transition: all 1s ease;
}

.numbers[data-num="2"] div{
  transform: translate(0, -60px);
  transition: all 1s ease;
}

.numbers[data-num="3"] div{
  transform: translate(0, -90px);
  transition: all 1s ease;
}

.numbers[data-num="4"] div{
  transform: translate(0, -120px);
  transition: all 1s ease;
}

.numbers[data-num="5"] div{
  transform: translate(0, -150px);
  transition: all 1s ease;
}

.numbers[data-num="6"] div{
  transform: translate(0, -180px);
  transition: all 1s ease;
}

.numbers[data-num="7"] div{
  transform: translate(0, -210px);
  transition: all 1s ease;
}

.numbers[data-num="8"] div{
  transform: translate(0, -240px);
  transition: all 1s ease;
}

.numbers[data-num="9"] div{
  transform: translate(0, -270px);
  transition: all 1s ease;
}

let arr = [];
var numbers = 1234561234;
const wrap = document.getElementById("wrap");

function toArray (val) {

  return (val).toString().split('');
}

arr = toArray(numbers);

for (let i = 0; i < arr.length; i++) {
    div = document.createElement('div'),
    div.className = "numbers";
    div.dataset.num = arr[i];
    div.dataset.x = i;
    div.innerHTML = "<div><span>0</span><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span></div>"
    wrap.appendChild(div);
}


setInterval(function(){ 
arr.forEach( (k) => {
  arr[k] = Math.floor(Math.random() * 9) + 0;
})

for (let i = 0; i < arr.length; i++) {
    document.querySelector('.numbers[data-x="'+i+'"]').dataset.num = arr[i];
}
 }, 1000);

不幸的是,在这种情况下window.requestAnimationFrame()对我不起作用