如何阻止javascript事件触发另一个(不同的)事件?

如何阻止javascript事件触发另一个(不同的)事件?,javascript,Javascript,我有一个onkeydown事件,可以滚动页面,也可以不滚动页面,还可以设置变量。我希望有一个onscroll事件将该变量设置为false。我不希望onkeydown事件触发onscroll事件。我该怎么做 编辑:添加了我的代码。它允许您使用左右箭头键在“我的页面”上的图像中上下移动。对不起,太宽了 <script> window.anImageHasBeenCentered = false; window.onkeydown = function ( key )

我有一个onkeydown事件,可以滚动页面,也可以不滚动页面,还可以设置变量。我希望有一个onscroll事件将该变量设置为false。我不希望onkeydown事件触发onscroll事件。我该怎么做

编辑:添加了我的代码。它允许您使用左右箭头键在“我的页面”上的图像中上下移动。对不起,太宽了

<script>
    window.anImageHasBeenCentered = false;

    window.onkeydown = function ( key )
    {
        var element = document.elementFromPoint ( window.innerWidth / 2, window.innerHeight / 2 );

        if ( anImageHasBeenCentered ) // move to next image
        {
            switch ( window.event ? event.keyCode : key.keyCode )
            {
                case 37: element = element.parentNode.previousSibling.previousSibling.firstChild; break; // Left Arrow
                case 39: element = element.parentNode.nextSibling.nextSibling.firstChild; break; // Right Arrow
                default: anImageHasBeenCentered = false; element = null;
            }
        }

        else // center current image
        {
            switch ( window.event ? event.keyCode : key.keyCode )
            {
                case 37: break; // Left Arrow
                case 39: break; // Right Arrow
                default: element = null;
            }
        }


        if ( element )
        {
            element.scrollIntoView ( );

            if ( ( window.innerHeight + window.scrollY ) < document.body.offsetHeight ) // if we are not at the bottom of the page
            {
                if ( element.offsetHeight < window.innerHeight ) // if the element is shorter than the screen
                { window.scrollBy ( 0, -( ( window.innerHeight - element.offsetHeight ) / 2 ) ) } // position it in the middle of the screen
            }

            anImageHasBeenCentered = true;
        }
    };

    // The function I would like to add, that is unfortunately triggered by the onkeydown function.
    // window.onscroll = function ( ) { anImageHasBeenCentered = false; }
</script>

window.anImageHasBeenCentered=false;
window.onkeydown=函数(键)
{
var element=document.elementFromPoint(window.innerWidth/2,window.innerHeight/2);
如果(anImageHasBeenCentered)//移动到下一个图像
{
开关(window.event?event.keyCode:key.keyCode)
{
案例37:element=element.parentNode.previousSibling.previousSibling.firstChild;break;//左箭头
案例39:element=element.parentNode.nextSibling.nextSibling.firstChild;break;//右箭头
默认值:anImageHasBeenCentered=false;element=null;
}
}
else//center当前图像
{
开关(window.event?event.keyCode:key.keyCode)
{
案例37:break;//左箭头
案例39:break;//右箭头
默认值:element=null;
}
}
if(元素)
{
element.scrollIntoView();
if((window.innerHeight+window.scrollY)
我找到了一个解决办法。我不知道我是否真的能保证它会一直有效,但到目前为止它已经成功了。我只是使用setTimeout延迟将变量设置为true,直到onscroll将其设置为false

window.anImageHasBeenCentered = false;

window.onscroll = function ( ) { anImageHasBeenCentered = false; };

window.onkeydown = function ( key )
{
   var element = document.elementFromPoint ( window.innerWidth / 2, window.innerHeight / 2 );

   switch ( window.event ? event.keyCode : key.keyCode )
   {
      case 37: // Left Arrow
         if ( anImageHasBeenCentered ) element = element.parentNode.previousSibling.previousSibling.firstChild;
         break;
      case 39: // Right Arrow
         if ( anImageHasBeenCentered ) element = element.parentNode.nextSibling.nextSibling.firstChild;
         break;
      default: element = null;
   }


   if ( element )
   {
      element.scrollIntoView ( );

      if ( ( window.innerHeight + window.scrollY ) < document.body.offsetHeight ) // if we are not at the bottom of the page
      {
         if ( element.offsetHeight < window.innerHeight ) // if the element is shorter than the screen
            window.scrollBy ( 0, -( ( window.innerHeight - element.offsetHeight ) / 2 ) ); // position it in the middle of the screen
      }

      window.setTimeout ( function ( ) { anImageHasBeenCentered = true; }, 1 );
   }
};
window.animagehasbeencented=false;
window.onscroll=function(){anImageHasBeenCentered=false;};
window.onkeydown=函数(键)
{
var element=document.elementFromPoint(window.innerWidth/2,window.innerHeight/2);
开关(window.event?event.keyCode:key.keyCode)
{
案例37://左箭头
如果(anImageHasBeenCentered)元素=element.parentNode.previousSibling.previousSibling.firstChild;
打破
案例39://右箭头
如果(anImageHasBeenCentered)element=element.parentNode.nextSibling.nextSibling.firstChild;
打破
默认值:element=null;
}
if(元素)
{
element.scrollIntoView();
if((window.innerHeight+window.scrollY)
如果你能展示一些代码,你肯定能帮上忙。你看过event.stopPropagation()方法了吗?对据我所知,StopRopagation只能防止触发相同类型的事件。不是其他类型的事件。无论您是否决定处理事件,浏览器都会引发事件;你无法阻止这种行为。JavaScript是事件驱动的。您可以使用
addEventListener
添加事件处理程序,并使用
removeEventListener
删除它。这些是你的选择。剩下的只是应用程序逻辑。