首页 > 解决方案 > 从 offsetX 值中获取数值

问题描述

所以我试图解决一个涉及鼠标点击/触摸动作和移动交互的方程。工作起来很像一个滑块,但它会让动画逐帧运行,这就是我只需要数字的原因。

<html>
    <header>
        <!-- By the way, this is not the actual html file, just a generic example. -->
        <style>
            #box{
                border:1px solid red; 
                height:500px;
                width:500px;
            }
        </style>
    </header>
    <body>
        <div id="box" onclick="posInicial()" onmousemove="posMove()"></div>
        <div id="Z" ></div>

        <script>
            function posInicial() {
                var posX = event.offsetX;
                return posX;
            };

            function posMove() {
                var posY = event.offsetX;
                return posY;
            };

            var z = posY - posX;

            document.getElementById("Z").innerHTML =  z;
        </script>
    </body>
</html>

每当我编写下面的代码时,我都可以在 HTML 中实时显示和更新数字,但我需要动画帧控件的“减号”操作中的实际值

function posInicial() {
    var posX = event.offsetX;
    document.getElementById("Z").innerHTML = posX;
};

有人可以救我吗?

标签: javascript

解决方案



推荐阅读