@@ -4,14 +4,12 @@ $(document).ready(function () {
44} ) ;
55
66var aceEditor ;
7- var myWidth = 0 , myHeight = 0 ;
87var containExamples = true ;
98
109function initPage ( ) {
1110 initSideBar ( ) ;
1211 initEditor ( ) ;
1312 screenResize ( ) ;
14- // dragCode();
1513}
1614
1715function initSideBar ( ) {
@@ -24,23 +22,7 @@ function initSideBar() {
2422}
2523
2624function screenResize ( ) {
27- if ( typeof ( $ ( window ) . innerWidth ( ) ) == 'number' ) {
28- myWidth = $ ( window ) . innerWidth ( ) ;
29- myHeight = $ ( window ) . innerHeight ( ) ;
30- }
31- else if ( document . documentElement && ( document . documentElement . clientWidth || document . documentElement . clientHeight ) ) {
32- myWidth = document . documentElement . clientWidth ;
33- myHeight = document . documentElement . clientHeight ;
34- }
3525 window . onresize = function ( ) {
36- if ( typeof ( $ ( window ) . innerWidth ( ) ) == 'number' ) {
37- myWidth = window . innerWidth ;
38- myHeight = window . innerHeight ;
39- }
40- else if ( document . documentElement && ( document . documentElement . clientWidth || document . documentElement . clientHeight ) ) {
41- myWidth = document . documentElement . clientWidth ;
42- myHeight = document . documentElement . clientHeight ;
43- }
4426 mapHeight ( ) ;
4527 } ;
4628}
@@ -57,7 +39,6 @@ function initCodeEditor() {
5739 aceEditor . setValue ( $ ( '#editor' ) . val ( ) ) ;
5840 aceEditor . clearSelection ( ) ;
5941 aceEditor . moveCursorTo ( 0 , 0 ) ;
60-
6142}
6243
6344//初始化编辑器以及预览内容
@@ -113,41 +94,28 @@ function run() {
11394
11495//填充预览效果内容
11596function loadPreview ( content ) {
116- var iFrame = document . getElementById ( "innerPage" ) . contentWindow ;
117- iFrame . document . open ( ) ;
118- iFrame . addEventListener ( 'load' , mapHeight ) ;
119- iFrame . document . write ( content ) ;
120- iFrame . document . close ( ) ;
97+ var iFrame = createIFrame ( ) ,
98+ iframeDocument = iFrame . contentWindow . document ;
99+
100+ iframeDocument . open ( ) ;
101+ iframeDocument . write ( content ) ;
102+ iframeDocument . close ( ) ;
103+ iframeDocument . addEventListener ( 'load' , function ( ) {
104+ mapHeight ( ) ;
105+ } ) ;
121106 mapHeight ( ) ;
122107}
123108
124- /**设置显示源码的拖拽效果**/
125- function dragCode ( ) {
126- $ ( "#drag" ) . mousedown ( function ( ) {
127- document . onselectstart = function ( ) {
128- return false ;
129- } ;
130- document . onmousemove = function ( e ) {
131- var bottomX = ( e || window . event ) . clientX - 202 ;
132- if ( bottomX <= 0 ) {
133- bottomX = 0 ;
134- }
135- if ( bottomX >= myWidth - 202 ) {
136- bottomX = myWidth - 202 ;
137- }
138- $ ( "#codePane" ) . width ( bottomX ) ;
139- $ ( "#editor" ) . width ( bottomX ) ;
140- aceEditor . resize ( ) ;
141- $ ( "#previewPane" ) . css ( { 'left:' : ( myWidth - bottomX - 202 ) + "px" } ) ;
142- $ ( "#innerPage" ) . width ( myWidth - bottomX - 202 ) ;
143- aceEditor . resize ( ) ;
144- } ;
145- document . onmouseup = function ( ) {
146- document . onmousemove = null ;
147- $ ( "#overiframe" ) . hide ( ) ;
148- } ;
149- } ) ;
109+ function createIFrame ( ) {
110+ var preViewPane = $ ( "#previewPane" ) ;
111+ preViewPane . empty ( ) ;
112+ var iframe = document . createElement ( "iframe" ) ;
113+ $ ( iframe ) . attr ( "id" , "innerPage" ) ;
114+ $ ( iframe ) . attr ( "name" , "innerPage" ) ;
115+ preViewPane . append ( iframe ) ;
116+ return iframe ;
150117}
118+
151119//重置编辑器
152120function refresh ( ) {
153121 initEditor ( ) ;
@@ -191,27 +159,28 @@ function bindEvents() {
191159 } ) ;
192160 var codePane = $ ( "#codePane" ) ;
193161 var previewPane = $ ( "#previewPane" ) ;
194- var expand = ! 1 ;
162+ var expand = ! ! 1 ;
195163 $ ( "#showCodeBtn" ) . click ( function ( ) {
196164 if ( expand ) {
165+ //编辑器和预览宽度5:7
197166 $ ( this ) . text ( " 展开" ) ;
198167 $ ( this ) . addClass ( "fa-arrows-alt" ) ;
199168 $ ( this ) . removeClass ( " fa-compress" ) ;
200169 codePane . show ( 10 , function ( ) {
170+ previewPane . removeClass ( "col-md-12" ) ;
201171 previewPane . addClass ( "col-md-7" ) ;
202172 codePane . addClass ( "col-md-5" ) ;
203173 } ) ;
204-
205174 } else {
175+ //预览独占一行
206176 $ ( this ) . text ( " 源码" ) ;
207177 $ ( this ) . addClass ( " fa-compress" ) ;
208178 $ ( this ) . removeClass ( "fa-arrows-alt" ) ;
209179 codePane . hide ( 200 , function ( ) {
210180 codePane . removeClass ( "col-md-5" ) ;
211181 previewPane . removeClass ( "col-md-7" ) ;
182+ previewPane . addClass ( "col-md-12" ) ;
212183 } ) ;
213-
214-
215184 }
216185 expand = ! expand ;
217186 } ) ;
0 commit comments