/** * Simple Load More * * Version: 1.0.2 * Author: Zeshan Ahmed * Website: https://zeshanahmed.com/ * Github: https://github.com/zeshanshani/simple-load-more/ */ (function($) { $.fn.simpleLoadMore = function( options ) { // Settings. var settings = $.extend({ count: 5, btnHTML: '', item: '' }, options); // Variables var $loadMore = $(this); var index=1; var maxNum=0; var formData = new FormData(); formData.append('scode',options.scode); formData.append('num', 5); formData.append('page', index); $.ajax({ url: '/api.php/list?content=1', type: 'POST', cache: false, data: formData, processData: false, contentType: false }).done(function (res) { let resp=JSON.parse(res); maxNum = resp.rowtotal // Run through all the elements. $loadMore.each(function(i, el) { // Variables. var $thisLoadMore = $(this); var $items = $thisLoadMore.find(settings.item); var pcshow = '鏇村鏂伴椈 '; var btnHTML = options.type=='pc'?pcshow : '鏇村鏂伴椈 '; var $btnHTML = $(btnHTML); // Add classes $thisLoadMore.addClass('load-more'); $items.addClass('load-more__item'); // Add button. if ( maxNum> $items.length ) { $thisLoadMore.append( $btnHTML ); } $btn = $thisLoadMore.find( '.load-more__btn' ); // Check if button is not present. If not, then attach $btnHTML to the $btn variable. if ( ! $btn.length ) { $btn = $btnHTML; } if ( $items.length > settings.count ) { $items.slice(settings.count).hide(); } // Add click event on button. $btn.on('click', function(e) { e.preventDefault(); index=index+1; var $this = $(this); //console.log($this,$thisLoadMore,$thisLoadMore.children().length,$thisLoadMore.children(),'12') var respond = getMessage(index,options.scode); respond.data.forEach(function (item,indext){ if(options.type=='pc'){ $this .before(`
  • ${item.title}

    ${item.description}...

    ${item.date}

  • `) }else{ $this .before(`
  • ${item.title}

    ${item.date}
    鏆傛棤鍥剧墖
  • `) } }) setTimeout(()=>{ //console.log($this,$thisLoadMore,$thisLoadMore.children().length,$thisLoadMore.children(),'13') if ( $thisLoadMore.children().length-1>= maxNum ) { $this.remove(); } },0) }); }); }).fail(function (res) {console.log(res) }); } function getMessage(indexed,scode){ var formDatas = new FormData(); formDatas.append('scode',scode); formDatas.append('num', 5); formDatas.append('page', indexed); var result; $.ajax({ url: '/api.php/list?ico=1&content=1', type: 'POST', cache: false, data: formDatas, processData: false, contentType: false, async:false, }).done(function (res) { result=JSON.parse(res); }); return result; } }( jQuery ));