简体   繁体   English

如何将媒体规则动态添加到元素

[英]How to add media-rules dynamically to a element

I want to add some media-rules flexible to some of the elements in a website. 我想添加一些适用于网站中某些元素的媒体规则。 If I click an element a kind of toolbar will be opened to change the css-styles for a specific media-rule. 如果单击某个元素,则会打开一种工具栏,以更改特定媒体规则的CSS样式。 So far so good... 到现在为止还挺好...

My first idea was simple, to do it with inline-style but media rules could not use in inline-style. 我的第一个想法很简单,就是使用内联样式,但是媒体规则不能以内联样式使用。

So I have to look for another way how I can add dynamically this rules. 因此,我必须寻找另一种方法来动态添加此规则。

Maybe I create a new style-set dynamicalls. 也许我创建了一个新的样式集dynamicall。

<style>
@media screen and  (min-width:920px) {}
@media screen and  (min-width:720px) {}
@media screen and  (max-width:580px) {}
</style>

But, I don't know... 1) How I can add new classes exactly to this new style-set (I have other style-declaration, and all in the style-tag without a id to use as a selector.) 2) If I find a way to select this style-set, how can I put the new classes into the wanted media rule - wich kind of selector I have to use? 但是,我不知道... 1)如何将新类完全添加到此新样式集中(我还有其他样式声明,并且所有样式标签中都没有ID用作选择器。) 2)如果我找到一种选择此样式集的方法,如何将新类放入所需的媒体规则中,即必须使用哪种选择器?

As an example, I want to add this new class ".newclass{font-size:12px;}" to the media ruls "min-width:920px " 例如,我想将此新类".newclass{font-size:12px;}"到媒体规则"min-width:920px

$("UNKOWN_SELECTOR").html/append/something_else("\
    .newclass {\
        font-size: 12px;\
    }")
    .appendTo("UNKONWN_SECOND_SELECTOR_TO_MEDIARULE");

At the end I want to have this result 最后我想得到这个结果

 <style>
    @media screen and  (min-width:920px) { .newclass{font-size:12px;} }
    @media screen and  (min-width:720px) {}
    @media screen and  (max-width:580px) {}
    </style>

I hope someone can tell me a easier way, to solve this. 我希望有人可以告诉我更简单的方法来解决这个问题。

thanks a lot. 非常感谢。

You can create classes seperately for each media and add that classes to your element with media-query . 您可以为每种媒体分别创建类,然后使用media-query将这些类添加到元素中。

For example, 例如,

.desktop {
     /* styles */
}


.mobile {
     /* styles */
}

Add one of those classes to the element with media-query 使用media-query将其中一个类添加到元素中

@media(max-width: 960px){
    /* add desktop class to your specific element */
}

@media(max-width: 480px){
    /* add mobile class to your specific element */
}

Of course you can mix it with your event handler such as onClick() 当然,您可以将其与事件处理程序(例如onClick()

You should define all mandatory class names in your styles and use addClass and removeClass to apply new rules to elements. 您应该在样式中定义所有必需的类名称,并使用addClassremoveClass将新规则应用于元素。

Please check this snippet and inspect element after click and before click and check its class name: 单击后和单击前,请检查此代码片段并检查元素,然后检查其类名称:

 $(document).ready(function(){ $("a").click(function(){ $(this).removeClass("defaultClass").addClass("newClass"); }) }) 
 .defaultClass{ font-size:20px; } .newclass{ font-size:20px; } @media screen and (min-width:920px) { .newclass{font-size:12px;} } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="defaultClass">Change Font size by clicking Here</a> 

Edit: If you dont want to predefine all classes then you can use insertRule to create new css rules on the fly and then use the method I described. 编辑:如果您不想预定义所有类,则可以使用insertRule创建新的CSS规则,然后使用我描述的方法。 Please check this Q/A for more details. 请检查此问题以了解更多详细信息。

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

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