简体   繁体   English

Javascript 事件对象 ClientY 错误地为 0

[英]Javascript Event Object ClientY is incorrectly 0

I wrote a scollbar that works pretty good so far.我写了一个到目前为止效果很好的 scollbar。

This is the order in which it works:这是它的工作顺序:

  1. when I click on the gripper/scrollbar element foo.addEventListener( "mousedown", scrollObject.sbar1, false);当我点击foo.addEventListener( "mousedown", scrollObject.sbar1, false); /滚动条元素时foo.addEventListener( "mousedown", scrollObject.sbar1, false); is triggered and sbar1() correctly has the event.clientY coordinates of the gripper/scrollbar element.被触发并且sbar1()正确具有event.clientY /滚动条元素的event.clientY坐标。

  2. I use foo.addEventListener( "mousemove", sbar2, false);我使用foo.addEventListener( "mousemove", sbar2, false); in sbar1(event) to get the gripper coordinates real time while it slides.sbar1(event) ,在滑动时实时获取夹具坐标。

However, the issue is when sbar2(event) is called the event.clientY coordinates always start at 0. In my logic, the event.clientY coordinates should be the same as sbar1's event object coordinates since it is the same element that the mousedown event listener.然而,问题是当sbar2(event)被调用时event.clientY坐标总是从 0 开始。在我的逻辑中, event.clientY坐标应该与 sbar1 的事件对象坐标相同,因为它是mousedown事件的相同元素听众。

Why is the sbar2() mousedown event object ( event.clientY ) coordinates 0?为什么sbar2() mousedown事件对象 ( event.clientY ) 坐标为 0? How can I make the event target object be correct as sbar1() event.clientY is so it can start from there as it takes coordinates real time?我怎样才能使事件目标对象正确,因为sbar1() event.clientY是这样它可以从那里开始,因为它需要实时坐标?

HTML: HTML:

<div id="portfolioScrollbar">
        <div id="scrollbarTrack"><a id="scrolla" href="#scrollbar"></a></div>
    <div id="pbody">
                      ...all the text stuff...
             </div>
     </div>

Javascript: Javascript:

function start() {
    var foo = document.getElementById("scrolla");
        foo.addEventListener( "mousedown", scrollObject.sbar1, false);
        foo.addEventListener( "mouseup", removeAll, false);
        foo.addEventListener( "mouseout", removeAll, false);
    }





var scrollObject = function (event) {
         
    var current = null;
    var move = null;
    var scroll = document.getElementById("scrolla"); // THIS IS THE GRIPPER/SLIDER ELEMENT

    function sbar1(event) {
        console.log("mousedown");
        event.preventDefault();
        current = event.clientY;
        console.log(current);
        console.log("seperate");
        foo.addEventListener( "mousemove", sbar2, false);
    }

    function sbar2(event) {
//      console.log(event.clientY);
        event.preventDefault();
        move = event.clientY - current;
        
        move = Math.abs(move);
        console.log(move);
        scroll.style.top =  move + "px";

    }

    return {
        sbar1: sbar1,
            sbar2: sbar2,
    }
}();

For my own learning purposes, I prefer to use Javascript without jQuery.出于我自己的学习目的,我更喜欢在没有 jQuery 的情况下使用 Javascript。

Figured it out....it was the correct coordinates and not zero.想通了......这是正确的坐标而不是零。 The difference from current was causing it to be 0...not sure how or why.与当前的差异导致它为 0...不知道如何或为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM