简体   繁体   English

如何通过向上移动到地址栏来检测鼠标离开页面?

[英]How can I detect a mouse leaving a page by moving up to the address bar?

I have created a jQuery event that pops up a dialog when a visitor is leaving a page. 我创建了一个jQuery事件,当访问者离开页面时会弹出一个对话框。 I am using the e.pageY to detect the mouse position. 我正在使用e.pageY来检测鼠标位置。 when the mouse is at Y: less than 2, the dialog pops up. 当鼠标位于Y:小于2时,弹出对话框。 the problem is, when the I scroll down through the page and decided to leave the page, the pop up does not show since the mouse is not at Y: less than 2. How can I fix that. 问题是,当我向下滚动页面并决定离开页面时,弹出窗口没有显示,因为鼠标不在Y:小于2.我该如何解决这个问题。 ie when I leave the page and just hover over the address bar, a pop up appears despite scrolling down. 即当我离开页面并将鼠标悬停在地址栏上时,尽管向下滚动,仍会出现弹出窗口。

Here my code and a working example at the bottom. 这里是我的代码和底部的工作示例。

var mouseLastYPos = null;
$(document).mousemove(function(e){ 
    if(mouseLastYPos){ 
        if (e.pageY < mouseLastYPos && e.pageY <= 2){

           $('#mystuff').show();

        }
    }
    mouseLastYPos = e.pageY;
});​

Working example: http://jsfiddle.net/bmHbt/ 工作示例: http//jsfiddle.net/bmHbt/

Old Question, but I think I should share my code also, maybe someone finds it useful. 旧问题,但我想我也应该分享我的代码,也许有人觉得它很有用。

$(function(){
    var mouseY = 0;
    var topValue = 0;
    window.addEventListener("mouseout",function(e){
        mouseY = e.clientY;
        if(mouseY<topValue) {
            alert("Do something here!!!");
        }
    },
    false);
});

JSFIDDLE Link JSFIDDLE链接

Here's my implementation: http://jsfiddle.net/fq8HT/ 这是我的实现: http//jsfiddle.net/fq8HT/

It also tries to account for someone who is moving their mouse really fast by including the difference between the last time mousemove was triggered. 它还试图通过包括上次触发mousemove之间的差异来解释正在快速移动鼠标的人。

(function() {

  var current_scroll = 0;
  var last_mouse_y = null;

  $(document)
    .scroll(function() {
      current_scroll = $(this).scrollTop();
    })
    .mousemove(function(e) {
      var speed = last_mouse_y - e.pageY;
      var success_val = e.pageY - speed;

      if (success_val < last_mouse_y && success_val <= current_scroll) {
        $('#stop-leaving').show();
      } else {
        $('#stop-leaving').hide();
      }

      last_mouse_y = e.pageY;
    });

})();

Not sure when this would be an effective feature. 不确定这将是一个有效的功能。 But anyways, you probably will have to track the scroll position of the page mixed with some other variables, but from that you should be able to detect where the top of the browser window is and get it to work better. 但无论如何,您可能必须跟踪页面的滚动位置以及其他一些变量,但是从那里您应该能够检测到浏览器窗口顶部的位置并使其更好地工作。

I believe there is no reliable way to know that the mouse left the document. 我相信没有可靠的方法知道鼠标离开了文档。 If you move the mouse fast enough then you will see nothing. 如果你足够快地移动鼠标,那么你什么都看不到。

Here's a solution without jQuery and should work for IE8+ on Desktop Browsers that will trigger when the user directs their mouse towards the top of the page: 这是一个没有jQuery的解决方案,应该适用于桌面浏览器上的IE8 +,当用户将鼠标指向页面顶部时会触发:

document.addEventListener('mouseleave', function(e) {
  // position of the mouse when it leaves the document, we only care about the top of the document so we use `e.pageY`
  console.log('e.pageY: ', e.pageY);

  // get the position of where the page is scrolled to. 
  console.log('document.body.scrollTop: ', document.body.scrollTop);

  console.log(e.pageY - document.body.scrollTop);

  var isLeaving = e.pageY - document.body.scrollTop;

  // you can adjust this number value to compensate if the user leaves
  if (isLeaving <= 50) {
    // do something here!
  }
});

The general purpose of this snippet is to detect a user's intent to exit the page and then do something like trigger a modal. 此代码段的一般用途是检测用户退出页面的意图,然后执行触发模式的操作。

 document.addEventListener('mouseleave', e => { var isLeaving = e.pageY - document.body.scrollTop; if (isLeaving <= 50) { // do something ... let elms = document.querySelectorAll('.target'); for (var i = elms.length - 1; i >= 0; i--) { elms[i].innerHTML = 'Welcome Back!' } } }); 
  html, body, section { min-height: 100vh; min-width: 100vw; display: flex; flex-flow: column nowrap; justify-content: center; align-items: center; } h1 { text-align: center; } 
 <section> <h1 class="target">Get ahead and try to leave</h1> </section> <section> <h1 class="target">Get ahead and try to leave</h1> </section> <section> <h1 class="target">Get ahead and try to leave</h1> </section> <section> <h1 class="target">Get ahead and try to leave</h1> </section> 

I know this can be done. 我知道这可以做到。 Here's a working demo: http://modules.xcart-service.com/?target=exit_offers_demo 这是一个工作演示: http//modules.xcart-service.com/? target = exit_offers_demo

This meets all the requirements of the OP - you can scroll down and it still acts the same. 这符合OP的所有要求 - 您可以向下滚动,它仍然可以起作用。 Would be great if someone could shed some light on how it is actually achieved.... 如果有人能够了解它是如何实现的话会很棒....

You Can use it: 你可以用它:

(function($){
    var topValue = 50;
    var mouseY = 0;
    jQuery( "body" ).on('mousemove',function( event ) {
        mouseY = event.clientY;
        if(mouseY <= topValue) {

            alert('ok ');
        }

    });

})(jQuery);

Another One here it is best for you : 另一个在这里最适合你:

$(document).bind("mouseleave", function(e) {
        if (e.pageY - $(window).scrollTop() <= 1) {    
            // $('#BeforeYouLeaveDiv').show();
            alert('ok ');
            $(document).unbind("mouseleave");
        }
    });

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

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