Answer the question
In order to leave comments, you need to log in
Delayed loading of blocks on the landing page?
Hello! I'm a coder in a company that develops landing pages. And analytics requires me to load sites in one or two seconds, which is almost impossible with designs like landing pages (a long, multi-block site that is replete with pictures and effects). And I decided to make a delayed loading of blocks.
Here is the code:
Репозиторий выглядит так:
project
|--build
| |--js
| |--scripts.js
|--parts
| |--file1.inc.php
| |--file3.inc.php
| |--...
|--index.php
HTML:
первый блок
второй блок
третий блок
<div class="ajax-load" id="file1"></div>
<div class="ajax-load" id="file2"></div>
JS:
// ******************* Ajax page partials load *******************
var ajaxPath = '../../parts/';
var ajaxExtension = '.inc.php';
var ajaxId = $('.ajax-load').first().attr('id');
//Инициализация при прокрутке
$("#load-start").waypoint(function(direction){
if(direction !== 'up'){
$('.ajax-load').first().load(
ajaxPath + ajaxId + ajaxExtension,
function( response, status, xhr ) {
if ( status == "error" ) {
var msg = "Произошла ошибка, пожалуйста обновите страницу!";
$( "#" + $(this).attr(id) ).html("<h2 class='ajax__error'>" + msg + "</h2>");
}else{
nextId = $(this).next('.ajax-load').attr('id');
ajaxLoader(nextId);
}
}
);
}
}, { offset: 600 });
//Рекурсивная функция подгрузки блоков
function ajaxLoader(id){
$('#' + id).load(
ajaxPath + id + ajaxExtension,
function( response, status, xhr ) {
if ( status == "error" ) {
var msg = "Произошла ошибка, пожалуйста обновите страницу!";
$( $(this) ).html("<h2 class='ajax__error'>" + msg + "</h2>");
}else{
nextId = $(this).next('.ajax-load').attr('id');
ajaxLoader(nextId);
}
}
);
}
// ******************* Ajax page partials load *******************
Answer the question
In order to leave comments, you need to log in
Although I’m not even a junior, you won’t save almost anything on loading blocks, but loading pictures has been completely postponed.
Google image lazy load
For example, here is a plugin
www.appelsiini.net/projects/lazyload
Why in your company the issue of site performance is only within the competence of the layout designer?
We load all the static at the end of the file. first, html will load, then css, js is connected, and in js it is already regulated which bibles and in what order to connect and on which pages. lazy loading needs to be done.
the option with loading the code as you have looks very sad (the idea itself)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question