我应该在此代码中添加什么以使弹出窗口在 10 秒后出现

What should I add to this code to make popup appear after 10 seconds

本文关键字:窗口 代码 添加 我应该 什么      更新时间:2023-09-26

>我需要访问者在我的页面上停留 5-10 秒,直到出现一个简单的弹出窗口。我尝试了 setTimeout 函数,因为关闭按钮在 30 秒后出现,但除了阴影之外的其他元素出现在页面外的错误位置。有什么想法在这里是正确的吗?这是我到目前为止的代码:

var shadow = $('<div id="shadowElem"></div>');
var speed = 1000;
$(document).ready(function() {
    $('body').prepend(shadow);
});
$(window).load( function() {
    screenHeight = $(window).height();
    screenWidth = $(window).width();
    elemWidth = $('#dropElem').outerWidth(true);
    elemHeight = $('#dropElem').outerHeight(true)
    leftPosition = (screenWidth / 2) - (elemWidth / 2);
    topPosition = (screenHeight / 2) - (elemHeight / 2);
    setTimeout(function() {
    $("#dropClose").show();
    }, 30 * 1000);
    $('#dropElem').css({
        'left' : leftPosition + 'px',
        'top' : -elemHeight + 'px'
    });
    $('#dropElem').show().animate({
        'top' : topPosition
    }, speed);
    shadow.animate({
        'opacity' : 0.7
    }, speed);
    $('#dropClose').click( function() {
        shadow.animate({
            'opacity' : 0
        }, speed);
        $('#dropElem').animate({
        'top' : -elemHeight + 'px'
    }, speed, function() {
            shadow.remove();
            $(this).remove();
        });            
    });
});

你应该

使用javascript setTimeout()方法在指定的毫秒数内延迟你想要延迟的任何内容。

你应该尝试

setTimeout(function() {
    $('#dropElem').show().animate({
        'top' : topPosition
    }, speed);
}, 10000);

而不是

$('#dropElem').show().animate({
    'top' : topPosition
}, speed);

那个工人,但由于阴影时间,屏幕一直保持黑暗,直到弹出窗口出现。我添加了

  setTimeout(function() {
  $("#shadowElem").show();
  }, 5 * 1000);

    display:none;

到 CSS 在 #shadowElem

此外,弹出动画的时间设置为 7 秒,因此两者之间的差异使其不那么令人沮丧