从 jquery 访问 css 块的属性
我有一个严重基于 javascript 的网站,但我仍然想让它可以通过 CSS 轻松修改。
拿这个例子...有一个 div,它在悬停时通过 jQuery 修改了不透明度,但我希望设计者能够选择不透明度的值。
我正在考虑在 css 中创建一个块来声明这个常量,例如:
.constants_someid{
opacity: 0.5;
}
然后通过 jQuery 我采取这种不透明度,就像
var opacity = jQuery('css:constants_someid').attr('opacity');
我不知道这是否是执行这些常量声明的好方法,但这就是我想到的现在。
你们觉得呢?
谢谢,
乔
I have a site that is heavily based on javascript, but I still want to let it be easily modified by CSS.
Take this example... there is a div that it has its opacity modified when on hover through jQuery, but I would like to the designer to be able to choose the value of the opacity.
I was thinking on creating a block in css just to declare this constants, for example:
.constants_someid{
opacity: 0.5;
}
and then through jQuery I take this opacity like
var opacity = jQuery('css:constants_someid').attr('opacity');
I don't know if this is a good way to do these constants declaration, but thats what came to mind right now.
What you guys think?
Thanks,
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相反,在 Javascript 文件中定义它们:
这种语法对于设计者来说应该足够容易修改。然后您可以将其用作
settings.opacity
。然后,您可以使用
$.extend
来合并由设计器使用默认设置:Define them in a Javascript file instead:
That kind of syntax should be easy enough for a designer to modify. You can then use it as
settings.opacity
.You could then use
$.extend
to merge settings defined by the designer with your default settings:@lonesomdays 解决方案是显而易见的,而且是一个很好的解决方案。
这是我的看法;
要调用它,请使用
getConfig('constants_someid','opacity')
工作小提琴: http:// jsfiddle.net/5GWHU/
@lonesomdays solution is the obvious one, and a good one at that.
Here is my take;
To call it use
getConfig('constants_someid','opacity')
working fiddle: http://jsfiddle.net/5GWHU/
将它们定义为 CSS 类是一个很好的方法,因为正如您提到的,它可以更好地维护。
但是,我认为您不应该从单个 CSS
constants
类中提取各个属性。相反,定义多个有意义类并使用 jQuery 添加/删除它们。例如,用于动态添加的各个 CSS 类:
以及相关的 jQuery:
Defining them as CSS classes is a good method since, as you mentioned, it can be better maintained.
However, I don't think you should be pulling individual properties out of a single CSS
constants
class. Instead define multiple, meaningful classes and add/remove them using jQuery.For example, individual CSS classes for adding dynamically:
And the relevant jQuery: