2015-08-14 60 views
0

我爲移動版本創建了一個頁面。我根據屏幕的標題和大小制定了一個條件。這裏是條件:如何將PHP和CSS的條件與JavaScript結合起來

If the Title > 17 characters and screen size < 392px 
then do: 
.postContent img { 
    bottom: 65px; 
} 

else and screen size > 391px 
.postContent img { 
    bottom: 50px; 
} 

下面是圖片預覽:http://i.imgur.com/59IG7UG.png (對不起,我至少需要10聲譽張貼圖片)

的第一篇文章應該圖像定位爲圖像的第二篇文章。

如何結合PHP和JavaScript來改變CSS:

<?php if (strlen(trim($post->getTitle())) > 17): ?> 
<script type="text/javascript"> 
    //do something 
</script> 
<?php else: ?> 
<script type="text/javascript"> 
    //do something 
</script> 
<?php endif; ?> 

我已閱讀本:PHP conditions depending on window width (media queries?) 但不知道如何實現它。

回答

0

也許嘗試添加類到您的.postContent?通過使用PHP將其打印到div中。

<div class="postContent <?php 
    if (strlen(trim($post->getTitle())) > 17) print 'bot65'; 
    else print 'bot50'; ?> 
"> 
    // content 
</div> 
+0

謝謝@sko,您的代碼工作,以標題/字符的大小。但是屏幕尺寸條件呢? –

+0

你不能這樣做與PHP becouse php是服務器端語言 - > http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php。在JS中,您將通過使用「document.body.clientWidth」或「window.innerWidth」來獲取此值。 – Sko

+0

這是我的問題的核心:)如何結合PHP和CSS的條件與JavaScript。我讀過這篇文章:http://stackoverflow.com/questions/18890053/php-conditions-depending-on-window-width-media-queries。它說我們可以使用screenWidth。但我不知道它在PHP + CSS + JavaScript中的實現。 –

相關問題