Is it possible to set the equivalent of a src attribute of an img tag in CSS?

后端 未结 25 2737
借酒劲吻你
借酒劲吻你 2020-11-22 09:30

Is it possible to set the src attribute value in CSS? At present, what I am doing is:


25条回答
  •  情书的邮戳
    2020-11-22 10:09

    Put several images in a "controlling" container, and change the container's class instead. In CSS, add rules to manage images' visibility depending on the container's class. This will produce the same effect as changing img src property of a a single image.

    HTML:

    
        
        
        
    
    

    CSS:

    #light         { ... }
    #light         *        { display: none; }     // all images are hidden
    #light.red     .red     { display: inline; }   // show red image when #light is red
    #light.yellow  .yellow  { display: inline; }   // .. or yellow
    #light.green   .green   { display: inline; }   // .. or green
    

    Note that it will preload all images, like with CSS backround-images, but unlike changing img src via JS.

提交回复
热议问题