我有一个在父div中移动不同子元素的JavaScript函数,但我想添加一个转换,以便元素移动到它们在div中的新位置,而不是立即更改位置.

就我使用的过渡而言,对我来说,它只适用于定义的属性,如top/bottomheight/width.在本例中,我没有定义任何属性,所以我想知道是否仍然可以使用过渡效果.

在这里,您可以看到我设置的子元素和父元素,以及我的移动子元素的函数:

function moveElement(elementId, place) {
  let div = document.getElementById('parent')
  let element = document.getElementById(String(elementId))
  let referencenode = div.children[place]
  div.insertBefore(element, referencenode)
}

var children = document.getElementById('parent').children;

document.addEventListener("click", function(event) {
  let id = children.item(2).id;
  moveElement(id, 0)
});
<div id="parent">
  <div id="child1" style="background-color:red" class="child">
    <p>child1</p>
  </div>
  <div id="child2" style="background-color:lightblue" class="child">
    <p>child2</p>
  </div>
  <div id="child3" style="background-color:green" class="child">
    <p>child3</p>
  </div>
  <div id="child4" style="background-color:yellow" class="child">
    <p>child4</p>
  </div>
</div>

由于我所有进行工作转换的try 都失败了,我只离开了switch 功能,所以他们立即交换了位置.

我try 使用transition-duration,但由于没有定义任何css属性,因此预计不会发生任何情况.

提前谢谢!

请不要使用jQuery,因为我从来没有使用过它,所以如果可能的话,我会很感激普通的JavaScript:)

推荐答案

正如 comments 所建议的那样,当更改了css属性时,就会发生css转换,因此必须更改一些css属性才能进行转换.对于顺序或DOM元素中的动画更改,您可以考虑使用First, Last, Invert, Play(翻转)technique1234,从而:

  1. 获取元素的初始位置(S).
  2. Last:使元素达到其结束状态.
  3. 使用前面步骤中的信息将位置更改反转回元素(S)在更改发生之前的外观.
  4. 移除反转,但带有过渡,以便元素(S)从反转动画回到其新位置.

function moveElement(elementId, place) {
  let div = document.getElementById('parent')
  let element = document.getElementById(String(elementId))
  
  const children = Array.from(element.parentElement.children);
  let referencenode = children[place]
  
  // Get the child index of the element being moved.
  const elementIndex = children.indexOf(element);
  // Get the list of elements that will move.
  const movingChildren = children.slice(
    elementIndex > place ? place : elementIndex,
    (elementIndex > place ? elementIndex : place) + 1,
  );
  
  // Capture the positions of the elements being moved (First).
  const before = movingChildren.map(
    movingChild => movingChild.getBoundingClientRect(),
  );  
  
  // Do the moving (Last).
  div.insertBefore(element, referencenode);
  
  // Do the animations.
  movingChildren.forEach((child, i) => {
    // Get the moved position.
    const newPosition = child.getBoundingClientRect();
    // `transform` the element back to their old position (Invert).
    child.style.transform = `translate(${before[i].x - newPosition.x}px, ${before[i].y - newPosition.y}px)`;
    
    // Initialize the transition on the next render frame.
    requestAnimationFrame(() => {
      // Clear transitioning.
      const clearTransition = () => {
        child.style.transition = '';
        child.removeEventListener('transitionend', clearTransition);
      };
      child.addEventListener('transitionend', clearTransition);
      
      // Do the transition (Play).
      child.style.transition = 'transform 250ms';
      child.style.transform = '';
    });
  });  
}

var children = document.getElementById('parent').children;

document.addEventListener("click", function(event) {
  let id = children.item(2).id;
  moveElement(id, 0)
});
<div id="parent">
  <div id="child1" style="background-color:red" class="child">
    <p>child1</p>
  </div>
  <div id="child2" style="background-color:lightblue" class="child">
    <p>child2</p>
  </div>
  <div id="child3" style="background-color:green" class="child">
    <p>child3</p>
  </div>
  <div id="child4" style="background-color:yellow" class="child">
    <p>child4</p>
  </div>
</div>


1Layout Animation FLIP Technique
2Animating Layouts with the FLIP Technique
3FLIP Your Animations
4Animating the Unanimatable

Javascript相关问答推荐

我的glb文件没有加载到我的three.js文件中

无法将nPM simplex-noise包导入在JS项目中工作

如何解决这个未能在响应上执行json:body stream已读问题?

ReactJS中的material UI自动完成类别

我不知道为什么setwritten包装promise 不能像我预期的那样工作

使搜索栏更改语言

嵌套异步JavaScript(微任务和macrotask队列)

JS:XML insertBefore插入元素

MarkLogic-earch.suggest不返回任何值

在VS代码上一次设置多个变量格式

如何限制显示在分页中的可见页面的数量

Phaserjs-创建带有层纹理的精灵层以自定义外观

在不扭曲纹理的情况下在顶点着色器中旋转UV

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

不协调嵌入图片

需要RTK-在ReactJS中查询多个组件的Mutations 数据

Firebase函数中的FireStore WHERE子句无法执行

如何使pdf.js上的文本呈现为可选?

使用Java脚本筛选数组中最接近值最小的所有项

Js问题:有没有办法将数据从标记表转换成图表?