JSFiddle - React, Tailwind, and code Playground
HTML
<article>
<div class="items"></div>
<a href="javascript:;" class="items-more-btn">展开</a>
</article>
<script type="text/template" id="item-tpl">
<div class="item item__anim item__anim-{{index}}">
<h2>标题{{index}}</h2>
<p>{{content}}</p>
</div>
</script>
SCSS
body {
background: #f2f2f2;
}
article {
margin-bottom: 100px;
width: 500px;
text-align: center;
}
.items {
width: 400px;
margin: 30px auto;
}
.items-more-btn {
margin-bottom: 20px;
}
@keyframes showBox {
to {
opacity: 1;
transform: scale(1) translate3d(0, 0, 0);
}
}
.item {
position: relative;
margin-top: 10px;
width: 100%;
height: 150px;
background: #fff;
border-radius: 10px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
color: #666;
opacity: 0;
transform: scale(.9) translate3d(0, -10px, 0);
&:nth-child(odd) {
height: 200px;
}
h2 {
width: 100%;
font-size: 18px;
}
&.item__anim {
animation-name: showBox;
animation-duration: .4s;
animation-fill-mode: forwards;
}
&__anim-1 {
animation-duration: .6s;
}
@mixin showingDelay() {
@for $n from 1 through 30 {
&__anim-#{$n} {
animation-delay: #{(0.05 + $n / 10)}s;
}
}
}
@include showingDelay();
}
JavaScript
function addEvent(elem, type, handler) {
elem.addEventListener(type, handler, false);
}
function qs(selector) {
return document.querySelector(selector);
}
function qsa(selectors) {
return document.querySelectorAll(selectors);
}
var mockData = ['内容1', '内容2', '内容3', '内容4', '内容5'];
addEvent(qs('.items-more-btn'), 'click', function() {
renderList(mockData);
});
var itemTpl = qs('#item-tpl').innerHTML;
var itemsDOM = qs('.items');
/**
* 渲染数据
* @param {[type]} data [description]
* @return {[type]} [description]
*/
function renderList(data) {
var data = data.map(function(val, index) {
return {
index: index + 1,
content: val
};
});
var html = '';
var fragment = document.createDocumentFragment();
data.forEach(function(item) {
var divTemp = document.createElement('div');
// 模板替换
divTemp.innerHTML = itemTpl.replace(/{{(\w+)}}/g, function(input, match) {
return match ? item[match] || '' : '';
});
fragment.appendChild(divTemp.firstElementChild);
});
// 渲染
itemsDOM.appendChild(fragment);
}