【问题标题】:how to show div on event.clientX mouse position 50vw如何在 event.clientX 鼠标位置 50vw 上显示 div
【发布时间】:2021-10-13 06:57:10
【问题描述】:

大家好,我想知道是否可以找到 50vw 的鼠标值

我已经弄清楚如何显示和隐藏一个 div,具体取决于鼠标是否在 X 轴上,我想要实现的只是在超过 50vw 时显示或隐藏

任何帮助将不胜感激,我已搜索但找不到答案

这是我的代码

document.body.addEventListener('mousemove', function (event) {

    if (event.clientX >= 500) {
        console.log("show div or addclass");
        
    } else {
        console.log("hide div or remove class");
    }
}, false);

更新:我发现我添加了 window.innerWidth/2 所以习惯于 css 这从来没有发生在我身上

【问题讨论】:

  • 哇,我想通了,我添加了 window.innerWidth/2

标签: javascript position mouse


【解决方案1】:

这是你需要的吗?

const half_of_width = window.innerWidth / 2
document.addEventListener("mousemove", (event) => {console.log(event.clientX > half_of_width)})

【讨论】:

    【解决方案2】:

    感谢您的回复,我做了类似的事情

    if (event.clientX >= window.innerWidth/2) {
        console.log("show div or addclass");
        $("#choice2").fadeIn(500);
         $("#choice1").fadeOut(500);
       
     
        
        
    } else {
        console.log("hide div or remove class");
        $("#choice1").fadeIn(500);
         $("#choice2").fadeOut(500);
       
    }
    

    },假);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多