javascript将事件处理程序绑定到水平滚动 [英] javascript bind an event handler to horizontal scroll

查看:119
本文介绍了javascript将事件处理程序绑定到水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中有一种方法来将事件处理程序绑定到水平滚动,而不是在用户水平和垂直滚动时触发的通用滚动事件?我只想在用户水平滚动时触发一个事件。



我搜索了这个问题的答案,但似乎找不到任何东西。 >

谢谢!



PS如果我使用某些术语错误,我很抱歉。我很新的javascript。



更新



非常感谢所有的答案!总而言之,您似乎都在说JavaScript不支持这一点,但是我可以用这样的东西来完成这个功能(使用jQuery)(jsFiddle)

  var oldScrollTop = $(window).scrollTop(); 

$(window).bind('scroll',function(){
if(oldScrollTop == $(window).scrollTop())
//水平滚动
else {
//垂直滚动
oldScrollTop = $(window).scrollTop();
}
});

这就是我需要知道的一切。再次感谢!

解决方案

从我的手机回覆,所以目前无法提供代码。



您需要做的是订阅滚动事件。垂直/水平没有一个特定的。



接下来,您需要对当前显示区域进行一些测量。你需要测量window.clientHeight和window.clientWidth。



接下来,获取window.top和window.left。这将告诉你视口的位置,即如果它大于0,那么滚动条已被使用。



从这里得到你所需要的数学很简单。如果没有人在未来几个小时内提供代码示例,我会尝试这样做。



编辑:
有一些进一步的信息。



您必须捕获滚动事件。您还需要在某处存储初始window.top和window.left属性。每当滚动事件发生时,请进行简单的检查以查看当前的顶部/左侧值是否与存储值不同。



此时,如果两者不同,您可以触发自己的自定义事件来指示垂直或水平滚动。如果你使用jQuery,这很简单。如果您在没有图书馆协助的情况下编写js,这很简单,但更多的涉及。



在js中进行一些事件调度搜索。



然后,您可以编写要订阅自定义事件的任何其他代码,而无需使用方法调用将它们绑定在一起。


Is there a way in javascript to bind an event handler to a horizontal scroll as opposed to the generic scroll event which is fired when the user scrolls horizontally and vertically? I want to trigger an event only when the user scrolls horizontally.

I searched around for an answer to this question, but couldn't seem to find anything.

Thanks!

P.S. My apologies if I'm using some terminology incorrectly. I'm fairly new to javascript.

UPDATE

Thanks so much for all your answers! In summary, it looks like you are all saying that this isn't supported in javascript, but I that I can accomplish the functionality with something like this (using jQuery) (jsFiddle):

var oldScrollTop = $(window).scrollTop();

$(window).bind('scroll', function () {
    if (oldScrollTop == $(window).scrollTop())
        //scrolled horizontally
    else {
        //scrolled vertically
        oldScrollTop = $(window).scrollTop();
    }
});​

That's all I needed to know. Thanks again!

解决方案

Answering from my phone, so unable to provide code at the moment.

What you'll need to do is subscribe to the scroll event. There isn't a specific one for vertical/horizontal.

Next, you'll need to get some measurements about the current display area. You'll need to measure the window.clientHeight and window.clientWidth.

Next, get window.top and window.left. This will tell you where position of the viewport is, ie if it's greater than 0 then scroll bars have been used.

It's pretty simple math from here to get what you need. If no one else has provided a code example in the next few hours I'll try to do so.

Edit: A bit further information.

You must capture the scroll event. You also need to store the initial window.top and window.left properties somewhere. Whenever the scroll event happens, do a simple check to see if the current top/left values differ from the stores value.

At this point, if either are different you can trigger your own custom events to indicate vertical or horizontal scrolling. If you are using jQuery, this is very easy. If you are writing js without library assistance, it's easy too but a little more involved.

Do some searches for event dispatching in js.

You can then write any other code you want to subscribe to your custom events without needing to tie them together with method calls.

这篇关于javascript将事件处理程序绑定到水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