单击在JavaScript中滚动鼠标位置 [英] click mouse position with scroll in javascript

查看:91
本文介绍了单击在JavaScript中滚动鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code获得x-y坐标,当浏览器滚动:

I have code to get x-y coordinates when the browser scrolls:

left1 = window.event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
top1 = window.event.clientY + document.body.scrollTop + document.documentElement.scrollTop;

这是工作在IE7,但不是在Mozilla Firefox 3.5.19。我怎样才能得到它在Firefox工作?

This is working in IE7, but not in Mozilla Firefox 3.5.19. How can I get it working in Firefox?

推荐答案

记住,click事件在Mozilla不同的处理方式比在Internet Explorer中。此外,他们持有实现光标位置的位置的不同方式。这是一个非常简单的谷歌搜索,获得在任一细节。

Remember that the click event is handled differently in Mozilla than it is in internet explorer. Also they hold different ways of attaining the position of the cursor location. This is a very easy google search to get the specifics on either.

 var IE = document.all ? true : false; // check to see if you're using IE

if (IE) //do if internet explorer 
        {
            cursorX = event.clientX + document.body.scrollLeft;
            cursorY = event.clientY + document.body.scrollTop;
        }
        else  //do for all other browsers
        {
            cursorX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            cursorY = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        }

另外请注意,你应该有类似的东西在你的初始化code以下内容:

Also note that you should have something like the following in your initialization code:

if (window.Event) {
            if (window.captureEvents) { //doesn't run if IE
                document.captureEvents(Event.MOUSEMOVE);
            }
        }
        document.onmousemove = refreshCursorXY;

在事物的点击一面,我说,有什么价值对点击浏览器之间的差异归因。例如,该检查应该在点击事件发生(您发送电子邮件,事件,到)。 Ë不会被I.E.发送所以我们做它像这样:

On the click side of things as I said there is differences in what value the click is attributed between browsers. For example, this check should happen in your click event (that you send e, the event, to). e will not be sent by I.E. so we do it like so:

//internet explorer doesn't pass object event, so check it...
        if (e == null) 
        {
            e = window.event;
        }

        //1 = internet explorer || 0 = firefox
        if ((e.button == 1 && window.event != null || e.button == 0)) 

和再次,还有IE和其他浏览器之间的许多不同之处,因此有很多的文档。如果您需要关于这个主题的更多信息谷歌搜索可以创造奇迹。

And again, there are many differences between IE and other browsers, thus there is much documentation. Google searches can do wonders if you require further information on the subject.

这篇关于单击在JavaScript中滚动鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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