【发布时间】:2015-09-14 02:23:22
【问题描述】:
我试图在 Wordpress 的窗口中间垂直放置一个 div。 我有一个带有以下代码的 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";
}
还有一个获取帖子内容的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(); ?>
在我的 header.php 中:
<script type="text/javascript" src="http://localhost/wp/wp-content/themes/r-child/assets/js/functions.js"></script>
一切都运作良好。 Div 位于我屏幕的垂直中间。但是因为 Div 是在 java 脚本之后呈现的,所以它仅在刷新页面后才起作用。 我的问题是我必须在哪里放置我的 javascript 以避免刷新?我可以从我的functions.js 执行所有内容而不在我的php 中调用函数吗? 谢谢
【问题讨论】:
-
您可以使用 CSS 垂直对齐到中间
标签: javascript php css wordpress