Flask + Ajax + Mysql 实现网页异步加载(二)

Flask + Ajax + Mysql 实现网页异步加载(一)

二、jquery 和Ajax 实现前端请求

<script src="static/js/jquery.min.js"  >
</script>

<script type="text/javascript">
    var pk = 1;
    $(window).scroll(function () {
        //下面这句主要是获取网页的总高度,主要是考虑兼容性所以把Ie支持的documentElement也写了,这个方法至少支持IE8
        var htmlHeight = $(document).height();
        //clientHeight是网页在浏览器中的可视高度,
        var clientHeight = $(window).height();
        //scrollTop滚动条到顶部的垂直高度
        var scrollTop = $(document).scrollTop();
        //通过判断滚动条的top位置与可视网页之和与整个网页的高度是否相等来决定是否加载内容;
        var he = scrollTop + clientHeight;
        if (he == htmlHeight ) {
            pk = pk +1; //每次和后端交互,page+1。
            addListMore();
        }
        if (scrollTop <=0){
            refresh();
        }
        //console.log("滚动条位置:" + scrollTop);
        //console.log("可视高度:" + clientHeight);
        //console.log("网页总高度" + htmlHeight);
    });
    function addListMore() {
        //console.log("加载更多");
        $.ajax({
            type:"GET",
            url:"/?page="+pk,
            dataType:"html",
            success:function (data) {
                //var $data = $(data);
                //var target_div = $data.find("#div1");
                //$("body").append(target_div);
                $("#div2").empty();
                var div = document.createElement("div");
                document.body.appendChild(div);
                div.innerHTML = data;

            }

        })
    }
    function refresh() {
        $.ajax({
            type:"GET",
            url:"/",
            dataType:"html",
            success:function () {
                window.location.reload();
                //location.href = url + "/teacherList";


            }
        })

    }
</script>

主要有三个问题:

1、判断滚动位置+可视高度=网页总高度。

2、如何改变url的page参数.

3、ajax 的success:function(data){ }    如何把服务器端发送过来的html加载到当前页面的后面。   

var div = document.createElement("div");
document.body.appendChild(div);
div.innerHTML = data;

    这三句实现了把data,也就是返回的index.html 作为div显示在当前网页上。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值