首页 > 其他 > 详细

瀑布流(基于Django)

时间:2019-02-03 21:59:58      阅读:297      评论:0      收藏:0      [点我收藏+]
# 后端
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from app01 import models
import json
from django.db.models import Q

def imgs(request):
    return render(request, img.html)


def get_imgs(request):
    nid = request.GET.get(nid)
    img_list = models.Img.objects.filter(Q(id__gt=nid)&Q(id__lt=(nid+7))).values(id, src, title) # 一次从数据库取7张图片
    img_list = list(img_list)
    ret = {
        status: True,
        data: img_list
    }
    return JsonResponse(ret)
# HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .w {
            width: 1000px;
            margin: 0 auto;
        }

        .item {
            width: 25%;
            float: left;
        }

        .item img {
            width: 100%;
        }
    </style>
</head>
<body>
<div>姑娘们</div>
<div class="w" id="container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
<script src="/static/jquery-1.12.4.js"></script>
<script>
    $(function () {
        var obj = new ScrollImg();
        obj.fetchImg();
        obj.scrollEvent();

    });
    function ScrollImg() {
        this.NID = 0;
        this.LASTPOSITION = 3;
        this.fetchImg = function () {
            var that = this;
            $.ajax({
                url: /get_imgs.html,
                type: GET,
                data: {nid: that.NID},
                dataType: JSON,
                success: function (arg) {
                    var img_list = arg.data;
                    $.each(img_list, function (index, v) {
                        var eqv = (index + that.LASTPOSITION + 1) % 4;
                        //console.log(eqv);
                        var tag = document.createElement(img);
                        tag.src = / + v.src;
                        $(#container).children().eq(eqv).append(tag);
                        if (index + 1 == img_list.length) {
                            that.LASTPOSITION = eqv;
                            that.NID = v.id;
                        }
                    })

                }

            })
        };
        this.scrollEvent = function () {
            var that = this;
            $(window).scroll(function () {
                var scrollTop = $(window).scrollTop();
                var winHeight = $(window).height();
                var docHeight = $(document).height();
                if (scrollTop + winHeight == docHeight) {
                    that.fetchImg();
                }
            })
        }

    }

</script>
</body>
</html>

 

瀑布流(基于Django)

原文:https://www.cnblogs.com/linyuhong/p/10351042.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!