【问题标题】:Uniqiue background on every page每页都有独特的背景
【发布时间】:2012-06-14 16:16:53
【问题描述】:

我正在使用 Drupal 7。page.tpl.php 文件中的我的 bg 图像和图像获取到 css。

我的html:

<div class="mainimage" id="mainimg" class="clearfix"></div>

还有 CSS:

#mainimg {
    background: url("../img/mainimg.jpg") repeat scroll 0 0 transparent;
    height: 500px;
    left: 50%;
    margin-left: -640px;
    position: absolute;
    width: 1280px;
    z-index: 1;
    top:0;
}

我的背景图片看到了所有页面。这很正常。但我想在每一页上更改我的 bg 图像。 例如

...
mysite.com/index.php -bg image: mainimg.jpg
mysite.com/news -bg image:news.jpg
mysite.com/about -bg image: about.jpg
...

我该如何解决这个问题?

【问题讨论】:

    标签: php jquery css drupal-7


    【解决方案1】:

    您可以使用built-in Drupal functionsbody 添加唯一标识符,这样您就可以在CSS 中单独定位每个页面。

    例如,如果正文有id="news",您可以通过将其添加到您的 CSS 来更改背景图片:

    #news #mainimg {
        background-image: url("../img/news.jpg");
    }
    

    【讨论】:

      【解决方案2】:

      每个页面使用不同的类。

      mysite.com/news

      HTML

      <div class="mainimage news-bg" id="mainimg" class="clearfix"></div>
      

      CSS

      .news-bg {
          background: url("../img/news-bg.jpg") repeat scroll 0 0 transparent;
      }
      

      mysite.com/about

      HTML

      <div class="mainimage about-bg" id="mainimg" class="clearfix"></div>
      

      CSS

      .about-bg {
          background: url("../img/about-bg.jpg") repeat scroll 0 0 transparent;
      }
      

      【讨论】:

      • 我有一个模板文件。 page.tpl.php
      【解决方案3】:

      使图像生成 php。
      1) 设置标题("content-type: image/jpg");
      2)检查请求来自哪个页面并选择图像
      3) 回显图像的二进制数据。

      假设这是在bg.php文件中,你必须设置background-image:url('bg.php');

      例子:

      function currentPageName() {
          return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
      }
      if(currentPageName() == "news.html")
          $imagedata = file_get_contents('path/to/news.jpg');
      //etc
      
      header('Content-type: image/jpg');
      echo $imagedata;
      

      【讨论】:

      • 我不完全确定该标题,如果正确,有人可以评论吗?
      • 我不会推荐这个解决方案。它可能会做你想要的,但它是矫枉过正的。使用 MrSlayer 和 tbowman 提供的 CSS 解决方案。如果您有一个非常好的用例,例如合成图像或更改图片中的文本,则应该使用 php 生成图像。在这种情况下,您没有这样做;您只是在使用已有的独特图像。
      • @user1213807 这里是例子!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多