I have above code to loop all the blog data to generate the ui.
now I want to add the load more function when user scroll down until the end.
let say the blogs data split already and stored in $blog_batches_json, can you rewrite the part to render the blog card with above html?
Assistant
User
Assistant
User
請加入「加載中」的提示以當向下滾動時若需載入更多數據時標示
Assistant
User
我現在需要新增一個Loading Leading Overlay, 於網頁加載時顯示,中間可放置網頁的LOGO,當加載完畢後將消失。
請提供相關程式碼,並為Responsive Design for pc, tablet and mobile
為免衝突,element的id及class請加上 loading-modal 的前綴
我現在需要新增一個Loading Leading Overlay, 於網頁加載時顯示,中間可放置網頁的LOGO,當加載完畢後將消失。
請提供相關程式碼,並為Responsive Design for pc, tablet and mobile
為免衝突,element的id及class請加上 loading-modal 的前綴
Assistant
以下是你需要的「Loading Leading Overlay」完整範例,所有相關的 id/class 均加上 loading-modal 前綴以避免衝突,並且包含響應式設計(PC、Tablet、Mobile):
1. HTML 放在 <body> 頂部(或最合適的位置)
html
Certainly! To implement the "Load More on Scroll" feature using your existing $blog_batches_json which contains the blogs split into batches, you can do the following:
Approach:
Render only the first batch of blog cards initially in PHP.
Pass the json encoded batches to JavaScript.
Use JavaScript to listen for scroll events and append the next batch of blog cards dynamically when the user scrolls near the bottom.
Stop loading more when all batches are rendered.
Here is a complete rewrite of your blog rendering section with the infinite scroll load more functionality.
php
<?php
$all_blogs = $blogs['data'];
$batch_size = 5;
$blog_batches = array_chunk($all_blogs, $batch_size);
$blog_batches_json = json_encode($blog_batches);
?>
<div id="blogs-container" class="row w-100" style="margin-left: 0px;">
<?php
// Render the first batch of blogs only
if (!empty($blog_batches[0])) {
foreach ($blog_batches[0] as $row) { ?>
<div class="col-md-6 mb-4 blog-grid-custom">
<div class="blog-card">
<div class="blog-card-img" style="height: 100% !important;">
<a href="<?= base_url("blogs/view_detail/" . $row['slug']) ?>">
<img src="<?= strpos($row['image'], "base64") !== false ? $row['image'] : base_url($row['image']) ?>" alt="Card image">
</a>
</div>
<div class="card-body" style="padding: 1.25rem 1.25rem 0rem 1.25rem !important;">
<h2 class="blog-title mb-2" style="white-space: unset !important; text-overflow: unset !important;">
<a class="link-dark text-decoration-none" href="<?= base_url("blogs/view_detail/" . $row['slug']) ?>"><?= htmlspecialchars($row['title']) ?></a>
</h2>
<p style="text-align: right; margin: unset !important;color: #1f98f0">
<?= date('Y年 m月 d日', isset($row['held_date']) ? strtotime($row['held_date']) : strtotime($row['date_added'])) ?>
</p>
</div>
</div>
</div>
<?php
}
}
?>
</div>
<script>
// Parse PHP json batches into a JS variable
var blogBatches = <?= $blog_batches_json ?>;
// First batch is already loaded, start from second batch
var currentBatchIndex = 1;
var isLoading = false;
function createBlogCard(row) {
// Construct the HTML for a blog card, matching your PHP output
// Use template literals and sanitize content accordingly
// For dates, we can build a simple function here to format if needed or just output raw and format in PHP ideally.
// Format date in JS as fallback: YYYY年 MM月 DD日
function formatDate(dateString) {
var date = new Date(dateString);
if (isNaN(date)) return "";
var year = date.getFullYear();
var month = ("0" + (date.getMonth() +1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "年 " + month + "月 " + day + "日";
}
var imageSrc = row.image.includes("base64") ? row.image : ("<?= base_url('') ?>" + row.image);
var url = "<?= base_url('blogs/view_detail/') ?>" + row.slug;
var title = row.title.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
var dateToUse = row.held_date ? row.held_date : row.date_added;
var formattedDate = formatDate(dateToUse);
return `
<div class="col-md-6 mb-4 blog-grid-custom">
<div class="blog-card">
<div class="blog-card-img" style="height: 100% !important;">
<a href="${url}">
<img src="${imageSrc}" alt="Card image">
</a>
</div>
<div class="card-body" style="padding: 1.25rem 1.25rem 0rem 1.25rem !important;">
<h2 class="blog-title mb-2" style="white-space: unset !important; text-overflow: unset !important;">
<a class="link-dark text-decoration-none" href="${url}">${title}</a>
</h2>
<p style="text-align: right; margin: unset !important;color: #1f98f0">${formattedDate}</p>
</div>
</div>
</div>`;
}
function loadMoreBlogs() {
if (isLoading) return;
if (currentBatchIndex >= blogBatches.length) return; // no more batches
isLoading = true;
var container = document.getElementById('blogs-container');
var batch = blogBatches[currentBatchIndex];
// Create a document fragment to batch append
var fragment = document.createDocumentFragment();
batch.forEach(function(row){
// Create a temporary div to hold the HTML string
var tempDiv = document.createElement('div');
tempDiv.innerHTML = createBlogCard(row);
// tempDiv.children[0] is the blog card div with col-md-6 etc.
fragment.appendChild(tempDiv.children[0]);
});
container.appendChild(fragment);
currentBatchIndex++;
isLoading = false;
}
window.addEventListener('scroll', function() {
// If scrolled to within 100px of bottom, load more
if ((window.innerHeight + window.scrollY) >= (document.body.offsetHeight - 100)) {
loadMoreBlogs();
}
});
</script>
Explanation:
Initial PHP rendering:
Only the first batch of blogs is rendered server-side so the page loads immediately with some content.
JavaScript:
Receives the JSON arrays of blog batches.
Listens to scroll events.
When the user nears the bottom of the page, appends the next batch dynamically.
Batch control:
Uses currentBatchIndex to keep track of which batch is loaded next.
Stops loading once all batches are rendered.
Let me know if you need any improvements like adding a loading spinner or integrating with AJAX instead!
我發現載入速度比較慢。現時方案會在伺服器加載所有Blogs的數據:
public function index()
{