当前位置:   article > 正文

第十四届蓝桥杯web第三次模拟_web蓝桥杯 模拟 下载

web蓝桥杯 模拟 下载

网页ppt

  1. const sectionsCount = $("section").length;
  2. let activeIndex = 0;
  3. // 监听用户按下空格和方向键的事件
  4. $(document).on("keydown", (e) => {
  5. e.preventDefault();
  6. if (e.key === " " || e.key === "ArrowRight") {
  7. goRight();
  8. }
  9. if (e.key === "ArrowLeft") {
  10. goLeft();
  11. }
  12. });
  13. // 监听按钮点击事件
  14. $(".btn.left").click(goLeft);
  15. $(".btn.right").click(goRight);
  16. // 切换下一张的函数
  17. function goLeft() {
  18. var list=document.querySelectorAll('section');
  19. for(i=1;i<list.length;i++)
  20. list[activeIndex].style.display="none";
  21. if (activeIndex === 4) {
  22. $(".btn.right").removeClass('disable')
  23. }
  24. if (activeIndex === 0) {
  25. return;
  26. }
  27. if (activeIndex === 1) {
  28. $(".btn.left").addClass('disable',true)
  29. }
  30. activeIndex -= 1;
  31. switchPage();
  32. }
  33. // 切换上一张的函数
  34. function goRight() {
  35. var list=document.querySelectorAll('section');
  36. for(i=1;i<list.length;i++)
  37. list[activeIndex].style.display="none";
  38. if (activeIndex === sectionsCount - 1) {
  39. return;
  40. }
  41. $(".btn.left").removeClass('disable')
  42. activeIndex += 1;
  43. if(activeIndex==4){
  44. $(".btn.right").addClass('disable',true)
  45. }
  46. switchPage();
  47. }
  48. function switchPage() {
  49. // TODO: 请补充该函数,实现根据activeIndex切换页面的功能,并且在到达最后一页或第一页时给相应的按钮添加disable类
  50. var list=document.querySelectorAll('section');
  51. // console.log(list)
  52. // console.log(activeIndex)
  53. list[activeIndex].style.display="block";
  54. $(".page").html(activeIndex+1+"/"+5);
  55. }

西游记之西天取经


  1. body {
  2. background: url(../images/background.webp) no-repeat;
  3. background-size: cover;
  4. overflow-y: hidden;
  5. }
  6. .playground {
  7. width: 800px;
  8. margin: 400px auto;
  9. overflow: hidden;
  10. }
  11. .actor {
  12. float: left;
  13. }
  14. .actor:first-child {
  15. width: 200px;
  16. height: 180px;
  17. background: url(../images/west_01.png) no-repeat;
  18. /* TODO 填空 */
  19. animation: a1 0.8s steps(8) infinite;
  20. }
  21. .actor:nth-child(2) {
  22. width: 200px;
  23. height: 180px;
  24. background: url(../images/west_02.png) no-repeat;
  25. /* TODO 填空 */
  26. animation: a2 0.8s steps(8) infinite;
  27. }
  28. .actor:nth-child(3) {
  29. width: 170px;
  30. height: 240px;
  31. background: url(../images/west_03.png) no-repeat;
  32. /* TODO 填空 */
  33. animation: a3 0.8s steps(8) infinite;
  34. }
  35. .actor:last-child {
  36. width: 210px;
  37. height: 200px;
  38. background: url(../images/west_04.png) no-repeat;
  39. /* TODO 填空 */
  40. animation: a4 0.8s steps(8) infinite;
  41. }
  42. @keyframes a1{
  43. from{
  44. background-position-x: 0;
  45. }
  46. to{
  47. background-position-x: -1600px;
  48. }
  49. }
  50. @keyframes a2{
  51. from{
  52. background-position-x: 0;
  53. }
  54. to{
  55. background-position-x: -1600px;
  56. }
  57. }
  58. @keyframes a3{
  59. from{
  60. background-position-x: 0;
  61. }
  62. to{
  63. background-position-x: -1360px;
  64. }
  65. }
  66. @keyframes a4{
  67. from{
  68. background-position-x: 0;
  69. }
  70. to{
  71. background-position-x: -1680px;
  72. }
  73. }

