如何解决BigCommerce分类页面Infinite Scroll添加无效问题
Hey there, let's work through getting that infinite scroll up and running on your BigCommerce category pages. It sounds like the main issue is ensuring your script runs at the right time, especially with how BigCommerce's template injection works. Here's a step-by-step breakdown to fix this:
1. First, Confirm jQuery Availability & Script Loading Order
BigCommerce typically loads jQuery by default, but double-check it's available before your script runs. Open your browser's dev tools (F12), go to the Console tab, and type $ or jQuery—you should see a function returned, not an error.
Place your custom script right before the closing </body> tag in base.html—this ensures it loads after the DOM and jQuery are ready. If you're loading it via WebDAV, make sure the script path in your <script src="..."></script> tag is correct (check the Network tab for 404 errors if it's not loading).
2. Use BigCommerce's Stencil Load Event (Critical for AJAX Navigation)
Since category pages are injected into base.html (and users might navigate to them via AJAX without a full page reload), $(document).ready() might not fire reliably. Instead, use BigCommerce's stencil:load event, which triggers whenever a new template is rendered:
$(document).on('stencil:load', function() { // Only run this on category pages if (!$('body').hasClass('category')) return; // Set up infinite scroll variables let currentPage = 1; const productContainer = $('.productGrid'); // Adjust this selector to match your template const paginationElement = $('.pagination'); // Adjust this too const loadingIndicator = $('<div class="infinite-scroll-loader">Loading more products...</div>').hide(); // Add loading indicator to the page productContainer.after(loadingIndicator); // Hide default pagination initially paginationElement.hide(); // Scroll listener $(window).on('scroll.infiniteScroll', function() { // Trigger load when we're 200px from the bottom of the page if ($(window).scrollTop() + $(window).height() >= $(document).height() - 200) { loadingIndicator.show(); currentPage++; // Build URL for next page (adjust the "page" parameter if your template uses something else) const nextPageUrl = `${window.location.origin}${window.location.pathname}?page=${currentPage}`; // Fetch next page content $.get(nextPageUrl, function(response) { // Extract new product items from the response HTML const newProducts = $(response).find('.productGrid-item'); // Match your product item selector // Add new products to the container productContainer.append(newProducts); loadingIndicator.hide(); // Stop listening if no more products are returned if (newProducts.length === 0) { $(window).off('scroll.infiniteScroll'); loadingIndicator.text('No more products to load.'); } }).fail(function() { loadingIndicator.hide(); console.error('Failed to load additional products.'); }); } }); });
3. Verify Selectors Match Your Template
The biggest gotcha here is using the correct CSS selectors for your specific BigCommerce theme. Right-click on your product grid and pagination in the browser, select "Inspect", and update the selectors in the script (like .productGrid, .productGrid-item, .pagination) to match what your theme uses.
4. Debug with Browser Dev Tools
If it's still not working:
- Check the Console tab for errors (e.g., "jQuery is not defined", "Cannot read property 'append' of undefined")—these will point you to exactly what's broken.
- Use the Network tab to confirm the
$.getrequest is firing and returning valid HTML for the next page. - Test the next page URL directly in your browser to ensure it loads products correctly.
Quick Notes
- If your theme uses a different pagination parameter (some use
page_numberinstead ofpage), adjust thenextPageUrlaccordingly. - You can style the
.infinite-scroll-loaderclass in your CSS to make it fit your store's design.
内容的提问来源于stack exchange,提问作者Christian Dicker

