在添加类的情况下转换是否不适用于模板元素(HTML5) ...??
我使用HTML5的模板通过javascript将新的子元素添加到根元素。为了获得良好的视觉效果,我使用了一些CSS过渡。通常,CSS的所有转换都可以正常工作,但我不能从HTML5模板中添加新元素
有人能帮帮我吗?
我的代码简化如下
function transform() {
var root = document.querySelector('#root');
var template_content = document.querySelector('#myElem').content;
root.appendChild(document.importNode(template_content, true));
var el = root.querySelector('.ini');
console.log(root);
el.classList.add('show');
}
.ini {
position: relative;
left: 200px;
top: 200px;
width: 300px;
height: 200px;
background-color: #f0f;
}
.show {
transition: all 3s ease;
left: 200px;
top: 200px;
width: 500px;
height: 200px;
background-color: #f0f;
}
<body>
<h1 class="text-center">Component Test Bed</h1>
<!-- <div class="ini"></div> -->
<div id="root"></div>
<button onclick="transform()">Click</button>
<template id="myElem">
<div class="ini"></div>
</template>
</body>
提前感谢
发布于 2015-09-02 18:33:02
我有一个解决方案,但更复杂...使用CSS3关键帧和监听动画结束事件
CSS
.ini {
position: relative;
left: 200px;
top: 200px;
width: 300px;
height: 200px;
background-color: #f0f;
animation: GROWUP 2s ;
}
@keyframes GROWUP {
0% { width: 300px; }
100% { width: 500px; }
}
function transform() {
var root = document.querySelector('#root');
var template_content = document.querySelector('#myElem').content;
root.appendChild(document.importNode(template_content, true));
var el = root.querySelector('.ini');
console.log(root);
el.addEventListener("animationend", function(){
console.log(this);
this.style.height = "50px";
}, false);
}
HTML
<body>
<h1 class="text-center">Component Test Bed</h1>
<!-- <div class="ini"></div> -->
<div id="root"></div>
<button onclick="transform()">Click</button>
<template id="myElem">
<div class="ini"></div>
</template>
</body>
https://stackoverflow.com/questions/32349511
复制相似问题