程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> js-Jquery 瀑布流應用,怎麼把它和數據庫對接

js-Jquery 瀑布流應用,怎麼把它和數據庫對接

編輯:編程綜合問答
Jquery 瀑布流應用,怎麼把它和數據庫對接
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title>jQuery Wookmark Plug-in Example</title>
  <meta name="description" content="An very basic example of how to use the Wookmark jQuery plug-in.">
  <meta name="author" content="Christoph Ono, Sebastian Helzle">

  <meta name="viewport" content="width=device-width,initial-scale=1">

  <!-- CSS Reset -->
  <link rel="stylesheet" href="../css/reset.css">

  <!-- Global CSS for the page and tiles -->
  <link rel="stylesheet" href="../css/main.css">

</head>

<body>

  <div id="container">
    <header>
      <h1>jQuery Wookmark Plug-in Example</h1>
      <p>Scroll down to see the infinite scroll effect.</p>
    </header>
    <div id="main" role="main">

      <ul id="tiles">
        <!-- These are our grid blocks -->
        <li><img src="../sample-images/image_1.jpg" width="200" height="283"><p>1</p></li>
        <li><img src="../sample-images/image_2.jpg" width="200" height="300"><p>2</p></li>
        <li><img src="../sample-images/image_3.jpg" width="200" height="252"><p>3</p></li>
        <li><img src="../sample-images/image_4.jpg" width="200" height="158"><p>4</p></li>
        <li><img src="../sample-images/image_5.jpg" width="200" height="300"><p>5</p></li>
        <li><img src="../sample-images/image_6.jpg" width="200" height="297"><p>6</p></li>
        <li><img src="../sample-images/image_7.jpg" width="200" height="200"><p>7</p></li>
        <li><img src="../sample-images/image_8.jpg" width="200" height="200"><p>8</p></li>
        <li><img src="../sample-images/image_9.jpg" width="200" height="398"><p>9</p></li>
        <li><img src="../sample-images/image_10.jpg" width="200" height="267"><p>10</p></li>
        <li><img src="../sample-images/image_1.jpg" width="200" height="283"><p>11</p></li>
        <li><img src="../sample-images/image_2.jpg" width="200" height="300"><p>12</p></li>
        <li><img src="../sample-images/image_3.jpg" width="200" height="252"><p>13</p></li>
        <li><img src="../sample-images/image_4.jpg" width="200" height="158"><p>14</p></li>
        <li><img src="../sample-images/image_5.jpg" width="200" height="300"><p>15</p></li>
        <li><img src="../sample-images/image_6.jpg" width="200" height="297"><p>16</p></li>
        <li><img src="../sample-images/image_7.jpg" width="200" height="200"><p>17</p></li>
        <li><img src="../sample-images/image_8.jpg" width="200" height="200"><p>18</p></li>
        <li><img src="../sample-images/image_9.jpg" width="200" height="398"><p>19</p></li>
        <li><img src="../sample-images/image_10.jpg" width="200" height="267"><p>20</p></li>
        <li><img src="../sample-images/image_1.jpg" width="200" height="283"><p>21</p></li>
        <li><img src="../sample-images/image_2.jpg" width="200" height="300"><p>22</p></li>
        <li><img src="../sample-images/image_3.jpg" width="200" height="252"><p>23</p></li>
        <li><img src="../sample-images/image_4.jpg" width="200" height="158"><p>24</p></li>
        <li><img src="../sample-images/image_5.jpg" width="200" height="300"><p>25</p></li>
        <li><img src="../sample-images/image_6.jpg" width="200" height="297"><p>26</p></li>
        <li><img src="../sample-images/image_7.jpg" width="200" height="200"><p>27</p></li>
        <li><img src="../sample-images/image_8.jpg" width="200" height="200"><p>28</p></li>
        <li><img src="../sample-images/image_9.jpg" width="200" height="398"><p>29</p></li>
        <li><img src="../sample-images/image_10.jpg" width="200" height="267"><p>30</p></li>

        <!-- End of grid blocks -->
      </ul>

    </div>
    <footer>

    </footer>
  </div>

  <!-- include jQuery -->
  <script src="../libs/jquery.min.js"></script>

  <!-- Include the imagesLoaded plug-in -->
  <script src="../libs/jquery.imagesloaded.js"></script>

  <!-- Include the plug-in -->
  <script src="../jquery.wookmark.js"></script>

  <!-- Once the page is loaded, initalize the plug-in. -->
  <script type="text/javascript">
    (function ($){
      var $tiles = $('#tiles'),
          $handler = $('li', $tiles),
          $main = $('#main'),
          $window = $(window),
          $document = $(document),
          options = {
            autoResize: true, // This will auto-update the layout when the browser window is resized.
            container: $main, // Optional, used for some extra CSS styling
            offset: 20, // Optional, the distance between grid items 圖片與圖片之間的間距
            itemWidth: 210 // Optional, the width of a grid item 太小的話圖片與圖片會疊加
          };

      /**
       * Reinitializes the wookmark handler after all images have loaded
       */
      function applyLayout() {
        $tiles.imagesLoaded(function() {
          // Destroy the old handler
          if ($handler.wookmarkInstance) {
            $handler.wookmarkInstance.clear();
          }

          // Create a new layout handler.
          $handler = $('li', $tiles);
          $handler.wookmark(options);
        });
      }

      /**
       * When scrolled all the way to the bottom, add more tiles
       */
      function onScroll() {
        // Check if we're within 100 pixels of the bottom edge of the broser window.
        var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix
            closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 100);

        if (closeToBottom) {
          // Get the first then items from the grid, clone them, and add them to the bottom of the grid
          var $items = $('li', $tiles),
              $firstTen = $items.slice(0, 10);
          $tiles.append($firstTen.clone());

          applyLayout();
        }
      };

      // Call the layout function for the first time
      applyLayout();

      // Capture scroll event.
      $window.bind('scroll.wookmark', onScroll);
    })(jQuery);
  </script>