商品销量和销售额实时展示看板


  1. // 指定图表的配置项和数据
  2. const charData = {
  3. title: {
  4. text: '云课课程销量和销售额看板',
  5. width: 100,
  6. height: 50,
  7. textStyle: {
  8. lineHeight: 50,
  9. },
  10. left: 'center',
  11. },
  12. grid: {
  13. top: 80,
  14. },
  15. tooltip:{
  16. show: true,
  17. },
  18. xAxis: {
  19. data: [],
  20. },
  21. // TODO:补全 `yAxis` 的设置,要求“销售额”(即,配置项 `name`)的位置(即,配置项 `position`)在图表的左侧,“销量”(即,配置项 `name`)的位置(即,配置项 `position`)在图表的右侧。
  22. yAxis: [{
  23. type: 'value',
  24. name: '销售额',
  25. position: 'left',
  26. },
  27. {
  28. type: 'value',
  29. name: '金额',
  30. position: 'right',
  31. }],
  32. series: [
  33. {
  34. name: '销售额',
  35. type: 'line',
  36. data: [],
  37. yAxisIndex: 0,
  38. smooth: true
  39. },
  40. {
  41. name: '销量',
  42. type: 'bar',
  43. data: [],
  44. yAxisIndex: 1,
  45. smooth: true
  46. }
  47. ]
  48. };
  49. // 以下代码为模拟后端服务器返回数据
  50. let sale = 0;
  51. let count = 0;
  52. // 销售额
  53. const saleObj = {};
  54. // 销量
  55. const countObj = {};
  56. let index = 0;
  57. function Ajax() {
  58. return new Promise((resolve, reject) => {
  59. let randomNum = Math.random();
  60. const randomSum = () => (randomNum * 500 + 900);
  61. const randomCount = () => (randomNum * 50 + 80);
  62. let i = index++ * 10
  63. let key = `10:${i == 0 ? "00" : i}`;
  64. if (index < 7) {
  65. sale = randomSum();
  66. Object.assign(saleObj, { [key]: sale.toFixed(2) })
  67. count = randomCount();
  68. Object.assign(countObj, { [key]: count.toFixed(2) })
  69. }
  70. const respondBody = {
  71. "code": 200,
  72. "msg": "success",
  73. "data": {
  74. saleObj,
  75. countObj
  76. }
  77. };
  78. setTimeout(() => {
  79. resolve(respondBody);
  80. }, 1000)
  81. })
  82. }
  83. async function renderChart() {
  84. const result = await Ajax();
  85. document.querySelector("#result").innerText = JSON.stringify(result);
  86. const myChart = echarts.init(document.getElementById('main'));
  87. // TODO:补全代码,正确给 X 轴的时间,以及 Y 轴的商品的销售额 saleObj 和销量赋值 countObj。
  88. console.log( Object.values(saleObj))
  89. charData.series[0].data=Object.values(saleObj)
  90. charData.series[1].data=Object.values(countObj)
  91. charData.xAxis.data= (Object.keys(saleObj))
  92. console.log( charData.xAxis,"chartdata")
  93. myChart.setOption(charData, true);
  94. document.querySelector("#data").innerText = JSON.stringify(charData);
  95. }
  96. renderChart();
  97. let times = 0;
  98. let timer = setInterval(() => {
  99. renderChart();
  100. ++times == 6 && clearInterval(timer);
  101. }, 2000)

