Infinite Scroll To Load More Content in Webpage Using Ajax, and Jquery

前端之家收集整理的这篇文章主要介绍了Infinite Scroll To Load More Content in Webpage Using Ajax, and Jquery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
are on facebook Share on twitter Share on Share on pinterest_share e Sharing Service


Now a days in most all shopping website like flipkart or any other,you can see that product or data is loading in to website when you scroll mouse.

You want to know how its possible with PHP,well here is the simple example that how you can add infinite load content into your webpage,

Take a look at simple code and you can also download code,

How it works

I’ve created a little plugin that allows us to accomplish our goal. Simply put the plugin checks if the user is at the bottom of the container you specify and loads more content accordingly. Then this data is sent to an ajax file which processes what posts to show.

The key line here is

This is basically saying,if the user scroll position is greater than the height of the element targeted,then more elements should be loaded.

What is Ajax?

Ajax is how we send data to files when an event happens in javascript. For example,when we scroll we want to send some data to another file to figure out what to do. That file is usually a PHP file which willprocessthe data sent,and then you can do something like grab information from a database. So how do we do this with jQuery? Since we’re posting data,we use the$.postfunction. It looks a little like this:

So in the above example we end the information (thats the bit inside the first set of curly brackets) to the file,file.PHP. When the information is sent it will return some information in the form of a data variable,and we can then use that data to return information to the user via Javascript.

The ajax file

The ajax file is going to have to be customized to fit your needs. All you have to do is grab some information from the MysqL database of your choice with some PHP. I have created a very basic ajax.PHP file that grabs information from a wordpress MysqL database and displays the content with the title and link. It looks a little like this,but it will be included with the download files in the download link above.

<?PHP
MysqL_connect ( 'localhost' , 'username' , 'password' ) or die ( ) ;
MysqL_select_db ( 'database' ) ;
$offset = is_numeric ( $_POST [ 'offset' ] ) ? $_POST [ 'offset' ] : die ( ) ;
$postnumbers = is_numeric ( $_POST [ 'number' ] ) ? $_POST [ 'number' ] : die ( ) ;
$run = MysqL_query ( "SELECT * FROM content WHERE status = 'active' ORDER BY id DESC LIMIT " . $postnumbers . " OFFSET " . $offset ) ;
while ( $row = MysqL_fetch_array ( $run ) ) {
$content = substr ( strip_tags ( $row [ 'content' ] ) , 0 , 500 ) ;
echo '<h1><a href="' . $row [ 'id' ] . '">' . $row [ 'title' ] . '</a></h1><hr />' ;
echo '<p>' . $content . '...</p><hr />' ;
}
?>

Using the Plugin

Once you have your ajax file sorted out,its just a case of running the plugin. There are a bunch of variables you can determine,but it’s important that you define the query if you’re using the original ajax.PHP file listed above.

To run the plugin just upload all the files and create a container in your HTML called#contentor whatever you want to call your container. Then run the plugin on that. Easy!

$ ( document ) . ready ( function ( ) {
$ ( '#content' ) . scrollPagination ( {
nop : 10 , // The number of posts per scroll to be loaded
offset: 0 , // Initial offset,begins at 0 in this case
error : 'No More Posts!' , // When the user reaches the end this is the message that is
// displayed. You can change this if you want.
delay : 500 , // When you scroll down the posts will load after a delayed amount of time.
// This is mainly for usability concerns. You can alter this as you see fit
scroll : true // The main bit,if set to false posts will not load as the user scrolls.
// but will still load if the user clicks.
} ) ;
} ) ;

In the download you’ll find the files necessary to do everything I’ve shown you. Since I can’t include database details you will have to supply your own which you can edit in the ajax.PHP file. And that’s it! Have a good day!

原文链接:https://www.f2er.com/ajax/164830.html

猜你在找的Ajax相关文章