LESS 问题(范围和显式节点名称)
有没有办法绕过 LESS 范围?它变得烦人了。基本上,我有一个定义背景、边框等的 .text-box 。然后,在一个小节中进行一次性更改以添加 margin-top: .text-box { margin-top: 10px }。现在我无法在该部分中使用 .text-box 并获取原始的框样式;相反,我得到的只是边距顶部。如何才能在层次结构中获得更高的定义?我想我可以让它成为一个函数,并在两个地方调用该函数,但由于我使用的是 LESS,所以我想做更少的事情并 KISS。在 PHP 中,您可以使用 / 前缀访问全局命名空间,或者在 C++ 中使用 :: 前缀。
此外,似乎任何带有节点名称的定义都不适用于原型设计。意思是,我不能将其声明为 ul.products,然后使用 ul.categories { ul.products }。我必须省略节点名称才能重新使用它。含义:.categories {.products}。这是疏忽/不可能吗?
谢谢
Is there any way to bypass LESS scoping? It's becoming annoying. Basically, I have a .text-box which defines background, border, etc. Then, in a sub-section there's a one-off change to add a margin-top: .text-box { margin-top: 10px }. Now I can't use .text-box within that section and get my original box styles; instead, all I get is the margin-top. How can I get the definition higher in the heirarchy? I suppose I could make it a function, and call that function in both places, but being that I'm using LESS, I want to do less and KISS. In PHP, you'd get to the global namespace by using / prefix, or in C++ using :: prefix.
Additionally, it doesn't seem like any definitions with the node name work for prototyping. Meaning, I can't declare it ul.products, and then use ul.categories { ul.products }. I have to omit the node name in order to re-use it. Meaning: .categories { .products }. Is this an oversight/impossibility?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,假设您已经定义了 mixin,例如:
现在您想要添加属性或在某个小节中修改它,然后只需执行以下操作:
...将您的 mixin 放置到位,并添加一些您需要的属性添加(这将覆盖 mixin 本身的任何属性,但请确保在调用 mixin 后定义覆盖属性。
这不是理想的解决方案,因为它在输出 css 文件中创建两个声明(将有一个来自 mixin 的声明,后跟你在 .sub_section 中定义的那个),但除此之外,除了定义参数混合之外,我不知道这个问题的解决方案。
-
你的第二个问题 - 我认为 less 不故意支持范围有限的定义。 ..如果你真的需要知道特定标签要使用某些mixin,我会像这样处理它:
你还可以定义一个包并从那里调用东西:
我希望我做对了..?
ok so let's say you've got your mixin defined, for example:
now you want to add property or modify it in some subsection, then simply do this:
...which is putting your mixin in place, and adding some property you need to add (which will override any property from the mixin itself BUT make sure the overriding property is defined AFTER the mixin is called.
it's not ideal solution, as it creates two declarations in the output css file (there will be one from mixin followed by the one you defined in .sub_section), but otherwise I don't know a solution to this problem other than defining a parametric mixin..
--
your second issue - I think that less doesn't support scope-limited definitions on purpose... if you really need to know that certain mixin is to be used by a specific tag, I would deal with it like so:
you can also define a bundle and call stuff from there:
i hope i got it right.. ?