kevin man kevin man - 1 month ago 17
CSS Question

Jquery load more content divs

Basically trying to use Jquery to hide a chunk of divs, and using a button to show more. However since my divs are inline-block, they seem to not be hidden, and i have no idea why. Just trying to make a load more button to show more divs, but till then only the first 8 divs are to be shown, the rest should be hidden. Similar to the older post button on this website: https://melodydemo.wordpress.com/

Here is my Jquery for hiding a showing more divs:

$(function () {
$("div").slice(0, 8).show();
$("#loadMore").on('click', function (e) {
e.preventDefault();
$("div:hidden").slice(0, 8).slideDown();
if ($("div:hidden").length == 0) {
$("#load").fadeOut('slow');
}
$('html,body').animate({
scrollTop: $(this).offset().top
}, 1500);
});
});

$('a[href=#top]').click(function () {
$('body,html').animate({
scrollTop: 0
}, 600);
return false;
});

$(window).scroll(function () {
if ($(this).scrollTop() > 50) {
$('.totop a').fadeIn();
} else {
$('.totop a').fadeOut();
}
});


Heres the codepen of the my attempt: https://codepen.io/kastex/pen/RowYLL

Answer

Live view with Jsfiddle

Live view with codepen

What You need to Add and Removed

Removed CSS : From divstyle

display: inline-block;

And

ADD CSS : add new Style with a display class.

div.display {
    display: inline-block;
}

JavaScripts:

Removed JS : from line number 2 & 3.

.show(); & .slideDown();

And

ADD JS : Add this Line number 2 & 3 Both .

.addClass('ClassName')

$(function () {
    $("div").slice(0, 8).addClass('display');
    $("#loadMore").on('click', function (e) {
        e.preventDefault();
        $("div:hidden").slice(0, 8).addClass('display');
        if ($("div:hidden").length == 0) {
            $("#load").fadeOut('slow');
        }
        $('html,body').animate({
            scrollTop: $(this).offset().top
        }, 1500);
    });
});

$('a[href=#top]').click(function () {
    $('body,html').animate({
        scrollTop: 0
    }, 600);
    return false;
});

$(window).scroll(function () {
    if ($(this).scrollTop() > 50) {
        $('.totop a').fadeIn();
    } else {
        $('.totop a').fadeOut();
    }
});
  
  
  
body {
    background-color: #f6f6f6;
    width: 400px;
    margin: 20px auto;
    font: normal 13px/100% sans-serif;
    color: #444;
}
div {
    display:none;
    padding: 10px;
    border-width: 0 1px 1px 0;
    border-style: solid;
    border-color: #fff;
    box-shadow: 0 1px 1px #ccc;
    margin-bottom: 5px;
    background-color: #f1f1f1;
	/*display: inline-block;*/
}
div.display {
	display: inline-block;
}
.totop {
    position: fixed;
    bottom: 10px;
    right: 20px;
}
.totop a {
    display: none;
}
a, a:visited {
    color: #33739E;
    text-decoration: none;
    display: block;
    margin: 10px 0;
}
a:hover {
    text-decoration: none;
}
#loadMore {
    padding: 10px;
    text-align: center;
    background-color: #33739E;
    color: #fff;
    border-width: 0 1px 1px 0;
    border-style: solid;
    border-color: #fff;
    box-shadow: 0 1px 1px #ccc;
    transition: all 600ms ease-in-out;
    -webkit-transition: all 600ms ease-in-out;
    -moz-transition: all 600ms ease-in-out;
    -o-transition: all 600ms ease-in-out;
}
#loadMore:hover {
    background-color: #fff;
    color: #33739E;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Content 1</div>
<div>Content 2</div>
<div>Content 3</div>
<div>Content 4</div>
<div>Content 5</div>
<div>Content 6</div>
<div>Content 7</div>
<div>Content 8</div>
<div>Content 9</div>
<div>Content 10</div>
<div>Content 11</div>
<div>Content 12</div>
<div>Content 13</div>
<div>Content 14</div>
<div>Content 15</div>
<div>Content 16</div>
<div>Content 17</div>
<div>Content 18</div>
<div>Content 19</div>
<div>Content 20</div>

<div>Content 21</div>
<div>Content 22</div>
<div>Content 23</div>
<div>Content 24</div>

<div>Content 25</div>
<div>Content 26</div>
<div>Content 27</div>
<div>Content 28</div>

<div>Content 29</div>
<div>Content 30</div>
<div>Content 31</div>
<div>Content 32</div>

<div>Content 33</div>
<div>Content 34</div>
<div>Content 35</div>
<div>Content 36</div>


<a href="#" id="loadMore">Load More</a>

<p class="totop"> 
    <a href="#top">Back to top</a> 
</p>

Comments