简体   繁体   English

X秒钟的页面滚动后显示jQuery对话框

[英]Show the jQuery dialog after X seconds of page scroll

Using jQuery 1.7.1, I want to show a dialog box to the user for both these circumstances... 对于这两种情况,我想使用jQuery 1.7.1向用户显示一个对话框。

  1. Approximately after 15 seconds when the page has finished loading if no user interaction has been done ie page scroll, link click. 如果未完成任何用户交互(例如,页面滚动,链接单击),则在页面完成加载大约15秒钟后。 The dialog box should appear. 对话框应出现。

  2. If after say 10 seconds an interaction (page scroll / form field click) no further action has been done and the user is still on the same page. 如果说10秒钟后进行了一次交互(单击页面滚动/单击表单字段),则没有进一步的操作,并且用户仍在同一页面上。 The dialog box should appear. 对话框应出现。


The purpose of this is to show a 'do you need any help / feedback' dialog to a user if they are still on the same page for a number of seconds and haven't scrolled or have scrolled and not interacted on the page ie clicked a link / form field. 这样做的目的是向用户显示“您是否需要任何帮助/反馈”对话框,如果他们仍在同一页面上数秒并且没有滚动或没有在页面上进行交互(即单击)链接/表单字段。

In the dialog i will show a 'No help needed' link which needs to hide the dialog and stop it opening again during the session. 在对话框中,我将显示“无需帮助”链接,该链接需要隐藏对话框并在会话期间停止再次打开。 So if this link is clicked the dialog and counter must be stopped for the remainder of that php session. 因此,如果单击此链接,则该PHP会话的其余部分必须停止对话框和计数器。

Look into jQuery UI's modal and sessionStorage. 查看jQuery UI的模式和sessionStorage。

Here is an example of what you might look for: 这是您可能寻找的示例:

function showHelpModal()
{
    // Open the dialog and reset the timer
    $('#mymodal').dialog('open');
    timeoutStarted = false;
}

var t; // Timeout variable
var timeout = 1500; // Timeout in milliseconds
var fakeSessionStorage = {}; // The fake sessionStorage, don't use this in your real code

$(document).ready(function()
{   
    // Initialize the dialog
    $('#mymodal').dialog(
    {
        autoOpen: false,
        height: 500,
        width: 500,
        modal: true,
        buttons: {
            "Don't remind me": function()
            {
                fakeSessionStorage.stopReminding = true;
                $(this).dialog('close');
            },

            "Yes, please": function()
            {
                $(this).dialog('close');
            }
        }
    });

    // Set default reminder value
    if(!fakeSessionStorage.stopReminding)
        fakeSessionStorage.stopReminding = false;

    // On scroll...
    $(document).scroll(function()
    {        
        // If the user doesn't want to be reminded, return false
        if(fakeSessionStorage.stopReminding)
        {
            console.log("Will not remind again");
            return false;
        }

        // Start the timer and prevent it from firing while busy
        timeoutStarted = true;
        clearTimeout(t);
        t = setTimeout(showHelpModal, timeout);
    });
});

And a working example at JSfiddle JSfiddle的一个工作示例

Please mind you that sessionStorage doesn't work on JSfiddle, so I used a object called fakeSessionStorage to cover for that. 请注意,sessionStorage在JSfiddle上不起作用,因此我使用了一个名为fakeSessionStorage的对象来进行覆盖。

Also, sessionStorage doesn't work in Internet Explorer 7 or earlier. 另外,sessionStorage在Internet Explorer 7或更早版本中也不起作用。

[edit] [编辑]
I had the wrong fiddle linked, fixed that. 我拨错了小提琴,将其修复。

[edit 2] [编辑2]
Having the timeoutStarted variable there was apparently the problem. 具有timeoutStarted变量显然存在问题。 I thought it was a neccessary thing but it was doing more bad than good. 我以为这是必要的,但弊大于利。

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

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