简体   繁体   English

使用javascript垂直居中div

[英]using javascript to vertically center a div

I am trying to position a div vertically in the middle of a window in Wordpress. 我正在尝试在WordPress中的窗口中间垂直放置div。 I have a function.js file with the following code: 我有一个带有以下代码的function.js文件:

function setToCenter()
{   
    var a = document.getElementById("R_center");
    var h = window.innerHeight;

    var p = (h/2)-(650/2);      
    if( p < 80 ){ p = 80; }//limit to header height


    //a.style.transform = "transform(0,"+p+"px)";
    //a.style.margin-top= p+"px"; 
    a.style.paddingTop = p+"px";   
}

and a php file to get the post content: 和一个php文件来获取帖子内容:

<?php
/*
Template Name: Page R Center
*/
?>
<?php global $optimizer;?>
<?php get_header(); ?>

                <div id="R_center" class="R">

                        <script type="text/javascript"> setToCenter(); </script>

                        <?php if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
                        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">  

                                        <div class="thn_post_wrap">
                                            <?php the_content(); ?>
                                        </div>

                        </div>

                        <?php endwhile ?>

                </div><!--R_center class END-->

                        <?php endif ?>

<?php get_footer(); ?>

and in my header.php: 并在我的header.php中:

<script type="text/javascript" src="http://localhost/wp/wp-content/themes/r-child/assets/js/functions.js"></script>

evrything is working well. evrything运作良好。 The Div is positioned in the vertical middle of my screen. 该Div位于屏幕的垂直中间。 BUT because the Div is rendered after the java script it works only after refreshing the page. 但是,因为Div是在Java脚本之后呈现的,所以它仅在刷新页面后才起作用。 My question is where do I have to place my javascript to avoid refreshing? 我的问题是我必须在哪里放置JavaScript以避免刷新? Can I execute everything from my functions.js without calling function in my php? 我可以执行我的functions.js中的所有内容而无需在PHP中调用函数吗? thx 谢谢

In your case you need to launch setToCenter function after DOM is loaded by browser. 对于您的情况,您需要在浏览器加载DOM之后启动setToCenter函数。 You should wrap it into DOMContentLoaded event , example: 您应该将其包装到DOMContentLoaded event中 ,例如:

document.addEventListener("DOMContentLoaded", function(event) {
    setToCenter();
});

or if you use jQuery: 或者,如果您使用jQuery:

$(function(){
    setToCenter();
});

The easiest way is to place your function call near the footer even you can place it after the footer as well. 最简单的方法是将函数调用放在页脚附近,即使您也可以将其放在页脚之后。 this will solve you problem. 这将解决您的问题。 You have asked "can you place the code in function.js?" 您问过“可以将代码放在function.js中吗?” yes you can do this as well. 是的,您也可以这样做。 just place function call inside window.load function and put this code in function.js file. 只需将函数调用放在window.load函数内部,然后将此代码放入function.js文件即可。 Below are the example for both cases . 以下是这两种情况的示例。

$(window).load(function() { setToCenter(); });

OR 要么

<?php
/*
Template Name: Page R Center 
*/
?>
<?php global $optimizer;?>
<?php get_header(); ?>

            <div id="R_center" class="R">

                    <?php if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
                    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">  

                                    <div class="thn_post_wrap">
                                        <?php the_content(); ?>
                                    </div>

                    </div>

                    <?php endwhile ?>

            </div><!--R_center class END-->

                    <?php endif ?>
<?php get_footer(); ?>
<script type="text/javascript"> setToCenter(); </script>

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

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