在CSS中,使用@keyframes定义动画状态,然后使用animation属性将元素与动画状态关联起来。通过animation属性的不同属性值,可以控制动画的状态,例如:
.box {
animation-name: myAnimation;
animation-duration: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
}
@keyframes myAnimation {
from {transform: translateX(0);}
to {transform: translateX(100px);}
}
上述代码将一个名为box的元素与一个名为myAnimation的动画状态关联起来。其中,animation-duration属性表示动画持续的时间,animation-timing-function属性表示动画的缓动函数,animation-iteration-count属性表示动画的循环次数,animation-direction属性表示动画的方向。
在JavaScript中,我们可以使用createElement方法动态创建元素,例如:
const newDiv = document.createElement('div');
newDiv.textContent = '这是一个新的div元素';
document.body.appendChild(newDiv);
上述代码将创建一个新的div元素,并将其添加到body元素中。除了textContent属性外,还可以设置其他属性,例如class、id等。