本文實(shí)例為大家分享了js實(shí)現(xiàn)瀑布流效果的具體代碼,供大家參考,具體內(nèi)容如下
前端內(nèi)容:
使用JavaScript和四個(gè)div,將照片放入四個(gè)div中
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> .container { width: 1000px; margin: 0 auto; background-color: lightgray; } .item { width: 24%; margin-right: 10px; float: left; } .item img{ width: 100%; } </style></head><body>{#將內(nèi)容放在container中#}<div class="container">{# 將圖片內(nèi)容放入四個(gè)item中,形成四個(gè)item#} <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div><script src="/static/js/jquery-2.1.4.min.js"></script><script> $(function () {{# 網(wǎng)頁(yè)加載時(shí)自動(dòng)執(zhí)行#} var obj = new ScrollImg(); obj.fetchImg(); obj.scrollEvent(); }) {# 定義對(duì)象#} function ScrollImg() { this.nid = 0;{# 取照片方法#} this.fetchImg = function () { var that = this $.ajax({ url: '/get_imgs.html', type: 'GET',{# 傳輸數(shù)據(jù),已經(jīng)取了多少照片,后臺(tái)可以依據(jù),繼續(xù)取照片#} data: {'nid': that.nid}, dataType: 'JSON', success: function (args) { if (args.status) { var img_list = args.data; $.each(img_list, function (index, obj) { var eqv = that.nid % 4; var tag = document.createElement('img') tag.src = '/' + obj.img_dir; console.log(eqv) $('.container').children().eq(eqv).append(tag) that.nid += 1; }) } } }) }{# 監(jiān)聽滾動(dòng)條,當(dāng)滾到底部時(shí),自動(dòng)加載數(shù)據(jù)#} this.scrollEvent = function () { var that = this; $(window).scroll(function () { var srollTop = $(window).scrollTop(); var winHeight = $(window).height(); var docHeight = $(document).height(); if (srollTop + winHeight >= docHeight - 2) { that.fetchImg(); } }) } }</script></body></html>
后臺(tái)內(nèi)容:
基于Django的FBV,函數(shù)視圖,進(jìn)行數(shù)據(jù)的讀取和處理ajax請(qǐng)求
def get_imgs(request): # 獲取已經(jīng)取得的照片數(shù)目 index = request.GET.get('nid') #獲取QuerySet集合對(duì)象,取從index后的10調(diào)數(shù)據(jù) imgs_list = models.Student.objects.values('id','img_dir','name')[index:index+10] imgs_list = list(imgs_list) # 傳送狀態(tài)和數(shù)據(jù)內(nèi)容 ret = { 'status':True, 'data':imgs_list } return JsonResponse(ret)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注