1.2首页刷新以及点击排行

首先完成首页刷新的功能

就像下图一样能做到鼠标滑轮一直向下滑,页面能够刷新出来同时不刷新页面,新的新闻数据也能加载出来的效果

1、以下是后端的代码

 1 @index_blue.route("/news_list")
 2 def news_list():
 3     #接收参数
 4     cid = request.args.get("cid",1)
 5     page = request.args.get("page",1)
 6     per_page = request.args.get("per_age",10)
 7     #检测异常
 8     try:
 9         cid = int(cid)
10         page = int(page)
11         per_page = int(per_page)
12     except Exception as e:
13         current_app.logger.error(e)
14     #判断接收来的cid的值是否等于1
15     if cid == 1:
16     #在数据库中查找新闻并且以创建时间排序,用paginate函数接收
17         paginate = News.query.order_by(News.create_time.desc())
18             .paginate(page=page,per_page=per_page)
19     else:
20     #如果不是,就查找与前端接收到的cid相同的id的新闻
21         paginate = News.query.filter(News.category_id==cid).
22             order_by(News.create_time.desc()).
23             paginate(page=page,per_page=per_page)
24 
25     # 获取所有
26     newList = paginate.items
27     # 获取总页数
28     totalpage = paginate.pages
29     # 获取当前页数
30     currentPage = paginate.page
31     new_newList = []
32     #for循环遍历,用to_basic_dict()函数转换成字典类型
33     for new in newList:
34         new_newList.append(new.to_basic_dict())
35 
36     #传递上下文
37     context = {
38         "newList": new_newList,
39         "totalPage": totalpage,
40         "currentPage":currentPage
41     }
42     return jsonify(errno=RET.OK,errmsg="新闻获取成功" ,context=context)

2、下面是js的代码

  1 var currentCid = 1; // 当前分类 id
  2 var cur_page = 1; // 当前页
  3 var total_page = 1;  // 总页数
  4 var data_querying = true;   // 是否正在向后台获取数据:如果为ture表示正在加载数据;反之,没有加载数据
  5 
  6 
  7 $(function () {
  8 
  9     // 当主页加载完成之后,立即刷新主页的分页数据
 10     // 默认加载第一页
 11     updateNewsData();
 12 
 13     // 首页分类切换
 14     $('.menu li').click(function () {
 15         var clickCid = $(this).attr('data-cid')
 16         $('.menu li').each(function () {
 17             $(this).removeClass('active')
 18         })
 19         $(this).addClass('active')
 20 
 21         if (clickCid != currentCid) {
 22             // 记录当前分类id
 23             currentCid = clickCid;
 24 
 25             // 重置分页参数
 26             cur_page = 1;
 27             total_page = 1;
 28             updateNewsData()
 29         }
 30     });
 31 
 32     //页面滚动加载相关
 33     $(window).scroll(function () {
 34 
 35         // 浏览器窗口高度
 36         var showHeight = $(window).height();
 37 
 38         // 整个网页的高度
 39         var pageHeight = $(document).height();
 40 
 41         // 页面可以滚动的距离
 42         var canScrollHeight = pageHeight - showHeight;
 43 
 44         // 页面滚动了多少,这个是随着页面滚动实时变化的
 45         var nowScroll = $(document).scrollTop();
 46 
 47         if ((canScrollHeight - nowScroll) < 100) {
 48             // TODO 判断页数,去更新新闻数据
 49             if (!data_querying) {
 50                 // 表示正在加载数据
 51                 data_querying = true;
 52 
 53                 // 计算当前在第几页
 54                 cur_page += 1;
 55 
 56                 if (cur_page < total_page) {
 57                     // 加载指定页码的新闻数据
 58                     updateNewsData();
 59                 }
 60             }
 61         }
 62     })
 63 });
 64 
 65 function updateNewsData() {
 66     // TODO 更新新闻数据
 67     var params = {
 68         'cid':currentCid,
 69         'page':cur_page
 70         // 每页多少条不用传,默认10条
 71     };
 72 
 73     $.get('/news_list', params, function (response) {
 74         // 得到响应后,表示一次加载数据结束了
 75         data_querying = false;
 76 
 77         if (response.errno == '0') {
 78             // 记录总页数
 79             total_page = response.context.totalPage;
 80 
 81             if (cur_page == 1) {
 82                 $(".list_con").html("");
 83             }
 84 
 85             for (var i=0;i<response.context.newList.length;i++) {
 86                 var news = response.context.newList[i]
 87                 var content = '<li>'
 88                 content += '<a href="/news/detail/'+news.id+'" class="news_pic fl"><img src="' + news.index_image_url + '?imageView2/1/w/170/h/170"></a>'
 89                 content += '<a href="/news/detail/'+news.id+'" class="news_title fl">' + news.title + '</a>'
 90                 content += '<a href="/news/detail/'+news.id+'" class="news_detail fl">' + news.digest + '</a>'
 91                 content += '<div class="author_info fl">'
 92                 content += '<div class="source fl">来源:' + news.source + '</div>'
 93                 content += '<div class="time fl">' + news.create_time + '</div>'
 94                 content += '</div>'
 95                 content += '</li>'
 96                 $(".list_con").append(content);
 97             }
 98         } else {
 99             alert(response.errmsg);
100         }
101     });
102 }

3、然后就是点击排行的设置和代码 了

 1  <div class="conter_con">
 2         <ul class="list_con fl">
 3 
 4         </ul>
 5         <div class="rank_con fr">
 6             <div class="rank_title">
 7                 <h3>点击排行</h3>
 8             </div>
 9             <ul class="rank_list">
10                 {% for new in context.click_news %}
11                 <li><span class="{{ loop.index|rank }}">{{ loop.index }}</span>
12                     <a href="#">{{ new.title }}</a></li>
13                 {% endfor %}
14             </ul>
15         </div>
16     </div>

在11行中我们用了重定向,定向了我们自己在tools中创建的do_rank函数,这里rank是我们用了别名。

1 def do_rank(index):
2     if index == 1:
3         return "first"
4     elif index == 2:
5         return "second"
6     elif index == 3:
7         return "third"
8     else:
9         return ""
原文地址:https://www.cnblogs.com/Hdwmsyqdm/p/13916018.html