比如要实现一个媒体图片下拉加载更多的功能
主要看JS代码
后台代码
效果如下
主要看JS代码
- var winH = $(window).height(); //页面可视区域高度
- var i = 1;
- var max = ${attachments.pages};//总页数
- $(window).scroll(function () {
- var pageH = $(document.body).height();
- var scrollT = $(window).scrollTop(); //滚动条top
- var aa = (pageH - winH - scrollT) / winH;
- if (aa < 0.02 && i < max) {
- i++;
- $("#pageNum").html(i);
- var keywords = $('#keywords').val();
- $.ajax({
- type: 'GET',
- url: '/admin/attachments/ajax?page=' + i + '&keywords=' + keywords,
- success: function (data) {
- if (data.code == 1) {
- console.log(data);
- var str = '';
- $.each(data.result, function (index, value) {
- str += '<div class="col-lg-2 col-md-3 col-sm-6 col-xs-6 div-thumbnail"\n' +
- ' onclick="openDetail(' + value.attachId + ')">\n' +
- ' <a href="#" class="thumbnail">\n' +
- ' <img src="' + value.attachSmallPath + '" class="img-responsive"\n' +
- ' title="' + value.attachName + '">\n' +
- ' </a>\n' +
- '</div>';
- });
- console.log(str);
- $("#attachment-box").append(str);
- } else {
- showMsg(data.msg, "error", 1000);
- }
- }
- })
- }
- });
后台代码
- @GetMapping(value = "/ajax", produces = {"application/json;charset=UTF-8"})
- @ResponseBody
- public JsonResult listAttachmentByAjax(@RequestParam(value = "keywords", required = false) String keywords,
- @RequestParam(value = "page", defaultValue = "1") Integer page,
- @RequestParam(value = "size", defaultValue = "18") Integer size) {
- HashMap<String, Object> criteria = new HashMap<>();
- if (!Strings.isNullOrEmpty(keywords)) {
- criteria.put("keywords", keywords);
- }
- Page pageable = new Page(page, size);
- Page<Attachment> attachments;
- try {
- attachments = attachmentService.findAllAttachments(criteria, pageable);
- } catch (Exception e) {
- log.error("附件获取失败:{}", e.getMessage());
- return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.query-failed"));
- }
- return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.query-success"), attachments.getRecords());
- }
效果如下
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