在添加类的情况下,CSS 3的过渡是否不能与模板元素(HTML 5)一起工作?
Is CSS 3 Transition do not work with Template element (HTML 5) in case of Adding Class...?
是过渡不与模板元素(HTML5)在添加类的情况下工作…??
我使用HTML 5的模板,通过javascript添加新的子元素到根元素。为了使视觉效果更好,我使用了一些CSS过渡。通常,CSS的所有过渡都能正常工作但我不能从HTML 5模板中添加新元素
有人能帮我一下吗
我的代码简化如下
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>
提前致谢
一个快速和肮脏的.setTimeout()
解决您的问题。我不知道为什么会这样,但我发现这个技巧在不同的情况下非常有用。
片段:
var myElem = document.querySelector('#myElem');
var root = document.querySelector('#root');
var myButton = document.getElementsByTagName('button')[0];
myButton.addEventListener('click', transform, false);
function transform() {
root.appendChild(document.importNode(myElem.content, true));
setTimeout(function() {
var el = root.querySelector('.ini');
el.classList.add('show');
}, 100);
}
.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;
}
<h1 class="text-center">Component Test Bed</h1>
<div id="root"></div>
<button>Click</button>
<template id="myElem">
<div class="ini"></div>
</template>
jsFiddle希望这对你有所帮助。关于自定义元素的文章也可能有所帮助。
我有一个解决方案,但更复杂…通过使用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>
相关文章:
- 我应该怎么做才能避免在网页上移动元素(html、css、jQuery)
- 更改元素HTML,但忽略最后一个子项
- 增加在选择元素HTML中搜索的时间
- Jquery无法获取元素html
- 从所选元素 html 上方的元素获取属性值
- 在 JS 的单选按钮元素 (HTML) 中使用 title 属性
- 如何在页面上播放每个音频元素(HTML,JQuery,Javascript)
- 我需要一种新的方法来检测元素 HTML 是否有更改
- 获取父元素 html 以及子内容
- AngularJS指令:如何在ng-repeat中动态更改元素html
- html 属性中的引号被标记为 ”来自元素.html()的结果
- 在指令's链接中替换angular元素html
- 如果元素有子元素html元素
- 如何下推表格中的元素- HTML
- 如何在纯javascript中使用变量插入数组值,如元素HTML
- jQuery textilate .js -如何“重置”元素HTML并重新启动动画
- 将元素HTML附加到新附加的textarea中
- 在添加类的情况下,CSS 3的过渡是否不能与模板元素(HTML 5)一起工作?
- jQuery没有更新DOM元素HTML
- 在jQuery中更快地替换元素HTML