无缝轮播图的实现js
无缝轮播图
class lunbo{
constructor(){
// 选择元素;
this.imgbox = document.getElementById(“imgbox”);
this.child = this.imgbox.children;
this.left = document.getElementById(“left”);
this.right = document.getElementById(“right”);
this.index = 0;
}
// 完善布局
Sty(){
this.imgbox.style.width = this.child.lengththis.child[0].offsetWidth + “px”;
}
// 绑定事件
shijian(){
var that = this;
this.left.onclick = function() {
that.chageindex(1);
}
this.right.onclick = function () {
that.chageindex(2);
}
}
// 改变索引
chageindex(d){
if (d==1) {
if (this.index == 0) {
this.index = this.child.length - 2;
this.imgbox.style.left = this.child[0].offsetWidth * -(this.child.length-1) + “px”;
} else {
this.index–;
}
} else {
if (this.index ==this.child.length-1) {
this.index = 1;
this.imgbox.style.left = 0;
} else {
this.index++;
}
}
this.Move();
}
// 动起来
Move(){
move(this.imgbox,“left”,-this.indexthis.child[0].offsetWidth);
}
}
var b =new lunbo();
b.Sty();
b.shijian();
// 运动的封装
var t;
function move(ele,attr,target) {
clearInterval(t);
t = setInterval(function() {
// 获取元素样式
var sty = getStyle(ele,attr);
// 借助parseint 去掉单位“px”
var now = parseInt(sty);
// 为了设置缓冲效果
let speed = (target - now)/10;
speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);
if (now === target ) { clearInterval(t); } else { ele.style[attr] = now + speed + "px"; } },30)
}
// 获取非行内样式的兼容
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr];
}else{
return getComputedStyle(ele,false)[attr];
}
}
- 点赞
- 收藏
- 分享
- 文章举报
- 原生js实现无缝轮播图(最简单轮播图实现25行代码)
- 原生JS实现简单的无缝自动轮播效果
- JS实现动态无缝轮播
- 原生js实现无缝轮播图效果
- 原生js实现无缝轮播图(进阶版)
- js实现按钮控制图片向左、向右、向上、向下无缝轮播
- 原生js实现无缝轮播图
- js实现无缝轮播图——详细代码
- 原生js实现无缝滚动轮播图(移动端)-自动轮播
- js面向对象实现无缝轮播图
- js实现图片的无缝轮播滚动
- 纯js实现无缝滑动轮播
- 纯js实现无缝轮播图
- js实现图片无缝轮播效果
- js实现文字无缝向上轮播
- js实现图片无缝向左轮播
- js实现文字无缝轮播
- JS实现文字向上无缝滚动轮播
- [置顶] js 实现无缝轮播图 模仿某宝
- JS实现左右无缝轮播图代码