Dynamically loading Justified gallery images with infinite scroll

I'm using jQuery Justified gallery with the inbuilt infinite scroll plugin.

http://miromannino.github.io 

This may be stupid question but how can i load the images dynamically with PHP.

I know how to do it with the below infinity scroll plugin but this plugin doesn't work with the infinity scroll plugin.

http://www.infinite-scroll.com/

Code

$('#gallery').justifiedGallery({rowHeight:120});


$(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() == $(document).height()) {
        for (var i = 0; i < 5; i++) {
            $('#gallery').append('<a>' +
                '<img src="http://path/to/image" />' + 
                '</a>');
        }
        $('#gallery').justifiedGallery('norewind');
    }
});

    $('#gallery').justifiedGallery({rowHeight:120});


    $(window).scroll(function(){
        if($(window).scrollTop() + $(window).height() == $(document).height()){
          //jquery ajax for dynemic loading images
          $.ajax({
               type:'post',//method can bet get,post

               url:'yourPHPFile.php',//url of your php file

               data:{"key":value},//if you want to send some data for query

               success:function(result){ //function call when successful response from server

                  var PhpImageArray=JSON.parse(result);
                   $.each(PhpImageArray, function(index, item) {
                        $('#gallery').append('<a>' +
                             '<img src="http://path/to/image"'+item.image+' />' + 
                              '</a>');
                   });
               }
          });

        $('#gallery').justifiedGallery('norewind');
        }
  });

phpfile.php

    <?php
     //array contain image object as 
     $img_array=array();

    //your database query
     $query=mysqli_query($DB_connect,"select imageName from ImageTable");       
     while($img=mysqli_fetch_array($query))
     {
       //object name with "image"
        $obj["image"]=$img["imageName"];

        //push object to arraay
        array_push($img_array,$obj);
     }

     //convert array in to json format for javascript use
     echo json_encode($img_array);
    ?>

You could count the amount of images by using Javascript

var offset = $('#gallery').children().length

Then you could make an ajax call to a given route (eg /giveImages) which returns an JSON-Array containing the image URL's

$.get('/giveImages?offset=' + offset, function(data) {
  // data = [ 
  //   'http://foo.com/image/3.jpg',                               
  //   'http://foo.com/image/4.jpg', 
  //   'http://foo.com/image/5.jpg'
  // ]

  // APPEND STUFF HERE AND justifyGallery 
})

Full example:

$(window).scroll(function() {

  if($(window).scrollTop() + $(window).height() == $(document).height()) {

    var offset = $('#gallery').children().length

    $.get('/giveImages?offset=' + offset, function(data) {

      for(var i = 0; i < data.length; i++) {

        $('#gallery').append(
            '<a>' +
              '<img src="' + data[i] + '" />' + 
            '</a>'
        )

        $('#gallery').justifiedGallery('norewind')

      }

    })
  }
}
链接地址: http://www.djcxy.com/p/36314.html

上一篇: 动态版本(使用+)Gradle中的依赖关系

下一篇: 用无限滚动动态加载对齐图库图像