图形抽象
// let data = [
// [{char: " ", num: 0}, {char: "*", num: 1}],
// [{char: " ", num: 0}, {char: "*", num: 2}],
// [{char: " ", num: 0}, {char: "*", num: 3}],
// [{char: " ", num: 0}, {char: "*", num: 4}],
// [{char: " ", num: 0}, {char: "*", num: 5}],
// ]
抽象逻辑思维 分文件封装函数(打印出菱形)
//已知条件:
//gao = 7,i = 0; --> hangNum = i + 1
// --> bigHangCount = gao
// --> binHangNum = (gao + 1) / 2
//
// hangNum kongCount *号
// * 1------------------------3=4-1--------------------------1 =7-3*2
// *** 2------------------------2=4-2--------------------------3
// ***** 3------------------------1=4-3--------------------------5
// ******* 4------------------------0=4-0--------------------------7
// ***** 5------------------------1=5-4--------------------------5
// *** 6------------------------2=6-4--------------------------3
// * 7------------------------3=7-4--------------------------1
hangNum=i+1 kongCount=|binHangNum-hangNum| *=bigHangCount-2*kongcount
kongCount判断:
let kongCount;
//用if(){} else(){}
if(binHangNum<=hangNum){
kongCount=binHangNum-hangNum
}else{
kongCount=hangNum-bigHangNum
}
//用三元运算符
kongCount = hangNum <= bigHangNum ?
bigHangNum - hangNum :
hangNum - bigHangNum;
打印图形代码思路
- getShape.js(这个文件写打印图形)
// 得到duan函数
function getDuanFun(data) {
let duanStr = "";
for(let i = 0; i < data.num; i++) {
duanStr += data.char;
}
return duanStr;
}
// 得到hang函数
function getHangFun(data) {
let hengStr = "";
for(let i = 0; i < data.length; i++) {
let duanReturn = getDuanFun(data[i]);
hengStr += duanReturn;
}
hengStr += "\n";
return hengStr;
}
// 专门接收这种数据 然后将图形根据这个数据 打印出来
function getShape(data) {
let shapeStr = "";
for(let i = 0; i < data.length; i++) {
let hangReturn = getHangFun(data[i]);
shapeStr += hangReturn;
//或者可以直接把函数返回值拼接shapeStr
//shapeStr += getHangFun(data[i]);
}
console.log(shapeStr);
}
// getShape(data)
//暴露的本质是为该模块的exports对象添加属性并赋值
module.exports = getShape;
- getShapeDate.js(找到每种图形的data[]里的数据)
//引用模块
let getShape = require("./getShape.js"); // require是一个函数 要搞清楚他的返回值 这个很重要
//菱形
function lingxingFun(gao) {
let lingxingArr = [];
let bigHangNum = (gao + 1) / 2;
let bigHangCount = gao;
for (let i = 0; i < gao; i++) {
let hangNum = i + 1;
let kongCount;
//let kongCount = hangNum <= bigHangNum ?
//bigHangNum - hangNum :
//hangNum - bigHangNum;
if(hangNum <= bigHangNum) {
kongCount = bigHangNum - hangNum;
} else {
kongCount = hangNum - bigHangNum;
}
lingxingArr.push([
{char: " ", num: kongCount}, {char: "*", num: bigHangCount - 2 * kongCount}
])
}
getShape(lingxingArr);
}
lingxingFun(7); //菱形
轮播图数据抽象
HTML页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>znn的轮播图</title>
<link rel="stylesheet" href="./css/lunBo.css">
</head>
<body>
<div class="loopDiv">
<img src="" alt="" id="imgId">
<!-- 获取父亲标签,并且要把句柄绑定在父亲身上 -->
<div class="dots">
<!-- 添加index属性,以便后续用来获取每个改变元素的currentIndex -->
<div class="dot" index="0"></div>
<div class="dot" index="1"></div>
<div class="dot" index="2"></div>
<div class="dot" index="3"></div>
</div>
<div class="position" id="positionLeft"><</div>
<div class="position" id="positionRight">></div>
</div>
<script src="./js/lunBo.js"></script>
</body>
</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 {
position: absolute;
left: 0px;
top: 80px;
}
positionRight {
position: absolute;
right: 0px;
top: 80px;
}
- 关键核心:js代码编写,实现动态效果
```javascript
//获取元素
let dots = document.getElementsByClassName("dots")[0];
let dotArr = document.getElementsByClassName("dot");
let loopDiv = document.getElementsByClassName("loopDiv")[0];
let img = document.getElementById("imgId");
let positionLeft = document.getElementById("positionLeft");
let positionRight = document.getElementById("positionRight");
//
let data = {
srcArr: [
"./images/douLuo.jpeg", //0
"./images/haiZei.jpeg", //1
"./images/huoYing.jpeg", //2
"./images/longMao.jpg" //3
],
currentIndex: 0,
currentIndexColor: "black" //区别的,一开始只有一个,默认的,点击想变的颜色(孤立的颜色)
}
//设置定时器返回参数
let timer = null;
//修改某数据为某值时候,页面元素跟着渲染
// 页面渲染: 根据数据进行dom操作 (只不过有时候dom操作再框架中已经帮你进行了你只需要按照某种规则书写 他就会进行dom操作)
function render() {
let srcArr = data.srcArr;
let currentIndex = data.currentIndex;
img.setAttribute("src", srcArr[currentIndex]);
dotArr[currentIndex].style.backgroundColor = data.currentIndexColor;
}
//修改数据函数
function setData(name, value) {
data[name] = value;
render();
}
//页面加载句柄(可以直接赋值给render()函数,但是后面渲染页面的时候还需要启动定时器,所以把这两件事写在函数里)
document.body.onload = function() {
timer = setInterval(function() {
setData("currentIndexColor", "#aaa")
setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
setData("currentIndexColor", "black")
}, 2200);
render();
};
//功能函数(处理data.currentIndex的值,防止溢出是一件事,可以封装一个函数)
function maxMin(value, max, min) {
if(value > max) {
return min;
}
if(value < min) {
return max;
}
return value;
}
//往右
positionRight.onclick = function() {
setData("currentIndexColor", "#aaa")
setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
setData("currentIndexColor", "black")
}
//往左
positionLeft.onclick = function() {
setData("currentIndexColor", "#aaa")
setData("currentIndex", maxMin(data.currentIndex - 1, data.srcArr.length - 1, 0));
setData("currentIndexColor", "black")
}
//小圆点
dots.onclick = function(e) {
setData("currentIndexColor", "#aaa")
setData("currentIndex", maxMin(Number(e.target.getAttribute("index")), data.srcArr.length - 1, 0));
setData("currentIndexColor", "black")
}
//鼠标移入
loopDiv.onmouseover = function() {
clearInterval(timer); //清空定时器函数
}
//鼠标移出
loopDiv.onmouseout = function() {
timer = setInterval(function() {
setData("currentIndexColor", "#aaa")
setData("currentIndex", maxMin(data.currentIndex + 1, data.srcArr.length - 1, 0));
setData("currentIndexColor", "black")
}, 2200);
console.log(timer)
render();
}