Easy blog posting

I have a list of blog posts, and the number reaches 25+, but it's all on one page, so I need to try creating a lazy bootloader.

I tried various plugins but nobody works

http://jsfiddle.net/tara_irvine/S9GGz/6/

http://jsfiddle.net/tara_irvine/S9GGz/9/

http://jsfiddle.net/tara_irvine/S9GGz/13/

Is there a way to check the top value of the div and if it is in sight, then the class is added, so the div becomes visible (page loading is hidden)

I looked through these posts, but after testing various solutions, no one worked for me.

How to check if an element is in the user's view using jquery Div position relative to the top of the viewport

If anyone can shed light on this, I would be very grateful. I do not know where I am wrong.

Many thanks

+6
source share
5 answers

jQuery Waypoints is a good plugin for responding to elements in the field of view; they even have an example of lazy loading .

+7
source

I do not know how your installation is, but I would suggest using jquery to find out the distance from the top of the page:

var scrollTop = $(window).scrollTop(), elementOffset = $('#my-element').offset().top, distance = (elementOffset - scrollTop); 

according to this article: Determine the distance from the top of the div to the top of the window with javascript

apply this to your 25th post by placing a numbered identifier or names on each (I assumed that the page was generated by PHP).

then use ajax to upload more blog posts when the distance reaches a certain amount.

EDIT: you can use jquery more than hide them:

 $(".element-class:gt(24)").css("display","none");​ 

here: http://api.jquery.com/gt-selector/

it’s just that if you scroll up, you can just set

 $("visible-element").css("display","block") 

EDIT 2: TRY THIS FIDDLE - http://jsfiddle.net/addiktedDesign/KjNnY/

+2
source

http://archive.plugins.jquery.com/project/lazyload is a list of lazy bootable plugins, but it is more for uploading images.

what you can try is that every element of the blog is hidden, with the exception of the first three, and then

+1
source

I tried to get the top of the element and display the content, when it is in the viewport, the content can just be hidden or loaded from an ajax call. Try this code. You can experiment with variable sensitivity to see what works best:

 <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script type="text/javascript"> var processScroll = true; var sensitivity = 10; function handleScroll() { if (processScroll) { processScroll = false; topHidden = $('.block_hidden:first').offset().top; if(($(window).scrollTop() + $(window).height() ) > (topHidden + sensitivity)) { console.log('show now'); $('.block_hidden:first').removeClass('block_hidden').addClass('block'); } } processScroll = true; } $(document).ready(function() { $(window).scroll(handleScroll); }); </script> <style> .block_hidden{ width:300px; background: green; margin:5px; visibility: hidden; height: 10px !important; } .block{ height: 200px; width:300px; background: green; margin:5px; } </style> </head> <body> <div class="block"></div> <div class="block"></div> <div class="block"></div> <div class="block_hidden"></div> <div class="block_hidden"></div> <div class="block_hidden"></div> <div class="block_hidden"></div> <div class="block_hidden"></div> <div class="block_hidden"></div> </body> </html> 
+1
source

You ask about lazy loading.

Well, the answer should include the server side. Your question does not indicate what type of server language you are using. In my answer, I will use some basic PHP code to mock random blog posts.

Lazy loading means that we only load what the user can see, and then load more data as needed.

Downloading data means - request from the server. This is usually AJAX , but not necessary. (although you can probably use AJAX). JQuery has a wonderful ajax interface .

The biggest question is what should provoke my next load - therefore, all the plugins.

I took the advice from Tgr and applied a lazy load with waypoints. I even used a waypoint tutorial for lazy loading , as suggested by Tgr (give Tgr more requests).

You can see my implementation on my website.

What I did was a false blog post with a changing title. Every time the user scrolls enough, I get more messages from the server.

I added a download link for my source so that you can download and start playing with it. just run main.html and you're good to go.

The more_posts.php file creates a lorem ipsum message with a random header. (I need some kind of fake content to have a scroll on the page).

Looks like this

 <h1> This is post number <?php echo uniqid("",true)?> </h1> <div style="color:red"> Lorem ipsum dolor .... Quisque ut pretium nibh. </div> <div style="color:blue"> Lorem ipsum dolor .... Quisque ut pretium nibh. </div> 

As you can see, the only PHP code I have is to add something random to the header.

This should look familiar to you since you already have 25+ posts on your blog.

The real logic lies in main.html - part of the HTML looks like this

 <section id="container"> </section> <footer> <nav> <ul> <!-- Hijack this link for the infinite scroll --> <li class="more"><a href="more_posts.php" title="Traditional navigation link">Next Page</a></li> </ul> </nav> </footer> 

The idea is that you have a section that contains the first few posts and gives you a scroll on the page. At the bottom, you have a more link within the footer , which when disabling JavaScript should act like a regular β€œnext” link.

Waypoint uses this link to launch the next download. Whenever the link is about to be shown on the screen, we will use ajax to automatically receive the next message.

So, the JavaScript part looks like this:

 $(document).ready(function() { function loadMorePosts( callback ){ $.get($('.more a').attr('href'), function(data) { $('#container').append($(data)); if ( typeof(callback) == "function" ){ callback(); } }) } loadMorePosts(); var $footer = $('footer'); var opts = { offset: '100%' }; $footer.waypoint(function(event, direction) { $footer.waypoint('remove'); loadMorePosts( function(){ $footer.waypoint(opts);} ); }, opts); }); 

The loadMorePosts function calls the $.get method, which is a simpler syntax for $.ajax({type:'GET' .. }) . It uses the same URL as the link href attribute. In my example, the href attribute points to "more_posts.php".

When my demo loads, the content is completely empty, so I go ahead and get the first post that I want to show. Then I tell the waypoint to listen to the footer - and whenever the footer gets closer, I go and get more messages.

There's the tricky part where I do $footer.waypoint('remove') and pass the callback to loadMorePosts , which again connects the waypoint to the footer. This is just technicality - the waypoint requires you to remove the trigger while you get more HTML, and your other page can play funny.

It is more or less what ..

I tried to make this as simple as possible, but this is a huge problem that should cover one answer. So let me know if I can do more to figure it out.

+1
source

Source: https://habr.com/ru/post/922702/


All Articles