资源描述
1. 提供一个兼容的css,里面把所有兼容的可能行都写进去,这样页面只需请求一次,就可以全站通用一个css。节省了网络的带宽,同时减少http的请求。
2. 写出几套响应的css ,分别针对不同的浏览器版本,然后通过js判断浏览器的版本,动态的加载相应的css文件,当然这里文件的大小也许变小了,请求的响应速度快了,但是对于 不同的浏览器来说,他们的需要不同的请求,所以对于客户端的影响还是有存在的。但是这个可以忽略,毕竟现在的客户端配置已经相当的高了。同时这种方案是 css的风格比较同一,比较便于预览,不会出现那么的*, html等等前缀,来注释这些是兼容什么浏览器的代码.
对上述两种方案我推崇第二种,但是第二种方案的弊端就是css的代码量增加了..这是相对比较悲剧的。
同 理js方面,现在流行的多种框架又是我们面临的选择,多亏有了jquery的诞生让web程序员们从冗长的代码中得到解放,可是针对jquery的80K 左右的文件大小,有些门户依旧无法忍受,毕竟他们的页面越小轻量越好,这就牵涉到用原生态的js了,这就要求我们程序员需要具备这个技能。好在,现在的门 户在页面呈现的时候没有太多复杂的js对象操作,根据一般的js操作都可以满足。
针对以上情况我依旧有两种方案,
1. 针对要求性能特别高的模块,建议用原生态的js操作+漂亮的html + css;
2. 针对相对响应速度不是太高的页面,建议用jquery, prototype之类的js框架,毕竟他大大的提升了程序员的开发速度。
//建议,这里大家锻炼一下js的性能优化,这才是提升页面js执行性能的王道。
以上是web前端所要考虑的主要方面,但是按我的理解,如果真正提升一个网站的印象,除了不俗的页面响应及外观之外,优秀的用户体验才是web的王道。例如页面的搜索框的摆放位置,页面幻灯片的显示方式及位置,以及产品的展示方式都是需要考虑的。
动态加载外部CSS与JS文件使用dom创建<script>或者<link>标签,并给他们附加属性,如type等。然后使用appendChild方法把标签绑定到另一个标签,一般是绑到<head>。
# e3 h8 P, C$ C4 q; g三维网技术论坛 应用:三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxa, t! ]0 k/ c( [4 B/ i* w1 R
1、提高代码的复用,减少代码量;三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江 X6 h4 x8 @5 ^5 y0 u7 k s
2、添加一个javascript控制器和 session可以实现动态改变页面样式;
. S6 ?5 n+ v2 l三维网技术论坛 3、由于是页面是从上到下依次加载文件的,并且边加载边解释,所以可以添加javascript控制器控制页面文件的加载顺序,如先加载css布局文件,再显示有图片的css美化文件,之后再加载大的falsh文件,或者安内容的重要性来加载。三维网技术论坛2 l3 f; s. D( }- q! O8 Y7 v
To load a .js or .css file dynamically, in a nutshell, it means using DOM methods to first create a swanky new "script" or "LINK" element, assign it the appropriate attributes, and finally, use element.appendChild() to add the element to the desired location within the document tree. It sounds a lot more fancy than it really is. Lets see how it all comes together: 6 H$ u) b7 k& r; I( Z4 O
2 u! H: l* S0 ~. `# R% w三维网技术论坛 接下来的工作是绑定到<head>标签。绑定的时候有一个问题就是同一个文件有可能被我们绑定两次,绑定两次浏览器也不会出现异常,但是效率就低了。为了避免( o# _/ d$ z* p) M
这种情况我们可以新增一个全局数组变量,把绑定的文件名字保存在里面,每次绑定前先检查一下是否已经存在,如果存在就提示已经存在,如果不存在就绑定。3 I6 `! T3 G) j% x6 u. n$ M0 V1 F
document.getElementsByTagName("head")[0].appendChild(fileref) 三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江; Z" {5 {$ Z+ C1 z8 h- ]( J5 ]
By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag. Furthermore, you should be aware that no existing element is harmed in the adding of the new element- that is to say, if you call loadjscssfile("myscript.js", "js") twice, you now end up with two new "script" elements both pointing to the same Javascript file. This is problematic only from an efficiency standpoint, as you'll be adding redundant elements to the page and using unnecessary browser memory in the process. A simple way to prevent the same file from being added more than once is to keep track of the files added by loadjscssfile(), and only load a file if it's new:
' ]9 a# W* z4 N# k# W! r! X" C( O3 Rvar filesadded="" //保存已经绑定文件名字的数组变量 三维网技术论坛+ ?1 {3 L) e3 U
function checkloadjscssfile(filename, filetype){ ( B! Y, ]4 [$ w8 j: h, i
if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判断数组里是否有某一项
+ \4 H9 p% n* e+ I6 X, U# y8 r" } loadjscssfile(filename, filetype) * J, j! x) G |! L, I/ F
filesadded+="["+filename+"]" //把文件名字添加到filesadded
& E9 R2 l6 }6 |+ `3 _三维网技术论坛}
0 K: B; b& N: s7 f* welse 3 a" x* }. g& V
alert("file already added!")//如果已经存在就提示
, t' V' g: a. k( ~) F, L/ U+ J} 三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江/ g5 f/ l8 E' k" r6 L
checkloadjscssfile("myscript.js", "js") //success
. m$ z# i0 k( m$ W( k. H; z: scheckloadjscssfile("myscript.js", "js") //redundant file, so file not added
% {% d( k+ j& U+ }2 N三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxa Here I'm just crudely detecting to see if a file that's set to be added already exists within a list of added files' names stored in variable filesadded before deciding whether to proceed or not. 三维网技术论坛' I/ |: S0 x- o+ ^
Ok, moving on, sometimes the situation may require that you actually remove or replace an added .js or .css file. Lets see how that's done next. $ t2 M, e1 S* K% \. j$ @5 V8 o. K
3 V v# H. q9 N, j4 C: E三维网技术论坛function loadjscssfile(filename, filetype){
, B# w' }- O8 X s* g4 H: ?" qif (filetype=="js"){ //判断文件类型 三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxa0 t6 m1 g7 E/ o, w
var fileref=document.createElement('script')//创建标签 7 n5 ?3 J# p8 j7 r+ L s
fileref.setAttribute("type","text/javascript")//定义属性type的值为text/javascript
5 F* W( }3 o7 Y- t4 X/ @三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江 fileref.setAttribute("src", filename)//文件的地址
! P) z& N/ T8 _7 X& _0 H三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxa} 三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江8 f3 w$ h, j0 i4 A6 w: {7 c* l7 x
else if (filetype=="css"){ //判断文件类型 : m( e9 s8 u7 P* i
var fileref=document.createElement("link") 三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxa& {( t" E% y9 W& |+ V
fileref.setAttribute("rel", "stylesheet") 6 d9 Z# g) U( `7 E
fileref.setAttribute("type", "text/css") 1 f8 @& q+ i, U7 u
fileref.setAttribute("href", filename)
U4 y, _5 H7 n$ Q$ O/ }
1 E7 b/ B& b2 ]+ l+ L三维,cad,机械,技术,汽车,catia,pro/e,ug,inventor,solidedge,solidworks,caxa,时空,镇江if (typeof fileref!="undefined")
: b% ?- t$ T5 t5 g' B document.getElementsByTagName("head")[0].appendChild(fileref)
. t4 ^5 a- W# c6 D0 Q三维网技术论坛}
( p) W8 N6 X! Zloadjscssfile("myscript.js", "js") //打开页面时浏览器动态的加载文件
" G* H* R) _; V) O% |0 c h# w" q三维|cad|机械|汽车|技术|catia|pro/e|ug|inventor|solidedge|solidworks|caxaloadjscssfile("javascript.php", "js") // 打开页面时浏览器动态的加载"javascript.php" , 三维网技术论坛6 o* o9 s0 i) @ r5 H& }
loadjscssfile("mystyle.css", "css") //打开页面时浏览器动态的加载.css 文件
现在显示器很多种,宽度不同,比如说800*600或者1024*768等等。有些情况下,同一个网页在不同的显示器里显示的效果不同。
怎么样用JS在HTML文件装载的时候判断显示器的宽度,然后再根据显示器的宽度下载相应的CSS文件。
首先在< head>和< /head>中加入以下代码:
< script language=javascript>
< !--
function redirectPage(){
var url800x600=〃index-ie.html〃; //定义两个页面,此处假设index-ex.html和1024-ie.html同change-ie.html在同一个目录下
var url1024x768=〃1024-ie.html〃;
if ((screen.width==800) && (screen.height==600)) //在此处添加screen.width、screen.height的值可以检测更多的分辨率
window.location.href= url800x600;
else if ((screen.width==1024) && (screen.height==768))
window.location.href=url1024x768;
else window.location.href=url800x600;
}
然后再在< body…>内加入onLoad=〃redirectPage()〃
最后,同样地,在< body>和< /body>之间加入以下代码来显示网页的工作信息:
< script language=javascript>
< !--
var w=screen.width
var h=screen.height
document.write(〃系统已检测到您的分辨率为:〃);
document.write(〃< font size=3 color=red>〃);
document.write(w+〃×〃+h);
document.write(〃< /font>〃);
document.write(〃正在进入页面转换,请稍候…〃);
// -->
< /script>
=========================================================
建议方法,让窗口居中.在你做的网页最外面加个DIV,设置body属性,文本居中.或者把数值换成比例.
内容提要:
在CSS布局中,还常常用到IE Hack。if IE起着非常大的作用!
关键字:
Div CSS IE Hack
if IE什么意思呢?下面就列举了一些在CSS常见的HACK控制语句。
作为IE的IF条件注释使用备忘,大家可以参考,有经验欢迎与网友分享。
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->
<!--[if IE]> 所有的IE可识别 <![endif]-->
<!--[if IE 5.0]> 只有IE5.0可以识别 <![endif]-->
<!--[if IE 5]> 仅IE5.0与IE5.5可以识别 <![endif]-->
<!--[if gt IE 5.0]> IE5.0以及IE5.0以上版本都可以识别 <![endif]-->
<!--[if IE 6]> 仅IE6可识别 <![endif]-->
<!--[if lt IE 6]> IE6以及IE6以下版本可识别 <![endif]-->
<!--[if gte IE 6]> IE6以及IE6以上版本可识别 <![endif]-->
<!--[if IE 7]> 仅IE7可识别 <![endif]-->
<!--[if lt IE 7]> IE7以及IE7以下版本可识别 <![endif]-->
<!--[if gte IE 7]> IE7以及IE7以上版本可识别 <![endif]-->
判断IE6,加载不同的css代码
1. <!--[if lte IE 6]>
2. <STYLE type=text/css media=screen>@import url( ie6.css );
3. </STYLE>
4. <![endif]-->
5.
6. <!--[if gte IE 7]>
7. <style type="text/css" media="screen">
8. /* <![CDATA[ */ @import url(ie7.css); /* ]]> */
9. </style>
10. <![endif]-->
总结下css的一些hack
color:red; /* 所有浏览器都支持 */
color:red !important;/* 除IE6外 */
_color:red; /* IE6支持 */
*color:red; /* IE6、IE7支持 */
+color:red;/*IE7支持*/
*+color:red; /* IE7支持 */
color:red\9; /* IE6、IE7、IE8、IE9支持 */
color:red\0; /* IE8、IE9支持 */
color:red\9\0;/*IE9支持*/
/* webkit and opera */
@media all and (min-width: 0px){ div{color:red;} }
/* webkit */
@media screen and (-webkit-min-device-pixel-ratio:0){ div{color:red;} }
/* opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-
ratio:0) { div{color:red;} }
/* firefox * /
@-moz-document url-prefix(){ div{color:red;}} /* all firefox */
html>/**/body div, x:-moz-any-link, x:default {color:red;} /* newest firefox */
}
body:nth-of-type(1) p{color:red;} /* Chrome、Safari支持 */
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
<!–[if IE 7]> = 等于 IE7
<!–[if lt IE 8]> = 小于 IE8(就是 IE7 或以下了啦)
<!–[if gte IE 8]> = 大于或等于 IE8
<meta http-equiv="x-ua-compatible" content="ie=7" />
把这段代码放到<head>里面,在ie8里面的页面解析起来就跟ie7一模一样的了
展开阅读全文