二、代码实现
1.项目目录
2.主页面CSS部分
好像有点多 截图吧
3.Slider实现
export class Slider {
constructor(id){
this.box = document.querySelector(id)
this.picBox = this.box.querySelector(“ul”)
this.indexBox = this.box.querySelector(“.index-box”)
this.sliderWidth = this.box.clientWidth
this.sliders = this.picBox.children.length
this.index = 1
this.animated = false
this.auto = null
this.init()
}
init(){
this.initPoint()
this.copyPic()
this.leftRight()
this.play()
}
initPoint(){
const num = this.picBox.children.length;
let frg = document.createDocumentFragment();
for(let i = 0; i < num; i++){
let li = document.createElement(“li”)
li.setAttribute(“data-index”, i+1)
if(i == 0) li.className = “active”
frg.appendChild(li)
}
this.indexBox.children[0].style.width = num * 10 * 2 + “px”;
this.indexBox.children[0].appendChild(frg)
this.indexBox.children[0].addEventListener(“click”, (e) => {
console.log(“point”)
let pointIndex = (e.target).getAttribute(“data-index”)
if(pointIndex == this.index || this.animated){
return
}
let offset = (pointIndex - this.index) * this.sliderWidth
this.index = pointIndex
this.move(offset)
})
}
copyPic(){
const first = this.picBox.firstElementChild.cloneNode(true)
const last = this.picBox.lastElementChild.cloneNode(true)
this.picBox.appendChild(first)
this.picBox.insertBefore(last, this.picBox.firstElementChild)
this.picBox.style.width = this.sliderWidth * this.picBox.children.length + “px”
this.picBox.style.left = -1 * this.sliderWidth + “px”
}
move(offset){
this.animate(offset);
const num = this.indexBox.children[0].children.length
for(let i = 0; i < num; i++){
this.indexBox.children[0].children[i].className = “”
}
this.indexBox.children[0].children[this.index-1].className = “active”
}
animate(offset){
const time = 1000
const rate = 100
let speed = offset / (time/rate)
let goal = parseFloat(this.picBox.style.left) - offset
this.animated = true
let animate = setInterval(() => {
if( this.picBox.style.left == goal || Math.abs(Math.abs(parseFloat(this.picBox.style.left)) - Math.abs(goal)) < Math.abs(speed) ){
this.picBox.style.left == goal
clearInterval(animate)
this.animated = false
if(parseFloat(this.picBox.style.left) == 0){
this.picBox.style.left = -this.sliders * this.sliderWidth + “px”
}else if(parseFloat(this.picBox.style.left) == -(this.sliders+1)*this.sliderWidth){
this.picBox.style.left = -this.sliderWidth + “px”
}
}else{
this.picBox.style.left = parseFloat(this.picBox.style.left) - speed + “px”;
}
}, rate);
}
leftRight(){
this.box.querySelector(“.left-box”).addEventListener(“click”, () => {
//console.log(“left”)
if(this.animated){
return
}
if(this.index - 1 < 1){
this.index = this.sliders
}else{
this.index –
}
this.move(-this.sliderWidth)
})
this.box.querySelector(“.right-box”).addEventListener(“click”, () => {
//console.log(“right”)
if(this.animated){
return
}
if(this.index + 1 > this.sliders){
this.index = 1
}else{
this.index ++
}
this.move(this.sliderWidth)
})
}
play(){
this.auto = setInterval(() => {
this.box.querySelector(“.right-box”).click()
}, 2000);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
最后
整理面试题,不是让大家去只刷面试题,而是熟悉目前实际面试中常见的考察方式和知识点,做到心中有数,也可以用来自查及完善知识体系。
《前端基础面试题》,《前端校招面试题精编解析大全》,《前端面试题宝典》,《前端面试题:常用算法》PDF完整版点击这里免费领取
后
整理面试题,不是让大家去只刷面试题,而是熟悉目前实际面试中常见的考察方式和知识点,做到心中有数,也可以用来自查及完善知识体系。
《前端基础面试题》,《前端校招面试题精编解析大全》,《前端面试题宝典》,《前端面试题:常用算法》PDF完整版点击这里免费领取
[外链图片转存中…(img-oE1l53UN-1712337125951)]
[外链图片转存中…(img-gUr8TREx-1712337125951)]
[外链图片转存中…(img-WTLGccY2-1712337125951)]