【问题标题】:How to Combine the Condition of PHP and CSS with JavaScript如何将 PHP 和 CSS 的条件与 JavaScript 结合起来
【发布时间】:2015-11-07 17:03:41
【问题描述】:

我为移动版制作了一个页面。我根据标题和屏幕大小做了一个条件。这是条件:

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?) 但不知道如何实现。

【问题讨论】:

    标签: javascript php css conditional


    【解决方案1】:

    也许尝试将类添加到您的 .postContent 中?通过 PHP 将其打印到 div 中。

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

    【讨论】:

    • 感谢@sko,您的代码工作到标题/字符的大小。但是屏幕尺寸条件呢?
    • 你不能用 PHP 做到这一点,因为 php 是服务器端语言 -> stackoverflow.com/questions/1504459/… 。在 JS 中,您将通过使用 document.body.clientWidthwindow.innerWidth 来获取此值。
    • 这是我的问题的核心 :) 如何将 PHP 和 CSS 的条件与 JavaScript 结合起来。我读过这篇文章:stackoverflow.com/questions/18890053/…。它说我们可以使用screenWidth。但我不知道它在 PHP + CSS + JavaScript 中的实现。
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多