var DOMAnimations = {
/**
* SlideUp
*
* @param {HTMLElement} element
* @param {Number} duration
* @returns {Promise<boolean>}
*/
slideUp: function (element, duration = 500) {
return new Promise(function (resolve, reject) {
element.style.height = element.offsetHeight + 'px';
element.style.transitionProperty = `height, margin, padding`;
element.style.transitionDuration = duration + 'ms';
element.offsetHeight;
element.style.overflow = 'hidden';
element.style.height = 0;
element.style.paddingTop = 0;
element.style.paddingBottom = 0;
element.style.marginTop = 0;
element.style.marginBottom = 0;
window.setTimeout(function () {
element.style.display = 'none';
element.style.removeProperty('height');
element.style.removeProperty('padding-top');
element.style.removeProperty('padding-bottom');
element.style.removeProperty('margin-top');
element.style.removeProperty('margin-bottom');
element.style.removeProperty('overflow');
element.style.removeProperty('transition-duration');
element.style.removeProperty('transition-property');
resolve(false);
}, duration)
})
},
/**
* SlideDown
*
* @param {HTMLElement} element
* @param {Number} duration
* @returns {Promise<boolean>}
*/
slideDown: function (element, duration = 500) {
return new Promise(function (resolve, reject) {
element.style.removeProperty('display');
let display = window.getComputedStyle(element).display;
if (display === 'none')
display = 'block';
element.style.display = display;
let height = element.offsetHeight;
element.style.overflow = 'hidden';
element.style.height = 0;
element.style.paddingTop = 0;
element.style.paddingBottom = 0;
element.style.marginTop = 0;
element.style.marginBottom = 0;
element.offsetHeight;
element.style.transitionProperty = `height, margin, padding`;
element.style.transitionDuration = duration + 'ms';
element.style.height = height + 'px';
element.style.removeProperty('padding-top');
element.style.removeProperty('padding-bottom');
element.style.removeProperty('margin-top');
element.style.removeProperty('margin-bottom');
window.setTimeout(function () {
element.style.removeProperty('height');
element.style.removeProperty('overflow');
element.style.removeProperty('transition-duration');
element.style.removeProperty('transition-property');
}, duration)
})
},
/**
* SlideToggle
*
* @param {HTMLElement} element
* @param {Number} duration
* @returns {Promise<boolean>}
*/
slideToggle: function (element, duration = 500) {
if (window.getComputedStyle(element).display === 'none') {
return this.slideDown(element, duration);
} else {
return this.slideUp(element, duration);
}
}
}
// ------------------------------------------------------
document.addEventListener("DOMContentLoaded", function() {
var button = document.getElementById('slideToggle');
var cardElement = document.getElementById('firstCard');
button.addEventListener('click', function(event) {
event.preventDefault();
DOMAnimations.slideToggle(cardElement);
});
});
* {
box-sizing: border-box;
}
/* Add a gray background color with some padding */
body {
font-family: Arial;
padding: 20px;
background: #f1f1f1;
}
/* Header/Blog Title */
.header {
padding: 10px;
font-size: 24px;
text-align: center;
background: white;
}
/* Create two unequal columns that floats next to each other */
/* Left column */
.leftcolumn {
float: left;
width: 100%;
}
/* Fake image */
.fakeimg {
background-color: #aaa;
width: 100%;
padding: 20px;
}
/* Add a card effect for articles */
.card {
position:relative;
background-color: white;
padding: 20px;
margin-top: 20px;
}
#slideToggle {
background-color: #f9f5f5;
color: black;
border: 2px solid #a9b5a9;
padding: 5px;
margin-top:20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
font-weight: bold;
border-radius: 5px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Footer */
.footer {
padding: 20px;
text-align: center;
background: #ddd;
margin-top: 20px;
}
/* Responsive layout - when the screen is less than 800px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 800px) {
.leftcolumn, .rightcolumn {
width: 100%;
padding: 0;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="header">
<h2>Blog Name</h2>
</div>
<div class="row">
<div class="leftcolumn">
<button id="slideToggle">slideToggle</button>
<div class="card" id="firstCard">
<h2>FIRST TITLE HEADING</h2>
<h5>Title description, Dec 7, 2018</h5>
<div class="fakeimg" style="height:200px;">Image</div>
<p>Some text..</p>
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
</div>
<div class="card">
<h2>SECOND TITLE HEADING</h2>
<h5>Title description, Dec 7, 2018</h5>
<div class="fakeimg" style="height:200px;">Image</div>
<p>Some text..</p>
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
</div>
</div>
</div>
<div class="footer">
<h2>Footer</h2>
</div>
</body>
</html>