</body>
</html>




圖片說明

我想要的瀑布流是從數據庫裡讀取,但網頁進來時不會顯示全部圖片,往下滑動時才會顯示其他圖片。而這種它一次就把所有圖片讀取,滑動到底部也只是重復一遍而已。

最佳回答:


一般瀑布流插件有配置支持動態從數據庫中讀取數據的,你認真看下API。

如果確認你的這個不支持動態讀取,只能換一個瀑布流插件了,如這個:

  (function ($) {
    var 
    //參數
   setting = {
       column_width: 240, //列寬
       column_className: 'waterfall_column', //列的類名
       column_space: 10, //列間距
       cell_selector: '.cell', //要排列的磚塊的選擇器,context為整個外部容器
       img_selector: 'img', //要加載的圖片的選擇器
       auto_imgHeight: true, //是否需要自動計算圖片的高度
       fadein: true, //是否漸顯載入
       fadein_speed: 600, //漸顯速率,單位毫秒
       insert_type: 1, //單元格插入方式,1為插入最短那列,2為按序輪流插入
       getResource: function (index) { }  //獲取動態資源函數,必須返回一個磚塊元素集合,傳入參數為加載的次數
   },
    //
   waterfall = $.waterfall = {}, //對外信息對象
   $waterfall = null; //容器
    waterfall.load_index = 0, //加載次數
   $.fn.extend({
       waterfall: function (opt) {
           opt = opt || {};
           setting = $.extend(setting, opt);
           $waterfall = waterfall.$waterfall = $(this);
           waterfall.$columns = creatColumn();
           render($(this).find(setting.cell_selector).detach(), false); //重排已存在元素時強制不漸顯
           waterfall._scrollTimer2 = null;
           $(window).bind('scroll', function () {
               clearTimeout(waterfall._scrollTimer2);
               waterfall._scrollTimer2 = setTimeout(onScroll, 300);
           });
           waterfall._scrollTimer3 = null;
           $(window).bind('resize', function () {
               clearTimeout(waterfall._scrollTimer3);
               waterfall._scrollTimer3 = setTimeout(onResize, 300);
           });
       }
   });
    function creatColumn() {//創建列
        waterfall.column_num = calculateColumns(); //列數
        //循環創建列
        var html = '';
        for (var i = 0; i < waterfall.column_num; i++) {
            html += '<div class="' + setting.column_className + '" style="width:' + setting.column_width + 'px; display:inline-block; *display:inline;zoom:1; margin-left:' + setting.column_space / 2 + 'px;margin-right:' + setting.column_space / 2 + 'px; vertical-align:top; overflow:hidden"></div>';
        }
        $waterfall.prepend(html); //插入列
        return $('.' + setting.column_className, $waterfall); //列集合
    }
    function calculateColumns() {//計算需要的列數
        var num = Math.floor(($waterfall.innerWidth()) / (setting.column_width + setting.column_space));
        if (num < 1) { num = 1; } //保證至少有一列
        return num;
    }
    function render(elements, fadein) {//渲染元素
        if (!$(elements).length) return; //沒有元素
        var $columns = waterfall.$columns;
        $(elements).each(function (i) {
            if (!setting.auto_imgHeight || setting.insert_type == 2) {//如果給出了圖片高度,或者是按順序插入,則不必等圖片加載完就能計算列的高度了
                if (setting.insert_type == 1) {
                    insert($(elements).eq(i), setting.fadein && fadein); //插入元素
                } else if (setting.insert_type == 2) {
                    insert2($(elements).eq(i), i, setting.fadein && fadein); //插入元素  
                }
                return true; //continue
            }
            if ($(this)[0].nodeName.toLowerCase() == 'img' || $(this).find(setting.img_selector).length > 0) {//本身是圖片或含有圖片
                var image = new Image;
                var src = $(this)[0].nodeName.toLowerCase() == 'img' ? $(this).attr('src') : $(this).find(setting.img_selector).attr('src');
                image.onload = function () {//圖片加載後才能自動計算出尺寸
                    image.onreadystatechange = null;
                    if (setting.insert_type == 1) {
                        insert($(elements).eq(i), setting.fadein && fadein); //插入元素
                    } else if (setting.insert_type == 2) {
                        insert2($(elements).eq(i), i, setting.fadein && fadein); //插入元素  
                    }
                    image = null;
                }
                image.onreadystatechange = function () {//處理IE等浏覽器的緩存問題:圖片緩存後不會再觸發onload事件
                    if (image.readyState == "complete") {
                        image.onload = null;
                        if (setting.insert_type == 1) {
                            insert($(elements).eq(i), setting.fadein && fadein); //插入元素
                        } else if (setting.insert_type == 2) {
                            insert2($(elements).eq(i), i, setting.fadein && fadein); //插入元素  
                        }
                        image = null;
                    }
                }
                image.src = src;
            } else {//不用考慮圖片加載
                if (setting.insert_type == 1) {
                    insert($(elements).eq(i), setting.fadein && fadein); //插入元素
                } else if (setting.insert_type == 2) {
                    insert2($(elements).eq(i), i, setting.fadein && fadein); //插入元素  
                }
            }
        });
    }
    function public_render(elems) {//ajax得到元素的渲染接口
        render(elems, true);
    }
    function insert($element, fadein) {//把元素插入最短列
        if (fadein) {//漸顯
            $element.css('opacity', 0).appendTo(waterfall.$columns.eq(calculateLowest())).fadeTo(setting.fadein_speed, 1);
        } else {//不漸顯
            $element.appendTo(waterfall.$columns.eq(calculateLowest()));
        }
    }
    function insert2($element, i, fadein) {//按序輪流插入元素
        if (fadein) {//漸顯
            $element.css('opacity', 0).appendTo(waterfall.$columns.eq(i % waterfall.column_num)).fadeTo(setting.fadein_speed, 1);
        } else {//不漸顯
            $element.appendTo(waterfall.$columns.eq(i % waterfall.column_num));
        }
    }
    function calculateLowest() {//計算最短的那列的索引
        var min = waterfall.$columns.eq(0).outerHeight(), min_key = 0;
        waterfall.$columns.each(function (i) {
            if ($(this).outerHeight() < min) {
                min = $(this).outerHeight();
                min_key = i;
            }
        });
        return min_key;
    }
    function getElements() {//獲取資源
        $.waterfall.load_index++;
        return setting.getResource($.waterfall.load_index, public_render);
    }
    waterfall._scrollTimer = null; //延遲滾動加載計時器
    function onScroll() {//滾動加載
        clearTimeout(waterfall._scrollTimer);
        waterfall._scrollTimer = setTimeout(function () {
            var $lowest_column = waterfall.$columns.eq(calculateLowest()); //最短列
            var bottom = $lowest_column.offset().top + $lowest_column.outerHeight(); //最短列底部距離浏覽器窗口頂部的距離
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0; //滾動條距離
            var windowHeight = document.documentElement.clientHeight || document.body.clientHeight || 0; //窗口高度
            if (scrollTop >= bottom - windowHeight) {
                render(getElements(), true);
            }
        }, 100);
    }
    function onResize() {//窗口縮放時重新排列
        if (calculateColumns() == waterfall.column_num) return; //列數未改變,不需要重排
        var $cells = waterfall.$waterfall.find(setting.cell_selector);
        waterfall.$columns.remove();
        waterfall.$columns = creatColumn();
        render($cells, false); //重排已有元素時強制不漸顯
    }
})(jQuery);

配置getResource即可。。自己用ajax讀出數據後調用render回調即可

    var totalpage = 1;//從數據庫中讀取總頁數賦值給這個js變量,如<%=你的服務器端變量名%>

     function getResource(index, render) { if (index + 1 > totalpage || opt.loading) return false; opt.loading = true;  $.ajax({ url: '動態頁地址', data: {  page: index + 1, pagesize: 8}, type: 'POST', complete: function (xhr) { opt.loading = false; $('#pLoading').hide(); render($(xhr.responseText)); } }); }
    var opt = { auto_imgHeight: true, insert_type: 1 };
    if (totalpage > 1) opt.getResource = getResource;
    $('#waterfall').waterfall(opt);
baidu_24884161
baidu_24884161
showbo
showbo
baidu_24884161
showbo
baidu_24884161
baidu_24884161
baidu_24884161
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved