js轮播图

利用transform控制偏移

利用transition设置动画效果

html部分

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <link rel="stylesheet" href="./css/轮播图.css">
</head>

<body>
    <div class="bigbox">
        <div class="box">
            <div class="list">
                <img draggable="false" src="./img/111.jpg" alt="">
            </div>
            <div class="list">
                <img draggable="false" src="./img/112.jpg" alt="">
            </div>
            <div class="list">
                <img draggable="false" src="./img/113.jpg" alt="">
            </div>
            <div class="list">
                <img draggable="false" src="./img/111.jpg" alt="">
            </div>
        </div>
        <div class="cirs">
            <div class="cir cir_active"></div>
            <div class="cir"></div>
            <div class="cir"></div>
        </div>
        <div class="btns">
            <div class="pre">上</div>
            <div class="next">下</div>
        </div>
    </div>
</body>
<script src="./js/轮播图.js"></script>

</html>

css部分

:root {
    --imgwidth: 600px;
    --boxwidth: 2400px;
    --imgheight: 420px
}

* {
    margin: 0;
    padding: 0;
}

.bigbox {
    width: var(--imgwidth);
    height: 500px;
    margin: 200px auto;
    box-sizing: border-box;
    overflow: hidden;
}

.box {
    width: var(--boxwidth);
    height: var(--imgheight);
    display: flex;

}

.list {
    width: var(--imgwidth);
    height: var(--imgheight);
}

.list img {
    width: var(--imgwidth);
    height: var(--imgheight);
}

.cirs {
    display: flex;
}

.cir {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: #ccc;
}

.cirs .cir_active {
    background-color: red;
}

.btns {
    display: flex;
}

.btns div {
    width: 30px;
    text-align: center;
    height: 30px;
    line-height: 30px;
    border: 1px solid;
    border-radius: 50%;
    cursor: pointer;
    margin-right: 10px;
}

js部分

let bigbox = document.querySelector('.bigbox');
let box = document.querySelector('.box');
let lists = document.querySelectorAll('.list');
let cirs = document.querySelectorAll('.cir');

let leftIndex = 0;
let imgWidth = -600;
let leftWidth = 0;
function getleftWidth() {
    leftWidth = leftIndex * imgWidth;
}


// 设置焦点
function setcir() {
    cirs.forEach(item => {
        item.classList.remove('cir_active');
    });

    if (leftIndex == 3) {
        cirs[0].classList.add('cir_active');
    } else {
        cirs[leftIndex].classList.add('cir_active');
    }

}
// transform: translateX(10px);
// transition
// 设置滚动
function setTrans() {
    // 正常设置
    getleftWidth();
    box.style.transition = 'all 0.7s ease-out';
    box.style.transform = 'translateX(' + leftWidth + 'px)';
    setcir();
}

// 设置立即滚动
function setTrans1() {
    // 移动到了最后一张,立马切换到第一张,然后开启移动
    getleftWidth();
    box.style.transition = 'all 0s';
    box.style.transform = 'translateX(' + leftWidth + 'px)';
    setTimeout(() => {
        leftIndex = leftIndex + 1;
        setTrans();
    }, 20);
}
function setTrans2() {
    // 移动到了第一张,立马切换到最后一张,然后开启移动
    getleftWidth();
    box.style.transition = 'all 0s';
    box.style.transform = 'translateX(' + leftWidth + 'px)';
    setTimeout(() => {
        leftIndex = leftIndex - 1;
        setTrans();
    }, 20);
}

// 判断设置的类型
function setFn() {
    if (leftIndex > 3) {
        leftIndex = 0;
        setTrans1();
    } else if (leftIndex < 0) {
        leftIndex = 3;
        setTrans2();
    }
    else {
        setTrans();
    }
}

// 开启定时器
let timer;
function setTimer() {
    timer = setInterval(() => {
        leftIndex = leftIndex + 1;
        setFn();
    }, 2000);
}
setTimer();


// 鼠标移入清除定时器
bigbox.onmouseenter = function () {
    clearInterval(timer);
};
// 鼠标移出再次开启定时器
bigbox.onmouseleave = function () {
    setTimer();
};


let pre = document.querySelector('.pre');
let next = document.querySelector('.next');

// 设置点击上下切换
next.onclick = function () {
    leftIndex = leftIndex + 1;
    setFn();
};
pre.onclick = function () {
    leftIndex = leftIndex - 1;
    setFn();
};

// 设置点击焦点切换
cirs.forEach((item, index) => {
    item.onclick = function () {
        console.log(index);
        leftIndex = index;
        setFn();
    };
});



// 设置滑动切换
let downLft = 0;
box.onmousedown = function (event) {
    downLft = event.offsetX;
    event.stopPropagation();
    box.addEventListener('mousemove', (event) => {
        event.stopPropagation();
        box.onmouseup = function (event) {
            if (event.offsetX - downLft > 100) {
                leftIndex = leftIndex - 1;
                setFn();
            } else if (event.offsetX - downLft < -100) {
                leftIndex = leftIndex + 1;
                setFn();
            } else {
                return;
            }
        };
    });
};

其中初始化的imgWidth的可利用js获取指定元素的宽度进而不用再手动进行设置

定时器时间可用一个公共的变量存一下

样式可按照自己心情设置

拖动有点bug,不知道怎么解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值