javascript - onmousemove 事件不起作用

标签 javascript onmousemove

我正在学习使用 onmousemove 事件,但无法使其工作,其想法是稍微倾斜图像以创建一种效果,就像它跟随鼠标一样,我猜是一种视差。这就是我想要实现的目标: https://tympanus.net/Development/PhotographyWebsiteConcept/#

这是代码:

    <div class="container-fluid" >
        <div class="row" onmousemove="myFunction(event)">
            <div class="col-sm-12" >
                <div class="hero" id="myDIV">
                    <div class="hero__back hero__back--static"></div>
                    <div class="hero__back hero__back--mover" id="move" style="transform: perspective(1000px) translate3d(-3.08931px, 9.6px, 48px) rotate3d(-0.96, -0.308931, 0, 2deg);"></div>
                    <div class="hero__front"></div>
                </div>
            </div>
        </div>
    </div>

这是 JS:

<script>
function myFunction(e) {
    var xVal = -1/(win.height/2)*e.clientY + 1;
    var         yVal = 1/(win.width/2)*e.clientX - 1;
    var         transX = 20/(win.width)*e.clientX - 10;
    var         transY = 20/(win.height)*e.clientY - 10;
    var         transZ = 100/(win.height)*e.clientY - 50;
    var coor = "Coordinates: (" + x + "," + y + ")";
    document.getElementById("move").style.transform = 'perspective(1000px) translate3d(' + transX + 'px,' + transY + 'px,' + transZ + 'px) rotate3d(' + xVal + ',' + yVal + ',0,2deg)';
    document.getElementById("move").style.WebkitTransform = 'perspective(1000px) translate3d(' + transX + 'px,' + transY + 'px,' + transZ + 'px) rotate3d(' + xVal + ',' + yVal + ',0,2deg)';
}

</script>

最佳答案

我认为是因为类 row 的 div 没有高度,您可以使用检查器检查该 div 的高度。您可以尝试将 onmousemove 函数添加到另一个 div,该 div 具有图像的完整高度。

关于javascript - onmousemove 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839436/

相关文章:

javascript - Phoenix中的static/js/app.js中的代码什么时候被调用?

javascript - 动画 SVG : Move shape in direction of mouse/rotate around fixed point?

elm - 在 Elm 中,如何检测相对于 html 元素的鼠标位置?

javascript - 鼠标滚轮滚动 - 更改滚动图像

javascript - 第 0 行错误。Google jquery api

javascript - D3.js 栏元素未动态添加

javascript - jquery widget事件处理中的"ui"参数

javascript - 如果安装了Cydia,有办法显示文本吗?

c# - 使鼠标位置超过表格的最佳方法是什么?

html - 触发 :hover on moving element without moving mouse