简体   繁体   English

将css添加到自定义可重用功能

[英]adding css to custom reusable function

i created a function in jQuery which lets me create a lightbox as following: 我在jQuery中创建了一个函数,它允许我创建一个灯箱,如下所示:

var lightbox = create_lightbox(params);

I would like to expand this function so i can add custom css to it and make it look like a feel in any given circumstance. 我想扩展这个功能,所以我可以添加自定义css,使它看起来像在任何给定的情况下的感觉。 I would prefer to be able to do the folowing: 我希望能够做到这一点:

    lightbox.css = {
    "background-color" : "red",
    etc...

}

What would be the best way to do this and how would i iterate over the css elements inside my function? 什么是最好的方法,我将如何迭代我的函数内的CSS元素?

tyvm tyvm

Create your function to take an object with all the values you want to add to your lightbox's css property, then add in your defaults with jQuery's extend. 创建函数以获取一个对象,其中包含要添加到灯箱的css属性中的所有值,然后使用jQuery的extend添加默认值。

function create_lightbox(params, css){
    css = $.extend({ "background-color": "default val"}, css);

    lightbox.css = css;
}

If you really want to loop over all the properties in css, you could use a for in loop 如果你真的想要遍历css中的所有属性,你可以使用for循环

function create_lightbox (params, css) {
   css = $.extend({ "background-color": "default val"}, css);

   for (cssKey in css)
     lightbox.css[cssKey] = css[cssKey];
}

Just use CSS as CSS should be used: In a <style>-tag or a linked .css-file. 只需使用CSS,因为应该使用CSS:在<style> -tag或链接的.css文件中。 The lightbox plugin uses the following dom tree; lightbox插件使用以下dom树;

<div id="jquery-lightbox">
    <div id="lightbox-container-image-box">
        <div id="lightbox-container-image">
            <img id="lightbox-image" />
            <div id="lightbox-nav">...</div>
        </div>
    </div>
</div>

So then style what you want to style. 那么你想要风格的风格。 I guess you dpn't want to use different styles on each of your boxes? 我想你不想在每个盒子上使用不同的款式?

You can add an element property to the lightbox class so you can modify value out of the constructor: 您可以向灯箱类添加元素属性,以便可以从构造函数中修改值:

function lightbox() {
    this.element = document.ccreateElement("div")
    this.light = "light";
    this.box = "box";
}

var newLightBox = new lightbox();
newLightBox.element.style.background = "#000";

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

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