js学习笔记-----无缝滚动的实现

js部分:

var obox = document.getElementById('box');
var li = document.getElementsByTagName('li');
var n=0;
var timer=setInterval(function(){
    n=n-1;
    if(n<=-1200){
        n=0;
    }
    obox.style.left=n+'px';
},1)

for(n=0;n<li.length;n++){
    li[n].onmouseenter=function(){
        clearInterval(timer);
        for(j=0;j<li.length;j++){
            li[j].setAttribute('class','oli')
        }
        this.removeAttribute('class')
    }
    li[n].onmouseleave=function(){
        timer=setInterval(function(){
            n=n-1;
            if(n<=-1200){
                n=0;
            }
            obox.style.left=n+'px';
        },1)
        for(j=0;j<li.length;j++){
            li[j].removeAttribute('class')
        }
    }
}

css部分:

*{
    margin: 0px;
    padding: 0px;
    list-style: none;
}
body{
    background-color: black;
}
div{
    width: 600px;
    height: 100px;
    border: 1px solid lime;
    margin: 150px auto;
    overflow: hidden;
}
ul{
    width: 1800px;
    position: relative;
}
ul li{
    width: 300px;
    height: 100px;
    float: left;
}

li.oli{
    opacity: 0.3;
}

ul li:nth-of-type(1),ul li:nth-of-type(5){
    background-color: magenta;

}
ul li:nth-of-type(2),ul li:nth-of-type(6){
    background-color: rgb(255, 0, 0);
}
ul li:nth-of-type(3){
    background-color: rgb(0, 255, 42);
}
ul li:nth-of-type(4){
    background-color: rgb(255, 255, 0);
}

html部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/无缝滚动.css">
</head>
<body>
    <div>
        <ul id="box">
            <li class="oli"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script src="js/无缝滚动.js"></script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值