File tree 4 files changed +119
-0
lines changed
4 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ ( function ( win ) {
2
+ var doc = win . document ,
3
+ docBody = doc . body ,
4
+ createLink = function ( src ) {
5
+ var link = doc . createElement ( 'link' ) ;
6
+ link . type = 'text/css' ;
7
+ link . rel = 'stylesheet' ;
8
+ link . href = src ;
9
+ return link ;
10
+ } ,
11
+ resolveClassName = function ( moduleName ) {
12
+ var parts = moduleName . split ( '/' ) ;
13
+ return parts [ parts . length - 1 ] . replace ( '.' , '-' ) + '-loaded' ;
14
+ } ;
15
+
16
+ define ( {
17
+ load : function ( name , req , load ) {
18
+ var link = createLink ( name ) ,
19
+ head = doc . getElementsByTagName ( 'head' ) [ 0 ] ,
20
+ test , interval ;
21
+
22
+ test = doc . createElement ( 'div' ) ;
23
+ test . className = resolveClassName ( name ) ;
24
+ test . style . cssText = 'position: absolute;left:-9999px;top:-9999px;' ;
25
+ docBody . appendChild ( test ) ;
26
+
27
+ head . appendChild ( link ) ;
28
+
29
+ interval = win . setInterval ( function ( ) {
30
+ if ( test . offsetHeight > 0 ) {
31
+ clearInterval ( interval ) ;
32
+ docBody . removeChild ( test ) ;
33
+ load ( link ) ;
34
+ }
35
+ } , 50 ) ;
36
+ }
37
+ } ) ;
38
+ } ) ( window ) ;
Original file line number Diff line number Diff line change
1
+ # test {
2
+ background-color : # AA0000 ;
3
+ color : white;
4
+ font-weight : bold;
5
+ }
6
+
7
+ .sample-css-loaded {
8
+ height : 1px ;
9
+ }
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta http-equiv ="Content-Type " content ="text/html;charset=UTF-8 "/>
5
+ < style >
6
+ body {
7
+ font-family : sans-serif;
8
+ }
9
+ # test {
10
+ margin : 1em 0 ;
11
+ padding : .5em ;
12
+ border : 1px solid black;
13
+ }
14
+ </ style >
15
+ </ head >
16
+ < body >
17
+ < h1 > Loading CSS with Require JS demo</ h1 >
18
+ < div > < button id ="go "> Test</ button > </ div >
19
+ < div id ="test ">
20
+ < p > After you click the "Test" button, the background color of this element should change to red.</ p >
21
+ </ div >
22
+
23
+ < script src ="require.js "> </ script >
24
+
25
+ < script >
26
+ ( function ( win ) {
27
+ var doc = win . document ,
28
+ button = doc . getElementById ( 'go' ) ;
29
+ button . addEventListener ( 'click' , function ( ) {
30
+ require ( [ 'css!css/sample.css' ] , function ( ) {
31
+ alert ( 'Stylesheet has been loaded' ) ;
32
+ } ) ;
33
+ } , false ) ;
34
+ } ) ( window ) ;
35
+ </ script >
36
+ </ body >
37
+ </ html >
You can’t perform that action at this time.
0 commit comments