简体   繁体   English

如何每30秒更改一次模式弹出窗口?

[英]How can I change a modal popup every 30 seconds?

I have a div id called modalpage 我有一个名为modalpage的div ID
and have css. 并有CSS。 I need a javascript function which can dynamically shows popup for 20 mins and change in every 30 secs right now i have the following javascript function. 我需要一个可以动态显示弹出窗口20分钟并每30秒更改一次的javascript函数,现在我具有以下javascript函数。 Can anybody help me please 有人可以帮我吗

<script language="javascript" type="text/javascript">
function revealModal(divID)
{
    window.onscroll = function () { 
        document.getElementById(divID).style.top = document.body.scrollTop;
    };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.top = document.body.scrollTop;
}

which is called by a input id button. 由输入ID按钮调用。

<input id="Button1" type="button" value="Click here" onclick="revealModal('modalPage')" />

Thanks 谢谢


This is what i came up with 这就是我想出的

function revealModal(divID)
{
var i = 1;
divID.replace('*', i);
setInterval(function(){revealModal(divID)},1000);
i = i + 1;
if (i == 3) i = 1;
var div = getElementById(divID)
window.onscroll = function () { document.getElementById(divID).style.top =       document.body.scrollTop; };
document.getElementById(divID).style.display = "block";
document.getElementById(divID).style.top = document.body.scrollTop;
}

but this is not working its showing the modular_3 every time. 但这不能每次都显示modular_3。 is it because of all three divs are in the same file ?? 是因为所有三个div都在同一个文件中吗?

<input id="Button1" type="button" value="Click heres" onclick="revealModal('modalPage_*')" />

Well, for one thing don't put that set interval inside the function. 好吧,一件事就是不要将设置的间隔放在函数中。 That would start a never ending cycle on the first click that would keep triggering the div id it was passed. 这将在第一次点击时开始一个永无止境的循环,该循环将不断触发通过它的div ID。

What you should probably do is keep an array of id's that should be updated and loop through that in the function... 您可能应该做的是保留应更新的id数组,并在函数中循环遍历...

var divs = [];

function addID(id)
{
    divs.push(id);
}

function revealModal()
{
    for(var i = 0; i < divs.length; i ++)
    {
        var div = getElementById('modalPage_' + divs[i]);

        window.onscroll = function () { 
            div.style.top = document.body.scrollTop; 
        }

        div.style.display = "block";
        div.style.top = document.body.scrollTop;
    }
}

setInterval(function(){revealModal()},1000);

And your html buttons: 和您的html按钮:

<input id="Button1" type="button" value="Click heres" onclick="addID('1')" />
<input id="Button2" type="button" value="Click heres" onclick="addID('2')" />
<input id="Button3" type="button" value="Click heres" onclick="addID('3')" />

Now consider this - I don't really understand your request. 现在考虑一下-我不太了解您的要求。 It looks like a bunch of others don't understand either. 看来其他很多人也不了解。 The way I saw it was you want to reveal a div on click and make sure it updates every nth seconds. 我的观察方式是您要在点击时显示一个div,并确保它每n秒更新一次。 In this case, it looks like you chose 1000 ms, or every 1 second even though you said you wanted every 30 seconds. 在这种情况下,即使您说要每30秒一次,您似乎选择了1000 ms或每1秒一次。 Fair enough, change that 1000 to 30000. It looks like the ONLY update you do is make sure it sticks to a certain position on the screen and NOT updates the content. 相当公平,将1000更改为30000。看起来您所做的唯一更新是确保它坚持在屏幕上的某个位置并且不更新内容。

So what I've done is make the interval outside the function so it is always going. 因此,我要做的是在函数外部设置间隔,使其始终有效。 Then on click you push the id into the divs array where the interval will update only what is inside the loop. 然后单击,将id推入divs数组,在该数组中,间隔将仅更新循环内的内容。 While conceptually this will work, it seems like a bad way to do it. 虽然从理论上讲这是可行的,但这似乎是一种不好的方法。

You should just use an easy library like jquery and place a scroll listener that updates the position whether or not they're revealed. 您应该只使用一个简单的库(例如jquery),并放置一个滚动侦听器,以更新位置(无论是否显示它们)。 Seeing as you specified jquery in your tags but don't use a lick of it in your example, I assume that means you're not entirely familiar with the library. 看到您在标签中指定了jquery但在示例中没有使用它,我认为那意味着您并不完全熟悉该库。

This could be done by simply adding a class of "modal" to every modal div and using this: 只需在每个模式div中添加一类“模式”并使用以下代码即可完成此操作:

$(document).ready(function(){
    $(window).scroll(function(){
        $('.modal').css('top', $(window).scrollTop() + 'px');
    });

    $('.modal').on('click', function(){
        $(this).css('display', 'block');
    });
});

Of course you would need to call the jquery library before this call for this to work. 当然,在此调用之前,您需要先调用jquery库。

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

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