gpt4 book ai didi

javascript - 获取 Jquery 模态框以显示在页面加载时

转载 作者:行者123 更新时间:2023-12-02 20:35:35 25 4
gpt4 key购买 nike

我需要让这个 Jquery 框出现在页面加载时。目前,它仅在用户单击链接时出现。

这是触发器:

    <script type="text/javascript"> 

$(document).ready(function() {

//select all the a tag with name equal to modal
$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();

//Get the A tag
var id = $(this).attr('href');

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});

//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);

//transition effect
$(id).fadeIn(2000);

});

//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();

$('#mask').hide();
$('.window').hide();
});

//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});

});

</script>

我确信这里一定有某种解决方案,有什么想法吗?

最佳答案

您可以在必要的链接上触发点击事件

$(window).load(function() {
$('a#initialModalLink[name=modal]').trigger("click");
});

其中initialModalLink是应执行模态窗口的链接的ID。另外(这会更好)您可以将模态窗口打开提取到单独的函数中并使用它:

function openModal(id) {

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});

//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow", 0.8);

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

//Set the popup window to center
$(id).css('top', winH / 2 - $(id).height() / 2);
$(id).css('left', winW / 2 - $(id).width() / 2);

//transition effect
$(id).fadeIn(2000);

}

$(document).ready(function() {

$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
openModal($(this).attr('href'));
});

openModal('the href of initial modal box');

});

关于javascript - 获取 Jquery 模态框以显示在页面加载时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3320864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com