Ask the JS master to take a look at this scroll bar code, why does the offsetWidth value keep changing if you refresh yourself?

1. Why does the refresh page offsetWidth value change all the time? If possible, please run this code to know, if refreshed, it will automatically change to 200 or 0. 0. In addition, I added the auto-positioning function of clicking on the slider when the drag-and-drop function is good, but there is a conflict between the two events. How to solve this problem?
2. Page code

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ffg</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;

        }

        .box {
            width: 800px;
            height: 150px;
            margin: 50px auto;
            background-color: rgb(206, 206, 206);
            position: relative;
        }

        .leftBtn {
            float: left;
            padding-top: 20px;

        }

        .leftBtn a {
            display: block;
            width: 20px;
            height: 65px;
            background: url(images/bg05.png) no-repeat 0 0;

        }

        .rightBtn {
            float: left;
            padding-top: 20px;

        }

        .rightBtn a {
            display: block;
            width: 20px;
            height: 65px;
            background: url(images/bg05.png) no-repeat -40px 0;
        }

        .imgList {
            float: left;
            height: 150px;
            width: 760px;
            overflow: hidden;
            position: relative;
        }

        .m_unit {
            width: 1000%;
            height: 130px;
            position: absolute;
            top: 0;
            left: 0;
        }

        .m_unit ul {
            list-style: none;
        }

        .m_unit ul li {
            float: left;
            margin-right: 10px;
        }

        .scrollWay {
            width: 100%;
            height: 20px;
            background-color: rgb(66, 66, 66);
            position: absolute;
            bottom: 0;
        }

        .scrollBlock {
            position: absolute;
            left: 0;
            top: 2px;
            width: 300px;
            height: 15px;
            /* background-color: red; */
            background: url(images/bg07.png) 0 31px;
        }

        .sBleft {
            position: absolute;
            top: 0;
            left: 0;
            width: 11px;
            height: 15px;
            background: url(images/bg07.png) 0 0;
        }

        .sBright {
            position: absolute;
            top: 0;
            right: 0;
            width: 11px;
            height: 15px;
            background: url(images/bg07.png) -24px 0;
        }

        .sBcenter {
            position: absolute;
            top: 0;
            left: 50%;
            margin-left: -7px;
            width: 14px;
            height: 15px;
            background: url(images/bg07.png) -46px 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="leftBtn" id="leftBtn">
            <a href="javascript:;"></a>
        </div>
        <div class="imgList" id="imgList">
            <div class="m_unit" id="m_unit">
                <ul>
                    <li>
                        <img src="images/0.png" alt="">
                    </li>
                    <li>
                        <img src="images/1.png" alt="">
                    </li>
                    <li>
                        <img src="images/2.png" alt="">
                    </li>
                    <li>
                        <img src="images/3.png" alt="">
                    </li>
                    <li>
                        <img src="images/4.png" alt="">
                    </li>
                    <li>
                        <img src="images/5.png" alt="">
                    </li>
                    <li>
                        <img src="images/6.png" alt="">
                    </li>
                    <li>
                        <img src="images/7.png" alt="">
                    </li>
                    <li>
                        <img src="images/8.png" alt="">
                    </li>
                    <li>
                        <img src="images/9.png" alt="">
                    </li>
                    <li>
                        <img src="images/0.png" alt="">
                    </li>
                </ul>
            </div>
            <div class="scrollWay" id="scrollWay">
                <div class="scrollBlock" id="scrollBlock">
                    <div class="sBleft"></div>
                    <div class="sBright"></div>
                    <div class="sBcenter"></div>
                </div>
            </div>
        </div>
        <div class="rightBtn" id="rightBtn">
            <a href="javascript:;"></a>
        </div>
    </div>
    <script type="text/javascript">
        //
        var scrollBlock = document.getElementById("scrollBlock");
        var scrollWay = document.getElementById("scrollWay");
        var leftBtn = document.getElementById("leftBtn");
        var rightBtn = document.getElementById("rightBtn");
        var imgList = document.getElementById("imgList");
        var m_unit = document.getElementById("m_unit");
        var lis = document.getElementById("m_unit").getElementsByTagName("li");

        //
        var rate = imgList.offsetWidth / (210 * lis.length);
        console.log("LIOFFSET" + lis[0].offsetWidth);
        scrollBlock.style.width = scrollWay.offsetWidth * rate + "px";
        //
        scrollBlock.onmousedown = function (event) {
            event = event || window.event;
            //
            var Dvalue = event.clientX - scrollBlock.offsetLeft;
            //
            document.onmousemove = function (event) {
                event = event || window.event;
                var x = event.clientX - Dvalue;

                //
                if (x > scrollWay.offsetWidth - scrollBlock.offsetWidth) {
                    x = scrollWay.offsetWidth - scrollBlock.offsetWidth;
                } else if (x < 0) {
                    x = 0;
                }
                scrollBlock.style.left = x + "px";
                //
                m_unit.style.left = -x / rate + "px";
                //
                document.onmouseup = function () {
                    document.onmousemove = null;
                }
                //
                event.preventDefault();
                return false;
            }


        }
        //
        leftBtn.onclick = function () {
            var x = scrollBlock.offsetLeft - 50;
            //
            if (x > scrollWay.offsetWidth - scrollBlock.offsetWidth) {
                x = scrollWay.offsetWidth - scrollBlock.offsetWidth;
            } else if (x < 0) {
                x = 0;
            }

            scrollBlock.style.left = x + "px";
            m_unit.style.left = -x / rate + "px";
        }
        rightBtn.onclick = function () {
            var x = scrollBlock.offsetLeft + 50;
            //
            if (x > scrollWay.offsetWidth - scrollBlock.offsetWidth) {
                x = scrollWay.offsetWidth - scrollBlock.offsetWidth;
            } else if (x < 0) {
                x = 0;
            }
            scrollBlock.style.left = x + "px";
            m_unit.style.left = -x / rate + "px";
        }

        //
        scrollWay.onclick = function (event) {
            event = event || window.event;
            //
            var msx = event.offsetX;
            if (msx > scrollBlock.offsetLeft) {
                msx = event.offsetX - scrollBlock.offsetWidth;
            } else if (msx < scrollBlock.offsetLeft) {
                msx = event.offsetX;
            };
            scrollBlock.style.left = msx + "px";
            m_unit.style.left = -msx / rate + "px";
            return false;
        }
    </script>
</body>

</html>

3. Picture

4. If possible, please ask the god to run it to help see why two events are incompatible and there is no solution. Thank you very much!

Mar.31,2021

  1. Why does the refresh page offsetWidth value constantly change?
    because your li itself does not have a width, when the img download is complete, hold up the width. When the page is refreshed, when the img download is not complete, the offsetWidth of the li is 0. coincidentally, before your js performs the print operation, the img download is completed, and the true width will be printed.
    the solution is to put your pile of js in the window onload event, that is, wait for all the elements of the page to load before executing the script.
    window.onload = function () {/ / put here};
  2. event conflict?
    you didn't make it clear who clashed with whom in the question. It seems that the combination of scrollBlock.onmousedown and onmouseup (two combinations equivalent to a click) conflicts with scrollWay.click? If you are defining the handling method, just use the target property of event to determine the source of the event.
  3. your code is really weird.
    document.onmouseup Why is it written in onmousedown? it would be more reasonable to write side by side.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3bf2d-2ba50.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3bf2d-2ba50.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?