Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图使一个div出现在可滚动页面的中心-我的代码中有bug? 作为一个序言,以确保我是清楚的,我不希望DIV出现在页面中间的死点,我希望它在可视窗口的中间。因此,如果你想象一个长页面,用户向下滚动到接近底部并单击按钮,div将出现在靠近页面底部的屏幕中央_Javascript_Jquery - Fatal编程技术网

Javascript 试图使一个div出现在可滚动页面的中心-我的代码中有bug? 作为一个序言,以确保我是清楚的,我不希望DIV出现在页面中间的死点,我希望它在可视窗口的中间。因此,如果你想象一个长页面,用户向下滚动到接近底部并单击按钮,div将出现在靠近页面底部的屏幕中央

Javascript 试图使一个div出现在可滚动页面的中心-我的代码中有bug? 作为一个序言,以确保我是清楚的,我不希望DIV出现在页面中间的死点,我希望它在可视窗口的中间。因此,如果你想象一个长页面,用户向下滚动到接近底部并单击按钮,div将出现在靠近页面底部的屏幕中央,javascript,jquery,Javascript,Jquery,这是我的代码,在chrome中不起作用: function centerdiv() { var scrolledX, scrolledY; scrolledX = document.body.scrollLeft; scrolledY = document.body.scrolltop; var centerX, centerY; centerX = document.body.clientWidth; centerY = document.body.clie

这是我的代码,在chrome中不起作用:

                function centerdiv() {
var scrolledX, scrolledY;

scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrolltop;

var centerX, centerY;
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;

var leftoffset = scrolledX + (centerX - 100) / 2;
var topoffset = scrolledY + (centerY - 100) / 2;

 $('.current div[name="popup"]').css({'top' : topoffset + 'px', 'left':
 leftoffset + 'px'});} 


            $(function() {
        $("html").ajaxStart(function() {
           centerdiv();
            $(".current div[name=popup]").show();
        });
        $("html").ajaxComplete(function() {
            $(".current div[name=popup]").hide();
        });
    });

请注意,这是针对iphone移动网站的,附加到html的ajaxstart功能非常重要,因为它在iphone上不起任何作用。以下是我的解决方案,效果很好:

    var winH = $(window).height();
    var winW = $(window).width();
    $(this).css('top', winH/2 - $(this).height()/2);
    $(this).css('left', winW/2 - $(this).width()/2);
    $(this).show();

$(这)必须引用要显示在中间的DIV元素。

以下是我的解决方案,效果很好:

    var winH = $(window).height();
    var winW = $(window).width();
    $(this).css('top', winH/2 - $(this).height()/2);
    $(this).css('left', winW/2 - $(this).width()/2);
    $(this).show();
wh = $(window).height();
dh = $("#div").height();
$("#div").css("top",(wh - dh)/ 2 + $(window).scrollTop() + 'px');
$(此)必须引用要显示在中间的DIV元素

wh = $(window).height();
dh = $("#div").height();
$("#div").css("top",(wh - dh)/ 2 + $(window).scrollTop() + 'px');
编辑: 宽度不一样,实际上是:

ww = $(width).width();
dw = $("#div").width();
$("#div").css("left",(ww - dw)/ 2 + 'px');
但这真的取决于你是否有一个固定的视口,有很多方法使它居中

祝你好运

编辑: 宽度不一样,实际上是:

ww = $(width).width();
dw = $("#div").width();
$("#div").css("left",(ww - dw)/ 2 + 'px');
但这真的取决于你是否有一个固定的视口,有很多方法使它居中


祝你好运

您忘记将位置设置为绝对值,或固定值

$('.current div[name=“popup”]').css({'top':topoffset+'px','left':
leftoffset+'px','position':'absolute'});}

您忘记将位置设置为绝对值,或固定值

$('.current div[name=“popup”]').css({'top':topoffset+'px','left':
leftoffset+'px','position':'absolute'});}

如果您没有水平滚动,您可以使用直接CSS部分地进行滚动 div{ 左:50%; 左边距:-[box_width/2]px;
}

如果没有水平滚动,可以使用直接CSS进行部分滚动 div{ 左:50%; 左边距:-[box_width/2]px;
}

它是如何不起作用的?不显示div,放置不正确?你看到了什么结果?您是否尝试过调试,如果是的话,在非工作情况下,所有变量的值是多少?它是如何工作的?不显示div,放置不正确?你看到了什么结果?您是否尝试过调试,如果是,在非工作情况下,所有变量的值是多少?