简体   繁体   English

Tumblr: How to control CSS with post tagging (UPDATE: Working Method without JQuery!)

[英]Tumblr: How to control CSS with post tagging (UPDATE: Working Method without JQuery!)

Seen this done before, am curious as to how it is done.以前看过这个,很好奇它是怎么做的。 Example can be found over at http://wordographic.info/示例可以在http://wordographic.info/ 找到

For example, if I tag a post blue, the bg-color of the post turns blue, etc.例如,如果我将帖子标记为蓝色,则帖子的背景颜色会变为蓝色,等等。

Anyone know how this is done?有谁知道这是怎么做到的?

Thanks.谢谢。

Found a way to do this with only HTML/CSS.找到了一种仅使用 HTML/CSS 的方法。 Pretty simple, just add the {Tag} block to any div class wrapping the post area but make sure it's between {block:Posts} and {block:Text} etc. Now whatever you tag a post now becomes a new class.非常简单,只需将 {Tag} 块添加到包含帖子区域的任何 div class 中,但确保它位于 {block:Posts} 和 {block:Text} 等之间。现在,无论您标记帖子,现在都会变成新的 class。

{block:Posts}
  {block:Text}
    <div class="post {block:HasTags}{block:Tags}{Tag} {/block:Tags}{/block:HasTags}">
      {block:Title}<h2>{Title}</h2>{/block:Title}
      <p>{Body}</p>
    </div>
  {/block:Text}
{/block:Posts}

Pay attention to the third line down.注意向下的第三行。 it is important to add a space after {Tag} otherwise they won't be seperated in the HTML.在 {Tag} 之后添加一个空格很重要,否则它们不会在 HTML 中分开。

The CSS would look like this: CSS 看起来像这样:

.post { /* default style */
    background: #ccc;
    float: left;
    margin: 10px;
    position: relative;
    }
.blue { /* when tagged blue, use this style */
    background: blue !important; 
    }

Works, Pretty simple, no jquery required!工作,非常简单,不需要 jquery!

Thanks Blender, wouldn't have thought of this for some reason if I didn't read your jquery method:)谢谢Blender,如果我没有阅读您的jquery方法,出于某种原因不会想到这一点:)

You want to get the post's tags as class names so you can style posts with CSS, and there is a variable you can use for this purpose.您希望将帖子的标签作为 class 名称,以便您可以使用 CSS 为帖子设置样式,并且有一个变量可以用于此目的。 In your template simply use {TagsAsClasses} .在您的模板中只需使用{TagsAsClasses} This will render HTML friendly class names.这将呈现 HTML 友好的 class 名称。

An HTML class-attribute friendly list of the post's tags.帖子标签的 HTML 类属性友好列表。 Example: "humor office new_york_city"示例:“幽默办公室 new_york_city”

For detailed explanation see Post chapter in Tumblr docs.有关详细说明,请参阅 Tumblr 文档中的帖子章节

With jQuery, anything's possible, This isn't going to work right away: so tweak it for your theme:使用 jQuery,一切皆有可能,这不会立即奏效:因此请针对您的主题进行调整:

$('.post-class .tag-container .tag').each(function() {
  $(this).closest('.post-class').addClass($(this).text());
});

It is nothing to do with JS, such things are done on server-side.跟 JS 无关,都是在服务端做的。 Depends on tags some properties are set to posts and then they are taken into consideration while rendering them to HTML.取决于标签,一些属性设置为帖子,然后在将它们呈现为 HTML 时将它们考虑在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM