|
if (!!Promise) { |
|
window.slideUp = function(target, duration=500) { |
|
return new Promise((resolve, reject) => { |
|
if (!(target instanceof HTMLElement)) { |
|
reject('Unable to slide element.', target); |
|
} else { |
|
target.style.transitionProperty = 'height, margin, padding'; |
|
target.style.transitionDuration = duration + 'ms'; |
|
target.style.boxSizing = 'border-box'; |
|
target.style.height = target.offsetHeight + 'px'; |
|
target.offsetHeight; |
|
target.style.overflow = 'hidden'; |
|
target.style.height = 0; |
|
target.style.paddingTop = 0; |
|
target.style.paddingBottom = 0; |
|
target.style.marginTop = 0; |
|
target.style.marginBottom = 0; |
|
window.setTimeout( () => { |
|
target.style.display = 'none'; |
|
target.style.removeProperty('height'); |
|
target.style.removeProperty('padding-top'); |
|
target.style.removeProperty('padding-bottom'); |
|
target.style.removeProperty('margin-top'); |
|
target.style.removeProperty('margin-bottom'); |
|
target.style.removeProperty('overflow'); |
|
target.style.removeProperty('transition-duration'); |
|
target.style.removeProperty('transition-property'); |
|
resolve(); |
|
}, duration); |
|
} |
|
}); |
|
} |
|
window.slideDown = function(target, duration=500) { |
|
return new Promise((resolve, reject) => { |
|
if (!(target instanceof HTMLElement)) { |
|
reject('Unable to slide element.', target); |
|
} else { |
|
target.style.removeProperty('display'); |
|
let display = window.getComputedStyle(target).display; |
|
|
|
if (display === 'none') |
|
display = 'block'; |
|
|
|
target.style.display = display; |
|
let height = target.offsetHeight; |
|
target.style.overflow = 'hidden'; |
|
target.style.height = 0; |
|
target.style.paddingTop = 0; |
|
target.style.paddingBottom = 0; |
|
target.style.marginTop = 0; |
|
target.style.marginBottom = 0; |
|
target.offsetHeight; |
|
target.style.boxSizing = 'border-box'; |
|
target.style.transitionProperty = "height, margin, padding"; |
|
target.style.transitionDuration = duration + 'ms'; |
|
target.style.height = height + 'px'; |
|
target.style.removeProperty('padding-top'); |
|
target.style.removeProperty('padding-bottom'); |
|
target.style.removeProperty('margin-top'); |
|
target.style.removeProperty('margin-bottom'); |
|
window.setTimeout( () => { |
|
target.style.removeProperty('height'); |
|
target.style.removeProperty('overflow'); |
|
target.style.removeProperty('transition-duration'); |
|
target.style.removeProperty('transition-property'); |
|
resolve(); |
|
}, duration); |
|
} |
|
}); |
|
} |
|
window.slideToHeight = function(target, height, duration = 500) { |
|
return new Promise((resolve, reject) => { |
|
if (!(target instanceof HTMLElement)) { |
|
reject('Unable to slide element.', target); |
|
} else if (!height) { |
|
reject('slideToHeight() requires a height to slide to.'); |
|
} else { |
|
target.style.transitionProperty = 'height'; |
|
target.style.transitionDuration = duration + 'ms'; |
|
target.style.boxSizing = 'border-box'; |
|
target.style.height = target.offsetHeight + 'px'; |
|
target.style.overflow = 'hidden'; |
|
target.style.height = height; |
|
window.setTimeout( () => { |
|
target.style.removeProperty('transition-duration'); |
|
target.style.removeProperty('transition-property'); |
|
resolve(); |
|
}, duration); |
|
} |
|
}); |
|
} |
|
window.slideToggle = function(target, duration = 500) { |
|
return new Promise((resolve, reject) => { |
|
if (!(target instanceof HTMLElement)) { |
|
reject('Unable to slide element.', target); |
|
} else { |
|
if (window.getComputedStyle(target).display === 'none') { |
|
slideDown(target, duration); |
|
setTimeout(resolve(), duration); |
|
} else { |
|
slideUp(target, duration); |
|
setTimeout(resolve(), duration); |
|
} |
|
} |
|
}); |
|
} |
|
} else { |
|
window.slideUp = function(target, duration = 500){ |
|
target.style.transitionProperty = 'height, margin, padding'; |
|
target.style.transitionDuration = duration + 'ms'; |
|
target.style.boxSizing = 'border-box'; |
|
target.style.height = target.offsetHeight + 'px'; |
|
target.offsetHeight; |
|
target.style.overflow = 'hidden'; |
|
target.style.height = 0; |
|
target.style.paddingTop = 0; |
|
target.style.paddingBottom = 0; |
|
target.style.marginTop = 0; |
|
target.style.marginBottom = 0; |
|
window.setTimeout( () => { |
|
target.style.display = 'none'; |
|
target.style.removeProperty('height'); |
|
target.style.removeProperty('padding-top'); |
|
target.style.removeProperty('padding-bottom'); |
|
target.style.removeProperty('margin-top'); |
|
target.style.removeProperty('margin-bottom'); |
|
target.style.removeProperty('overflow'); |
|
target.style.removeProperty('transition-duration'); |
|
target.style.removeProperty('transition-property'); |
|
}, duration); |
|
} |
|
window.slideDown = function(target, duration = 500){ |
|
target.style.removeProperty('display'); |
|
let display = window.getComputedStyle(target).display; |
|
|
|
if (display === 'none') |
|
display = 'block'; |
|
|
|
target.style.display = display; |
|
let height = target.offsetHeight; |
|
target.style.overflow = 'hidden'; |
|
target.style.height = 0; |
|
target.style.paddingTop = 0; |
|
target.style.paddingBottom = 0; |
|
target.style.marginTop = 0; |
|
target.style.marginBottom = 0; |
|
target.offsetHeight; |
|
target.style.boxSizing = 'border-box'; |
|
target.style.transitionProperty = "height, margin, padding"; |
|
target.style.transitionDuration = duration + 'ms'; |
|
target.style.height = height + 'px'; |
|
target.style.removeProperty('padding-top'); |
|
target.style.removeProperty('padding-bottom'); |
|
target.style.removeProperty('margin-top'); |
|
target.style.removeProperty('margin-bottom'); |
|
window.setTimeout( () => { |
|
target.style.removeProperty('height'); |
|
target.style.removeProperty('overflow'); |
|
target.style.removeProperty('transition-duration'); |
|
target.style.removeProperty('transition-property'); |
|
}, duration); |
|
} |
|
window.slideToggle = function(target, duration = 500){ |
|
if (window.getComputedStyle(target).display === 'none') { |
|
slideDown(target, duration); |
|
} else { |
|
slideUp(target, duration); |
|
} |
|
} |
|
} |