2011-09-19 41 views
0

我使用這個代碼,以使#sidebar的高度匹配#post_content的身高:jQuery或CSS的問題?

<script type='text/javascript'> 
$().ready(function(){ 
    $('#sidebar').height($('#post_content').outerHeight(true)); 
}); 
</script> 

這適用於網頁:http://themeforward.com/demo2/,甚至沒有評論的帖子頁...但一旦意見被添加他們不再匹配高度:http://themeforward.com/demo2/2011/01/02/centered-and-captioned-image-longer-title/

爲了說明問題,我已將post_content div背景色設爲黑色,側邊欄背景色爲灰色。

這裏是我的CSS:

#container { 
    width:1126px; 
    margin:35px auto; 
    padding:0; 
    background:#000; 
    clear:both; 
    overflow:hidden 
} 
#sidebar { 
    display:inline-block; 
    float:right!important; 
    width:410px; 
    padding:0 0 0 25px; 
    overflow:hidden; 
    background:#AAA; 
    border-left:1px solid #EEE 
} 

這裏是我的comments.php:

<?php 

// Do not delete these lines 
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) 
die ('Please do not load this page directly. Thanks!'); 

if (post_password_required()) { ?> 
<p class="nocomments">This post is password protected. Enter the password to view comments.</p> 
<?php 
return; 
} 
?> 

<!-- You can start editing here. --> 

<?php if (have_comments()) : ?> 
<div class="responses"><?php comments_number('No Responses', 'One Response', '% Responses');?> to &#8220;<?php the_title(); ?>&#8221;<?php if (comments_open() && (4 <= get_comments_number())) : ?> 
<a href="#respond">»</a> 
<?php endif; ?> 
</div> 

<div id="commentlist"> 
<ul> 
<?php wp_list_comments('avatar_size=60'); ?> 
</ul> 
</div> 

<div id="more_posts_comments"> 
<div class="oe"><?php previous_comments_link() ?></div> 
<div class="re"><?php next_comments_link() ?></div> 
</div> 

<?php else : // this is displayed if there are no comments so far ?> 

<?php if ('open' == $post->comment_status) : ?> 
<!-- If comments are open, but there are no comments. --> 

<?php else : // comments are closed ?> 
<!-- If comments are closed. --> 
<p class="nocomments">Comments are closed.</p> 

<?php endif; ?> 
<?php endif; ?> 

<?php if ('open' == $post->comment_status) : ?> 

<!-- Respond to this --> 
<div id="respond"> 
<div class="responses"><?php comment_form_title('Leave a Reply', 'Leave a Reply to %s'); ?></div> 
<div class="cancel_comment"> 
<?php cancel_comment_reply_link('cancel comment'); ?> 
</div> 


<?php if (get_option('comment_registration') && !$user_ID) : ?> 
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p> 
<?php else : ?> 

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"> 

<!-- If the user is logged in... --> 
<?php if ($user_ID) : ?> 

<div class="logged-in"><p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a> - <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out</a></p></div> 

<?php else : ?> 

<!-- If the user isn't logged in, they need to fill out these forms... --> 
<div id="small_forms"> 
<input type="text" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Name (required)';" value="Name (required)" name="author" id="author" size="22" tabindex="1" /> 
<input type="text" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='E-Mail (required, hidden)';" value="E-Mail (required, hidden)" name="email" id="email" size="22" tabindex="2" /> 
<input type="text" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Website';" value="Website" name="url" id="url" size="22" tabindex="3" /> 
</div> 

<?php endif; ?> 

<div id="submit_spacer"><h6><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></h6></div> 

<!-- Submit button --> 
<h6><input name="submit" type="submit" value="Submit Comment" class="submit" /></h6> 

<h6><?php comment_id_fields(); ?></h6> 

<?php do_action('comment_form', $post->ID); ?> 

</form> 

<?php endif; // If registration required and not logged in ?> 
</div> 

<?php endif; // if you delete this the sky will fall on your head ?> 
enter code here 

回答

1

通過將側邊欄絕對定位到容器div,您可以輕鬆實現與純CSS的相同效果。試試這個:

地址:

#container { 
    position:relative; 
} 

#sidebar { 
    position:absolute; 
    top:0; 
    right:0; 
    bottom:0; 
} 
+0

+1。這可能是最好的解決方案,因爲他不需要#container除了position:relative; – skyuzo

+0

這有一個問題......如果頁面或帖子內容比側邊欄短,它會切斷側邊欄中的內容。有沒有辦法動態調整絕對定位的div? – siouxfan45

+0

你隱藏了「overflow:hidden」之後的內容..試試這個:刪除overflow:隱藏你的容器id和你的sidebar id,引入一個clearfix並且添加一個clearfix類到你的容器,我總是使用這一個:http://www.yuiblog.com/blog/2010/09/27/clearfix-reloaded-overflowhidden-demystified/ ...然後繼續嘗試我發佈上面,我只是測試它,它工作正常。 –

0

作出這樣的設置高度未功能。
例如:

function setHeight(){ 
    $('#sidebar').height($('#post_content').outerHeight(true)+'px'); 
} 

呼叫頁準備
此功能,當你在#post_content添加的內容再次調用它。

+0

如何做呢?我對jQuery和PHP非常陌生。 – siouxfan45

+0

您也可以在'resize()'事件處理程序中調用該函數。然後,你不需要跟蹤它的大小何時改變。 – skyuzo

+0

我不親自編寫jQuery,我不知道如何解決這個問題... – siouxfan45

0

您是否嘗試過使用clearfix元素爲您替代?這樣你就不必動態追蹤高度的變化。儘管如此,您仍然需要將它與虛假背景結合使用。

另一方面,不應該是$(function(){})或$(document).ready(function(){})$()。ready(function(){}) 。

+0

我不認爲clearfix會有幫助。他希望將側欄調整到容器的整個高度,而不是將容器調整到浮動的兒童的整個高度。 – skyuzo

+0

我已經嘗試過使用clearfix,但它沒有。感謝您的幫助! – siouxfan45

+0

哦,對,編輯。 – kidoman