Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincentntang/6bfb037308989a034a93adfaf2f5435b to your computer and use it in GitHub Desktop.
Save vincentntang/6bfb037308989a034a93adfaf2f5435b to your computer and use it in GitHub Desktop.
Flexbox Hide Overflow Child Elements Pt2
section
p There are 13 boxes here, 3 of them are hidden using `overflow: hidden` and max-height
p Show more/Show less button only appears on element overflow
radio
.wrapper
.grid-parent
- for (var x = 0; x < 13; x++)
.grid-item
.btn-expand Show More
// Overflow boolean checker
function isOverflown(element){
return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}
// Jquery Toggle Text Plugin
$.fn.toggleText = function(t1, t2){
if (this.text() == t1) this.text(t2);
else this.text(t1);
return this;
};
// Init
$(document).ready(function() {
const parent = document.querySelector('.grid-parent');
// Show if overflowed
if(isOverflown(parent)){
$(".btn-expand").addClass("btn-show");
}
// Toggle Overflow
$(".btn-expand").on("click",function(){
$(".grid-parent").toggleClass("grid-parent--showall")
$(".btn-expand").toggleText("Show More", "Show Less");
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
body {
background-color: #EEF0ED;
}
.grid-parent2,
.grid-parent {
width: 500px;
background-color: lightgrey;
display: flex;
flex-wrap: wrap;
overflow: hidden;
max-height: 200px;
// height: 200px;
position: relative;
&--showall{
// height: auto;
max-height: none;
// overflow: none; // Not necessary
}
}
.grid-item{
background-color: blue;
width: 100px;
height: 100px;
box-sizing: border-box;
border: 1px solid red;
&:nth-of-type(even){
background-color: lightblue;
}
}
.btn-expand {
display: none;
z-index: 3;
position: absolute;
right: 0px;
bottom: 0px;
padding: 3px;
background-color: red;
color: white;
}
.btn-show {
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment