使用JS以编程方式加载CSS [英] Programmatically load CSS using JS

查看:121
本文介绍了使用JS以编程方式加载CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现以下:


  • 我有一个服务器端脚本,根据GET参数生成CSS代码

  • 在用户请求时,JS现在应该执行以下操作

    • 加载新的CSS文件

    • I have a server-side script that generates CSS code depending on GET parameters
    • On user request a JS should now do the following
      • Load a new CSS file
      • When loading is done, fade to the newly loaded style

      问题在这里是最后一个

      向DOM中添加新的CSS文件没有问题,但我如何知道浏览器何时完成加载文件
      在文件实际加载之前,我无法使用新加载的样式启动动画。

      It is no problem to add a new CSS file to the DOM, but how do I know when the browser finished loading the file? I cannot start animations using the newly loaded styles until the file is actually loaded.

      或者:是否可以使用Async请求加载CSS文件,并使用Javascript将CSS代码注入DOM,而无需手动解析?

      Alternatively: Is it possible to load a CSS file using Async Requests, and inject the CSS code into the DOM using Javascript without parsing it by hand?

      非常感谢!

      Dennis

      推荐答案

      这种过滤器有一个很好的库...

      there are a nice library for this kind of filters...

      也许你可以试试:


      是不是

      Yepnope是一个超快的异步条件资源加载器,只允许加载用户需要的脚本。

      Yepnope is an asynchronous conditional resource loader that's super-fast, and allows you to load only the scripts that your users need.

      这篇关于使用JS以编程方式加载CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