ul.style.left设置无效,这个问题太神奇了,请大佬帮忙看一下到底是什么原因?

我是一个初学者,正在使用原生js写一个轮播图,一共是6张图片,第6张图和第1张图是一样的,我操作ul一直向左移动,当到达第6张图片时,瞬间使ul的left变成0px,完成一个循环
222222.gif

但结果让我很难过,我让动画效果结束时判断当前是第几张图片,如果是第六张图,就让ul的left变成0px,也就是ul.style.left = "0px";但无论我怎么弄,ul的left就是不变,但设置其他属性就可以。
比如top
ul.style.top = "100px";
TOP.gif

比如backgroundColor
ul.style.backgroundColor = "red";
1211.gif

并且我给一个按钮绑定响应函数设置ul.style.left = "0px",都可以成功让ul的left变成0px,但放到动画结束后就不行。太傻了,太难受了,我已经研究了一整天了,希望有大神帮忙看看,仔细研究研究!!!

以下是完整代码:
go函数用来操作某个对象移动
huadong函数用来让ul周期性往左移动
img内图片的尺寸都是500X333.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>重写轮播图</title>
<style>
    *{
        margin: 0;
        padding: 0;        
    }
    ul{
        width: 5000px;
        list-style:none;
        position: absolute;
    }
    li{
        float: left;
        padding: 0 10px;
    }
    #box{
        width: 500px;
        height: 333px;
        background-color: yellowgreen;
        padding: 20px 0;
        margin: 100px auto;
        position: relative;
    }
    #nav{
        position: absolute;
        bottom: 30px;
        left: 50%;
        margin-left: -62.5px;
    }
    #nav a{
        height:15px;
        width: 15px;
        background-color: red;
        opacity: 0.5;
        float: left;
        margin: 0 5px;
    }
    #nav a:hover{
        background-color: black;
    }
</style>
<script>
    window.onload = function(){
        var box = document.getElementById("box");
        var ul = document.getElementById("ul");
        var li = document.getElementsByTagName("li");
        var a = document.getElementsByTagName("a");
        //ul.width
        ul.style.width = li[0].offsetWidth * li.length + "px";
        //box.width
        box.style.width = li[0].offsetWidth + "px";
        //位置计数
        var index = 0;
        setA();
        //给a绑定单击响应函数
        for( i=0; i<a.length; i++){
            a[i].number = i;
            a[i].onclick = function(){
                index = this.number;
                console.log(this.number);
                //ul.style.left = -(li[0].offsetWidth * index) + "px";
                go(ul,"left",10,-(li[0].offsetWidth * index));
                //导航条变色
                setA();

            }
        }
        document.getElementById("btn01").onclick = function(){
            huadong();
        }
        document.getElementById("btn02").onclick = function(){
            ul.style.left = "0px";
        }
        //自然滑动函数
        function huadong(){
            setInterval(function(){
                index++;
                go(ul,"left",10,-(li[0].offsetWidth * index),function(){
                    //判断索引是否等于5,如果是,让ul的left变成0!!!!!!
                    //!!!!!!!!???为什么不行
                    if (index == 5){
                        ul.style.backgroundColor = "red";
                        ul.style.left = "0px";
                        ul.style.top = "100px";
                    }
                    setA();
                });
                //console.log("索引:"+index);
            },1000);
        }

        //移动函数
        //操作对象,操作样式,速度值,目标值,操作完毕后执行的函数
        function go(obj,attr,speed,mubiao,fc){
            //每次重新操作都关闭上一个定时器
            clearInterval(obj.biaoshi);
            //判断速度正负值,当前比目标小则正,当前比目标大则负;
            if (parseInt(getStyle(obj,attr)) > mubiao) {
                speed = -speed;
            }
            //计时器---------------------------------------
            obj.biaoshi = setInterval(function(){
                //获取样式值
                var getendStyle = parseInt(getStyle(obj,attr));
                //得到即将移动的新值
                var newstyle = getendStyle + speed;
                //判断是否到达目标
                if (speed>0 && newstyle>mubiao || speed<0 && newstyle<mubiao ) {
                    newstyle = mubiao;
                    clearInterval(obj.biaoshi);
                    fc && fc();
                }
                //让该元素开始变化
                obj.style[attr] = newstyle + "px";
            },10);
            //计时器---------------------------------------
            
            //获取样式值的函数
            function getStyle(obj,name){
                return getComputedStyle(obj,null)[name];
            }
        }
        //导航条变色函数
        function setA(){
            if (index == 5){
                index = 0;
            }
            for( i=0; i<a.length; i++){
                a[i].style.backgroundColor = "";
            }
            a[index].style.backgroundColor = "black";};
    }
</script>
</head>
<body>
    <div id="box">
        <ul id="ul">
            <li><img src="timg1.jpg" alt=""></li>
            <li><img src="timg2.jpg" alt=""></li>
            <li><img src="timg3.jpg" alt=""></li>
            <li><img src="timg4.jpg" alt=""></li>
            <li><img src="timg5.jpg" alt=""></li>
            <li><img src="timg1.jpg" alt=""></li>
        </ul>
        <div id="nav">
            <a href="#"></a>
            <a href="#"></a>
            <a href="#"></a>
            <a href="#"></a>
            <a href="#"></a>
        </div>
    </div>
    <button id="btn01">开始轮播</button>
    <button id="btn02">让ul的left变成0px</button>
</body>
</html>
阅读 6.3k
3 个回答

大致看了一下,你将left属性,放在行内,再用ul.style.left = "0px"试试,top能改变是因为css没有设置,你现在js操作的是行内的属性

image.png
改了几个地方。
最后执行的效果是这样的。3比2走的快,所以最后执行的是2那个位置。所以left一直到不了0
image.png

由于 go 方法里 obj.style[attr] = newstyle + "px";是在定时器回调函数中的异步代码,其下一次执行时机是在本次 go 的回调函数 fc 之后,也就是说很可能你前脚 fc 归零,后脚它又给你改成了 newstyle。
对应得解决方法之一,就是将 style.left 的重置计算也放进 go 方法中去。
还有最好通过修改 style.transform 实现轮播而不是修改 left ,改 left 比较耗性能;定时器频率也不要设得太高,你是一秒钟且一张图的话就设成 1000ms 切换一次,平滑过渡则通过修改 style.transition 来实现。当然,兼容IE的话另当别论。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题