校园一卡通

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>蓝桥校园一卡通</title>
  7. <link rel="stylesheet" href="./css/bootstrap.min.css">
  8. <link rel="stylesheet" href="./css/index.css" />
  9. </head>
  10. <body>
  11. <div id="app">
  12. <div class="form">
  13. <div class="card" id="cardStyle">
  14. <div class="cardName">蓝桥校园一卡通</div>
  15. <div class="info">
  16. <div class="item"></div>
  17. <div class="item"></div>
  18. <div class="item"></div>
  19. </div>
  20. </div>
  21. <div class="content">
  22. <div class="form-group">
  23. <label for="studentName">姓名</label>
  24. <input type="text" class="form-control" id="studentName" placeholder="请输入姓名"
  25. aria-describedby="inputSuccess2Status">
  26. <span id="vail_name" style="display: none;" class="help-block"> 姓名是2-4个汉字,请您检查输入的内容</span>
  27. </div>
  28. <div class="form-group">
  29. <label for="studentId">学号</label>
  30. <input type="number" class="form-control" id="studentId" placeholder="请输入学号">
  31. <span id="vail_studentId" style="display: none;" class="help-block"> 学号是1-12位的数字,请您检查输入的内容</span>
  32. </div>
  33. <div class="form-group">
  34. <label for="college">学院</label>
  35. <select id="college" class="form-control">
  36. <option value="软件工程学院">软件工程学院</option>
  37. <option value="信息技术学院">信息技术学院</option>
  38. <option value="数字媒体学院">数字媒体学院</option>
  39. <option value="计算机科学学院">计算机科学学院</option>
  40. </select>
  41. </div>
  42. <button id="submit">制卡</button>
  43. </div>
  44. </div>
  45. </div>
  46. <script>
  47. // 获取DOM元素对象
  48. const studentName = document.getElementById("studentName"); // 姓名
  49. const formgroup = document.getElementsByClassName("form-group"); // 姓名
  50. const studentId = document.getElementById("studentId"); // 学号
  51. const college = document.getElementById("college"); // 学院
  52. const submit = document.getElementById("submit"); // 制卡按钮
  53. const cardStyle = document.getElementById("cardStyle"); // 卡片
  54. const item = document.querySelectorAll(".item") // 卡片项
  55. submit.onclick = () => {
  56. document.getElementById('vail_name').style.display = 'block'
  57. // console.log(item.length)
  58. // TODO 待补充代码
  59. var nameReg = /^[\u4e00-\u9fa5]{2,4}$/; // 2-4位的汉字名字
  60. console.log(studentName.value)
  61. var naR = nameReg.test(studentName.value)
  62. console.log(naR)
  63. var numberReg = /^\d{1,12}$/; // 2-4位的汉字名字
  64. var nR = numberReg.test(studentId.value);
  65. console.log(nR)
  66. if (!naR) {
  67. formgroup[0].classList.add("has-error")
  68. document.getElementById('vail_name').style.display = 'block'
  69. }
  70. if (!nR) {
  71. formgroup[1].classList.add("has-error")
  72. document.getElementById('vail_studentId').style.display = 'block'
  73. }
  74. if (naR && nR) {
  75. item[0].innerHTML = "姓名:" + studentName.value
  76. item[1].innerHTML = "学号:" + studentId.value
  77. item[2].innerHTML = "学院:" + college.value
  78. // 添加 showCard 类显示放大一卡通的动画,请勿删除
  79. cardStyle.classList.add('showCard')
  80. }
  81. }
  82. </script>
  83. </body>
  84. </html>

