图形抽象

  1. // let data = [
  2. // [{char: " ", num: 0}, {char: "*", num: 1}],
  3. // [{char: " ", num: 0}, {char: "*", num: 2}],
  4. // [{char: " ", num: 0}, {char: "*", num: 3}],
  5. // [{char: " ", num: 0}, {char: "*", num: 4}],
  6. // [{char: " ", num: 0}, {char: "*", num: 5}],
  7. // ]

抽象逻辑思维 分文件封装函数(打印出菱形)

  1. //已知条件:
  2. //gao = 7,i = 0; --> hangNum = i + 1
  3. // --> bigHangCount = gao
  4. // --> binHangNum = (gao + 1) / 2
  5. //
  6. // hangNum kongCount *号
  7. // * 1------------------------3=4-1--------------------------1 =7-3*2
  8. // *** 2------------------------2=4-2--------------------------3
  9. // ***** 3------------------------1=4-3--------------------------5
  10. // ******* 4------------------------0=4-0--------------------------7
  11. // ***** 5------------------------1=5-4--------------------------5
  12. // *** 6------------------------2=6-4--------------------------3
  13. // * 7------------------------3=7-4--------------------------1
  14. hangNum=i+1 kongCount=|binHangNum-hangNum| *=bigHangCount-2*kongcount
  15. kongCount判断:
  16. let kongCount;
  17. //用if(){} else(){}
  18. if(binHangNum<=hangNum){
  19. kongCount=binHangNum-hangNum
  20. }else{
  21. kongCount=hangNum-bigHangNum
  22. }
  23. //用三元运算符
  24. kongCount = hangNum <= bigHangNum ?
  25. bigHangNum - hangNum :
  26. hangNum - bigHangNum;

打印图形代码思路

  • getShape.js(这个文件写打印图形)
  1. // 得到duan函数
  2. function getDuanFun(data) {
  3. let duanStr = "";
  4. for(let i = 0; i < data.num; i++) {
  5. duanStr += data.char;
  6. }
  7. return duanStr;
  8. }
  9. // 得到hang函数
  10. function getHangFun(data) {
  11. let hengStr = "";
  12. for(let i = 0; i < data.length; i++) {
  13. let duanReturn = getDuanFun(data[i]);
  14. hengStr += duanReturn;
  15. }
  16. hengStr += "\n";
  17. return hengStr;
  18. }
  19. // 专门接收这种数据 然后将图形根据这个数据 打印出来
  20. function getShape(data) {
  21. let shapeStr = "";
  22. for(let i = 0; i < data.length; i++) {
  23. let hangReturn = getHangFun(data[i]);
  24. shapeStr += hangReturn;
  25. //或者可以直接把函数返回值拼接shapeStr
  26. //shapeStr += getHangFun(data[i]);
  27. }
  28. console.log(shapeStr);
  29. }
  30. // getShape(data)
  31. //暴露的本质是为该模块的exports对象添加属性并赋值
  32. module.exports = getShape;
  • getShapeDate.js(找到每种图形的data[]里的数据)
  1. //引用模块
  2. let getShape = require("./getShape.js"); // require是一个函数 要搞清楚他的返回值 这个很重要
  3. //菱形
  4. function lingxingFun(gao) {
  5. let lingxingArr = [];
  6. let bigHangNum = (gao + 1) / 2;
  7. let bigHangCount = gao;
  8. for (let i = 0; i < gao; i++) {
  9. let hangNum = i + 1;
  10. let kongCount;
  11. //let kongCount = hangNum <= bigHangNum ?
  12. //bigHangNum - hangNum :
  13. //hangNum - bigHangNum;
  14. if(hangNum <= bigHangNum) {
  15. kongCount = bigHangNum - hangNum;
  16. } else {
  17. kongCount = hangNum - bigHangNum;
  18. }
  19. lingxingArr.push([
  20. {char: " ", num: kongCount}, {char: "*", num: bigHangCount - 2 * kongCount}
  21. ])
  22. }
  23. getShape(lingxingArr);
  24. }
  25. lingxingFun(7); //菱形