心愿便利贴

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>心愿便利贴</title>
  6. <!-- 引入element-ui样式 -->
  7. <link rel="stylesheet" href="css/index.css">
  8. <link rel="stylesheet" href="./css/wish.css">
  9. <!-- 引入element-ui组件库 -->
  10. <script src="./js/vue.min.js"></script>
  11. <script src="./js/index.js"></script>
  12. </head>
  13. <body>
  14. <div id="app">
  15. <h1>心愿便利贴</h1>
  16. <div class="container">
  17. <!-- TODO 待修改的代码 -->
  18. <div class="card" :class="item.css" v-for="(item,index) in wishList" :key="index">
  19. <div class="header">
  20. <img class="close" @click="closeCard(index)" src="./images/ding.png" alt="close">
  21. </div>
  22. <el-image
  23. v-if="item.pic"
  24. style="width: 100px; height: 100px"
  25. :src="item.pic"
  26. :preview-src-list="picList">
  27. </el-image>
  28. <div class="content">
  29. {{item.content}}
  30. </div>
  31. <div class="name">{{item.name}}</div>
  32. </div>
  33. </div>
  34. <div class="form">
  35. <el-form ref="form" :rules="rules" label-position="left" :model="form" label-width="80px">
  36. <el-form-item label="姓名" prop="name">
  37. <el-input id="firstName" v-model="form.name" placeholder="请输入姓名"></el-input>
  38. </el-form-item>
  39. <el-form-item label="许愿内容" prop="content">
  40. <el-input type="textarea" id="content" placeholder="请输入内容" v-model="form.content" show-word-limit></el-input>
  41. </el-form-item>
  42. <el-form-item label="图片上传">
  43. <el-upload
  44. ref="uploadRef"
  45. action="#"
  46. :limit="1"
  47. list-type="picture-card"
  48. :on-remove="handleRemove"
  49. :on-change="getPic"
  50. :auto-upload="false">
  51. <i slot="default" class="el-icon-plus"></i>
  52. <div slot="file" slot-scope="{file}">
  53. <img
  54. class="el-upload-list__item-thumbnail"
  55. :src="file.url"
  56. >
  57. <span class="el-upload-list__item-actions">
  58. <span
  59. class="el-upload-list__item-preview"
  60. @click="handlePictureCardPreview(file)"
  61. >
  62. <i class="el-icon-zoom-in"></i>
  63. </span>
  64. <span
  65. v-if="!disabled"
  66. class="el-upload-list__item-delete"
  67. @click="handleRemove(file)">
  68. <i class="el-icon-delete"></i>
  69. </span>
  70. </span>
  71. </div>
  72. </el-upload>
  73. </el-form-item>
  74. <el-form-item>
  75. <el-button id="onSubmit" type="primary" @click="onSubmit">发布</el-button>
  76. <el-button @click="onRest">重置</el-button>
  77. </el-form-item>
  78. </el-form>
  79. <el-dialog :visible.sync="dialogVisible">
  80. <img width="100%" :src="form.pic" alt="">
  81. </el-dialog>
  82. </div>
  83. </div>
  84. </body>
  85. <script>
  86. const app = new Vue({
  87. el: "#app",
  88. data: {
  89. wishList: [],
  90. form: {
  91. name:'',
  92. content: '',
  93. pic: ''
  94. },
  95. rules: {
  96. // TODO 待补充验证的代码
  97. name: [
  98. { required: true, message: '请输入活动名称', trigger: 'blur' },
  99. { min: 2, max: 4, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  100. ],
  101. content: [
  102. { required: true, message: '请输入活动名称', trigger: 'blur' },
  103. { min: 1, max: 30, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  104. ],
  105. },
  106. num:1,
  107. picList: [],
  108. textarea: '',
  109. dialogVisible: false,
  110. disabled: false
  111. },
  112. methods: {
  113. // 提交方法
  114. onSubmit() {
  115. this.$refs['form'].validate((valid) => {
  116. if (valid) {
  117. let obj = this.form;
  118. obj.css = 'item' + this.num;
  119. this.num++;
  120. if(this.num > 4) {
  121. this.num = 1;
  122. }
  123. this.wishList.push(obj)
  124. this.form = {};
  125. this.$refs.uploadRef.uploadFiles.pop()
  126. console.log(this.wishList);
  127. } else {
  128. this.$message({
  129. message: '提交错误!请检查输入内容',
  130. type: 'warning'
  131. });
  132. return false;
  133. }
  134. });
  135. },
  136. // 关闭许愿卡
  137. closeCard(index) {
  138. this.wishList.splice(index,1)
  139. },
  140. // 重置表单
  141. onRest() {
  142. this.$refs['form'].resetFields();
  143. },
  144. // 图片删除
  145. handleRemove(file) {
  146. let index = this.$refs.uploadRef.uploadFiles.findIndex(e => e.uid === file.uid);
  147. this.$refs.uploadRef.uploadFiles.splice(index,1);
  148. },
  149. // 模拟上传图片
  150. getPic(e) {
  151. this.form.pic = e.url;
  152. this.picList.push(e.url)
  153. },
  154. // 预览图片
  155. handlePictureCardPreview(file, fileList) {
  156. this.form.pic = file.url;
  157. this.dialogVisible = true;
  158. }
  159. }
  160. });
  161. </script>
  162. </html>

消失的Token

  1. <!DOCTYPE html>
  2. <html lang="zn-CH">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script src="./lib/vue.min.js"></script>
  8. <script src="./lib/vuex.min.js"></script>
  9. <script src="./store/BaseModule.js"></script>
  10. <script src="./store/UserModule.js"></script>
  11. <script src="./store/index.js"></script>
  12. <link rel="stylesheet" href="./css/style.css">
  13. </head>
  14. <body>
  15. <div id="app">
  16. <div class="wrapper" style="width: 900px;">
  17. <!-- 2. 登录成功后的欢迎界面 -->
  18. <Panel v-if="token" :username="username">
  19. {{welcome}}
  20. </Panel>
  21. <!-- 1. 登录界面 -->
  22. <Login v-else @confirm="login">
  23. {{welcome}}
  24. </Login>
  25. </div>
  26. </div>
  27. <script src="./components/login.js"></script>
  28. <script src="./components/panel.js"></script>
  29. <script>
  30. // TODO 修改下面错误代码
  31. var app = new Vue({
  32. el: '#app',
  33. data() { },
  34. computed: {
  35. welcome() {
  36. return store.getters.welcome
  37. },
  38. username() {
  39. return store.getters['user/username']
  40. },
  41. token() {
  42. return store.getters['user/token']
  43. }
  44. },
  45. methods: {
  46. // 回车/点击确认的回调事件
  47. login(username) {
  48. username && store.commit('user/login', { username, token: 'sxgWKnLADfS8hUxbiMWyb' })
  49. username && store.commit('say', '登录成功,欢迎你回来!')
  50. }
  51. }
  52. })
  53. </script>
  54. </body>
  55. </html>

封装promise

  1. const fs = require('fs')
  2. const path = require('path')
  3. const textPath = path.join(__dirname, '/test.md')
  4. // 读取示例文件
  5. fs.readFile(textPath, 'utf8', (err, contrast) => {
  6. // 通过promisefy转化为链式调用
  7. const readFileSync = promisefy(fs.readFile)
  8. // console.log(JSON.stringify(readFileSync) )
  9. readFileSync(textPath, 'utf8')
  10. .then((res) => {
  11. console.log(res === contrast) // 此处结果预期:true,即promise返回内容与前面读取内容一致
  12. })
  13. .catch((err) => {})
  14. })
  15. const promisefy = (fn) => {
  16. // TODO 此处完成该函数的封装
  17. return function(path,type){
  18. return new Promise((resolve,reject)=>{
  19. fn(path,type,(err,res)=>{
  20. if(err){
  21. console.log(err)
  22. reject(err)
  23. }
  24. console.log(res)
  25. resolve(res)
  26. })
  27. })
  28. }
  29. }
  30. module.exports = promisefy // 请勿删除该行代码

购物车

  1. //模拟赛三拖拽问题
  2. <!-- TODO: 补充拖拽事件,请不要改动任何 id 属性 -->
  3. <template>
  4. <div class="container">
  5. <div class="good-list">
  6. <div draggable="true" @dragstart="drag($event,good)" v-for="good in goods" :key="good.name" class="good">
  7. <img :src="good.cover" />
  8. <span>{{ good.name }}</span>
  9. <span>¥{{ good.price }}</span>
  10. </div>
  11. </div>
  12. <div id="trolley" @dragover.prevent @drop="dragend($event)" class="trolley">
  13. <span id="bought" class="bought" v-if="bought.length !== 0">{{
  14. bought.length
  15. }}</span>
  16. <img src="./images/trolley.jpeg" />
  17. </div>
  18. <div class="result">
  19. <div>
  20. 购物车商品:<span id="goods">{{ goodsDetail }}</span>
  21. </div>
  22. <div>
  23. 购物车商品总计:<span id="total">{{ totalPrice }}</span>
  24. </div>
  25. </div>
  26. </div>
  27. </template>
  28. module.exports = {
  29. data() {
  30. return {
  31. goods: [
  32. {
  33. name: "畅销书",
  34. price: 30,
  35. cover: "./images/book.jpeg",
  36. },
  37. {
  38. name: "收纳箱",
  39. price: 60,
  40. cover: "./images/box.jpeg",
  41. },
  42. {
  43. name: "纸巾",
  44. price: 20,
  45. cover: "./images/paper.jpeg",
  46. },
  47. {
  48. name: "电视",
  49. price: 1000,
  50. cover: "./images/tv.jpg",
  51. },
  52. ],
  53. bought: [],
  54. };
  55. },
  56. // TODO: 请补充实现代码
  57. computed: {
  58. totalPrice() {
  59. console.log(this.bought);
  60. let sum=0
  61. for(let i=0;i<this.bought.length;i++){
  62. sum+=Number(this.bought[i].price)
  63. }
  64. return sum;
  65. },
  66. goodsDetail() {
  67. let obj={
  68. '畅销书':0,
  69. '收纳箱':0,
  70. '纸巾':0,
  71. '电视':0
  72. }
  73. for(let it of this.bought){
  74. if(it.name=='畅销书')obj['畅销书']++
  75. if(it.name=='收纳箱')obj['收纳箱']++
  76. if(it.name=='纸巾')obj['纸巾']++
  77. if(it.name=='电视')obj['电视']++
  78. }
  79. let str=''
  80. for(let it in obj){
  81. if(obj[it]>0){
  82. str+=`${it}*${obj[it]} `
  83. }
  84. }
  85. return str;
  86. },
  87. },
  88. methods: {
  89. drag(ev,good){
  90. ev.dataTransfer.setData("name", good.name);
  91. ev.dataTransfer.setData("price", good.price);
  92. },
  93. dragend(ev){
  94. const name = ev.dataTransfer.getData("name");
  95. const price = ev.dataTransfer.getData("price");
  96. let obj={
  97. name,
  98. price
  99. }
  100. this.bought.push(obj)
  101. }
  102. }
  103. };

分页

  1. /**
  2. * @description 得到分页数组 indexArr,如[1,2,3,4,10],[1,3,4,5,10],[1,7,8,9,10]
  3. * @param {number} currentPage 当前页数,默认为第一页
  4. * @param {number} totalPages 总的页码数
  5. * @param {number} pagerCount 设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠
  6. * @return {Array} 分页数组 indexArr
  7. */
  8. const createPaginationIndexArr = (currentPage, totalPages, pagerCount) => {
  9. let indexArr = [];
  10. // TODO:根据传参生成分页数组 indexArr
  11. // console.log(currentPage, totalPages, pagerCount);//1,10,5
  12. if(totalPages<=pagerCount){
  13. for(let i=1;i<=totalPages;i++){
  14. indexArr.push(i)
  15. }
  16. }
  17. if(totalPages>pagerCount){
  18. indexArr.push(1)
  19. if(currentPage<=pagerCount-2){
  20. for(let i=2;i<pagerCount;i++){
  21. indexArr.push(i)
  22. }
  23. }else if(currentPage>totalPages-pagerCount+2){
  24. for(let i=totalPages-(pagerCount-2);i<=totalPages-1;i++){
  25. indexArr.push(i)
  26. }
  27. }else{
  28. for(let i=currentPage+1-(pagerCount-2)+1;i<=currentPage+1;i++){
  29. indexArr.push(i)
  30. }
  31. }
  32. indexArr.push(totalPages)
  33. }
  34. console.log(indexArr);
  35. return indexArr;
  36. }
  37. module.exports = {
  38. createPaginationIndexArr
  39. }
  40. /**
  41. * @description ajax 请求,通过传递的 currentPage, pageSize 获取到当前页和总页数的数据
  42. * @param {string} url 请求地址,必填
  43. * @param {string} method 请求方式,可选参数,默认为 get
  44. * @param {string} data 请求体数据,可选参数
  45. * @param {number} currentPage 当前页数,必填
  46. * @param {number} pageSize 每页显示条目个数,必填
  47. * @return {object} {data,total} data为data.json中data数组的部分数据,total为data.json中total的值
  48. * */
  49. async function ajax({ url, method = "get", data, query: { currentPage, pageSize } }) {
  50. // TODO:根据函数参数 `query` 对象 `currentPage, pageSize` 获得当前页的数据
  51. let result = {
  52. data:[],
  53. total:0
  54. }
  55. await axios({
  56. methods:method,
  57. url,
  58. data,
  59. /* params:{
  60. currentPage, pageSize
  61. } */
  62. }).then(res=>{
  63. console.log(res.data);
  64. let arr=[]
  65. for(let i=(currentPage-1)*10;i<pageSize+(currentPage-1)*10;i++){
  66. arr.push(res.data.data[i])
  67. }
  68. result.data=arr
  69. result.total=res.data.total
  70. },
  71. err=>{
  72. console.log(err.message);
  73. })
  74. // console.log(result);//
  75. return result;
  76. }
  77. class Pagination {
  78. /**
  79. * @param {Element} root
  80. * @param {number} pageSize 每页显示条目个数
  81. * @param {number} total 总条目数
  82. * @param {number} pagerCount 设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠
  83. * @param {number} currentPage 当前页数,默认为第一页
  84. * @param {function} currentChange current-page 改变时触发
  85. */
  86. constructor(root, { pageSize, total, pagerCount, currentPage }, { currentChange }) {
  87. if (pagerCount % 2 == 0) {
  88. pagerCount--;
  89. }
  90. this.root = root;
  91. this.pageSize = pageSize || 10;
  92. this.total = total;
  93. this.pagerCount = pagerCount || 7;
  94. this.currentPage = currentPage || 1;
  95. this.currentChange = currentChange;
  96. this.totalPages = Math.ceil(total / pageSize);
  97. this.initPagination();
  98. }
  99. /**
  100. * @description 初始化分页组件,首次创建和 this.currentPage 改变时调用
  101. * */
  102. initPagination() {
  103. const indexArr = createPaginationIndexArr(this.currentPage, this.totalPages, this.pagerCount);
  104. document.querySelector("#index-arr").innerText = JSON.stringify(indexArr);
  105. this.renderPagination(indexArr);
  106. this.initEvents();
  107. this.currentChange(this.currentPage);
  108. }
  109. /**
  110. * @description 根据序号数组生成分页组件的字符串模板通过 innerHTML 挂载在 root 元素内
  111. * @param {Array} indexArr 分页数组 indexArr
  112. * @return {String} 分页组件的字符串模板
  113. */
  114. renderPagination(indexArr) {
  115. let template = '';
  116. // TODO:根据 indexArr 数组生成分页组件的字符串模板 template
  117. // console.log(indexArr);
  118. let arr=[]
  119. arr.push(indexArr[0])
  120. for(let i=1;i<this.pagerCount-1;i++){
  121. if(indexArr[i-1]!==indexArr[i]-1){
  122. arr.push('...')
  123. arr.push(indexArr[i])
  124. }
  125. else if(indexArr[this.pagerCount-1]!==indexArr[this.pagerCount-2]+1&&i===this.pagerCount-2){
  126. arr.push(indexArr[i])
  127. arr.push('...')
  128. }
  129. else arr.push(indexArr[i])
  130. }
  131. arr.push(indexArr[indexArr.length-1])
  132. console.log(arr);
  133. console.log(this.currentPage);
  134. for(let i=0;i<arr.length;i++){
  135. if(this.currentPage===arr[i]){
  136. template+= `<li class="number active">${arr[i]}</li>`
  137. // console.log('a');
  138. }
  139. else if(arr[i]==='...'){
  140. template+=`<li class="number more">...</li>`
  141. // console.log('b');
  142. }
  143. else{
  144. template+=`<li class="number ">${arr[i]}</li>`
  145. // console.log('c');
  146. }
  147. }
  148. this.root.innerHTML = `
  149. <div class="pagination">
  150. <div class="btn btn-left" id="btn-prev">&lt;</div>
  151. <ul class="pager">${template} </ul>
  152. <div class="btn btn-right" id="btn-next">&gt;</div>
  153. </div>`;
  154. }
  155. /**
  156. * @description 事件绑定,改变 this.currentPage 的值,值在 1 到 this.totalPages 之间
  157. **/
  158. initEvents() {
  159. this.root.querySelector("#btn-prev").addEventListener('click', () => {
  160. // TODO:"<" 按钮的点击事件, 点击时 this.currentPage - 1
  161. console.log('iii');
  162. if(this.currentPage>1){
  163. this.currentPage--
  164. this.initPagination();
  165. }
  166. })
  167. this.root.querySelector("#btn-next").addEventListener('click', () => {
  168. // TODO:">" 按钮的点击事件, 点击时 this.currentPage + 1
  169. if(this.currentPage<this.totalPages){
  170. this.currentPage++
  171. console.log(this.currentPage);
  172. this.initPagination();
  173. }
  174. })
  175. this.root.querySelector(".pager").addEventListener('click', (e) => {
  176. if (e.target.nodeName.toLowerCase() === 'li') {
  177. if (this.currentPage === e.target.innerText) return;
  178. if (e.target.classList.contains('more')) return;
  179. this.currentPage = Number(e.target.innerText)
  180. }
  181. this.initPagination();
  182. });
  183. }
  184. }
  185. const paginationConfigObj = { pageSize: 10, total: 100, pagerCount: 5 };
  186. const root = document.querySelector(".pagination-container");
  187. async function renderContent(currentPage) {
  188. document.querySelector("#current-page").innerText = currentPage;
  189. const { data, total } = await ajax({ url: "./js/data.json", method: "get", query: { currentPage, ...paginationConfigObj } });
  190. document.querySelector("#ajax-data").innerText = JSON.stringify(data);
  191. document.querySelector("#ajax-total").innerText = JSON.stringify(total);
  192. const contentEle = document.querySelector('.content');
  193. let template = data.reduce((prev, cur) =>
  194. prev + `
  195. <li class="item" data-index="${cur.id}">
  196. <h4 class="title">${cur.title}</h4>
  197. <div class="item-right">
  198. 评论数:<span class="replay-count">${cur.replayCount}</span>
  199. /
  200. 点击数:<span class="click-count">${cur.clickCount}</span>
  201. </div>
  202. </li>`
  203. , "");
  204. contentEle.innerHTML = template;
  205. }
  206. new Pagination(root, paginationConfigObj, { currentChange: renderContent });
  207. async function ajax({ url, method = "get", data, query: { currentPage, pageSize } }) {
  208. // TODO:根据函数参数 `query` 对象 `currentPage, pageSize` 获得当前页的数据
  209. let result = {
  210. data:[],
  211. total:0
  212. }
  213. await axios({
  214. methods:method,
  215. url,
  216. data,
  217. query:{
  218. currentPage, pageSize
  219. }
  220. }).then(res=>{
  221. result.data=res.data.data
  222. result.total=res.data.total
  223. },
  224. err=>{
  225. console.log(err.message);
  226. })
  227. // console.log(result);//
  228. return result;
  229. }
  230. renderPagination(indexArr) {
  231. let template = '';
  232. // TODO:根据 indexArr 数组生成分页组件的字符串模板 template
  233. // console.log(indexArr);
  234. let arr=[]
  235. arr.push(indexArr[0])
  236. for(let i=1;i<this.pagerCount-1;i++){
  237. if(indexArr[i-1]!==indexArr[i]-1){
  238. arr.push('...')
  239. arr.push(indexArr[i])
  240. }
  241. else if(indexArr[this.pagerCount-1]!==indexArr[this.pagerCount-2]+1&&i===this.pagerCount-2){
  242. arr.push(indexArr[i])
  243. arr.push('...')
  244. }
  245. else arr.push(indexArr[i])
  246. }
  247. arr.push(indexArr[indexArr.length-1])
  248. console.log(arr);
  249. for(let i=0;i<arr.length;i++){
  250. if(this.currentPage===i){
  251. template+= `<li class="number active">${arr[i]}</li>`
  252. }
  253. if(arr[i]==='...'){
  254. template+=`<li class="number more">...</li>`
  255. }
  256. else{
  257. template+=`<li class="number ">${arr[i]}</li>`
  258. }
  259. }
  260. this.root.innerHTML = `
  261. <div class="pagination">
  262. <div class="btn btn-left" id="btn-prev">&lt;</div>
  263. <ul class="pager">${template} </ul>
  264. <div class="btn btn-right" id="btn-next">&gt;</div>
  265. </div>`;
  266. }
  267. initEvents() {
  268. this.root.querySelector("#btn-prev").addEventListener('click', () => {
  269. // TODO:"<" 按钮的点击事件, 点击时 this.currentPage - 1
  270. if(this.currentPage>1){
  271. this.currentPage--
  272. console.log(this.currentPage);
  273. }
  274. })
  275. this.root.querySelector("#btn-next").addEventListener('click', () => {
  276. // TODO:">" 按钮的点击事件, 点击时 this.currentPage + 1
  277. if(this.currentPage<this.totalPages){
  278. this.currentPage++
  279. console.log(this.currentPage);
  280. }
  281. })
  282. this.root.querySelector(".pager").addEventListener('click', (e) => {
  283. if (e.target.nodeName.toLowerCase() === 'li') {
  284. if (this.currentPage === e.target.innerText) return;
  285. if (e.target.classList.contains('more')) return;
  286. this.currentPage = Number(e.target.innerText)
  287. }
  288. this.initPagination();
  289. });
  290. }
  291. }
  292. const createPaginationIndexArr = (currentPage, totalPages, pagerCount) => {
  293. let indexArr = [];
  294. // TODO:根据传参生成分页数组 indexArr
  295. if(totalPages<=pagerCount){
  296. for(let i=1;i<=totalPages;i++){
  297. indexArr.push(i)
  298. }
  299. }
  300. if(totalPages>pagerCount){
  301. indexArr.push(1)
  302. if(currentPage<=pagerCount-2){
  303. for(let i=2;i<pagerCount;i++){
  304. indexArr.push(i)
  305. }
  306. }else if(currentPage>totalPages-pagerCount+2){
  307. for(let i=totalPages-(pagerCount-2);i<=totalPages-1;i++){
  308. indexArr.push(i)
  309. }
  310. }else{
  311. for(let i=totalPages-(pagerCount-2);i<totalPages;i++){
  312. indexArr.push(i)
  313. }
  314. }
  315. indexArr.push(totalPages)
  316. }
  317. console.log(indexArr);
  318. return indexArr;
  319. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/705910
推荐阅读
相关标签
  

闽ICP备14008679号