轮播图数据抽象

  • HTML页面

    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>znn的轮播图</title>
    7. <link rel="stylesheet" href="./css/lunBo.css">
    8. </head>
    9. <body>
    10. <div class="loopDiv">
    11. <img src="" alt="" id="imgId">
    12. <!-- 获取父亲标签,并且要把句柄绑定在父亲身上 -->
    13. <div class="dots">
    14. <!-- 添加index属性,以便后续用来获取每个改变元素的currentIndex -->
    15. <div class="dot" index="0"></div>
    16. <div class="dot" index="1"></div>
    17. <div class="dot" index="2"></div>
    18. <div class="dot" index="3"></div>
    19. </div>
    20. <div class="position" id="positionLeft">&lt;</div>
    21. <div class="position" id="positionRight">&gt;</div>
    22. </div>
    23. <script src="./js/lunBo.js"></script>
    24. </body>
    25. </html>
  • CSS样式 ```css .loopDiv { position: relative; width: 700px; height: 350px; margin: 50px auto; } img { width: 700px; height: 350px; } .dots { position: absolute; bottom: 5px; right: 0px; } .dot { width: 20px; height: 20px; margin-right: 4px; border-radius: 25px; background-color:#aaa; float: left; }

.position { margin-top:70px ; width: 100px; height: 50px; background-color: rgba(0,0,0,0.5); text-align: center; color: rgb(201, 192, 192); font-size: 50px; line-height: 50px; font-weight: bold; / border: 1px solid rebeccapurple; / }

positionLeft {

  1. position: absolute;
  2. left: 0px;
  3. top: 80px;

}

positionRight {

  1. position: absolute;
  2. right: 0px;
  3. top: 80px;

}

  1. - 关键核心:js代码编写,实现动态效果
  2. ```javascript
  3. //获取元素
  4. let dots = document.getElementsByClassName("dots")[0];
  5. let dotArr = document.getElementsByClassName("dot");
  6. let loopDiv = document.getElementsByClassName("loopDiv")[0];
  7. let img = document.getElementById("imgId");
  8. let positionLeft = document.getElementById("positionLeft");
  9. let positionRight = document.getElementById("positionRight");
  10. //
  11. let data = {
  12. srcArr: [
  13. "./images/douLuo.jpeg", //0
  14. "./images/haiZei.jpeg", //1
  15. "./images/huoYing.jpeg", //2
  16. "./images/longMao.jpg" //3
  17. ],
  18. currentIndex: 0,
  19. currentIndexColor: "black" //区别的,一开始只有一个,默认的,点击想变的颜色(孤立的颜色)
  20. }
  21. //设置定时器返回参数
  22. let timer = null;
  23. //修改某数据为某值时候,页面元素跟着渲染
  24. // 页面渲染: 根据数据进行dom操作 (只不过有时候dom操作再框架中已经帮你进行了你只需要按照某种规则书写 他就会进行dom操作)
  25. function render() {
  26. let srcArr = data.srcArr;
  27. let currentIndex = data.currentIndex;
  28. img.setAttribute("src", srcArr[currentIndex]);
  29. dotArr[currentIndex].style.backgroundColor = data.currentIndexColor;
  30. }
  31. //修改数据函数
  32. function setData(name, value) {
  33. data[name] = value;
  34. render();
  35. }
  36. //页面加载句柄(可以直接赋值给render()函数,但是后面渲染页面的时候还需要启动定时器,所以把这两件事写在函数里)
  37. document.body.onload = function() {
  38. timer = setInterval(function() {
  39. setData("currentIndexColor", "#aaa")
  40. setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
  41. setData("currentIndexColor", "black")
  42. }, 2200);
  43. render();
  44. };
  45. //功能函数(处理data.currentIndex的值,防止溢出是一件事,可以封装一个函数)
  46. function maxMin(value, max, min) {
  47. if(value > max) {
  48. return min;
  49. }
  50. if(value < min) {
  51. return max;
  52. }
  53. return value;
  54. }
  55. //往右
  56. positionRight.onclick = function() {
  57. setData("currentIndexColor", "#aaa")
  58. setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
  59. setData("currentIndexColor", "black")
  60. }
  61. //往左
  62. positionLeft.onclick = function() {
  63. setData("currentIndexColor", "#aaa")
  64. setData("currentIndex", maxMin(data.currentIndex - 1, data.srcArr.length - 1, 0));
  65. setData("currentIndexColor", "black")
  66. }
  67. //小圆点
  68. dots.onclick = function(e) {
  69. setData("currentIndexColor", "#aaa")
  70. setData("currentIndex", maxMin(Number(e.target.getAttribute("index")), data.srcArr.length - 1, 0));
  71. setData("currentIndexColor", "black")
  72. }
  73. //鼠标移入
  74. loopDiv.onmouseover = function() {
  75. clearInterval(timer); //清空定时器函数
  76. }
  77. //鼠标移出
  78. loopDiv.onmouseout = function() {
  79. timer = setInterval(function() {
  80. setData("currentIndexColor", "#aaa")
  81. setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
  82. setData("currentIndexColor", "black")
  83. }, 2200);
  84. console.log(timer)
  85. render();
  86. }