[织梦CMS教程] 根据判断PC浏览器类型和手机屏幕像素自动调用不同CSS的代码

[复制链接]
查看994 | 回复9 | 2020-5-2 23:23:31 | 显示全部楼层 |阅读模式
  1. css媒体查询

     

  2.  

    php、asp、js判断客户端输出对应的样式

 

------------------- 1.媒体查询方法在 css 里面这样写 --------------------

@media screen and (min-width: 320px) and (max-width: 480px){
在这里写小屏幕设备的样式
}

 

@media only screen and (min-width: 321px) and (max-width: 1024px){
这里写宽度大于321px小于1024px的样式(一般是平板电脑)
}

 

@media only screen and (min-width: 1029px){
这里写pc客户端的样式
}

 

------------------- 2.用js根据客户端输出对应样式 --------------------

/*事实上用asp、php后台判断更保险,js在前端,有可能被用户禁止*/

function loadCSS() {
 if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|wOSBrowser|BrowserNG|WebOS)/i))) {
        document.write('<link href="css/pad-phone.css" rel="stylesheet" type="text/css" media="screen" />');
    }
    else {
        document.write('<link href="css/pc.css" rel="stylesheet" type="text/css" media="screen" />');
    }
}
loadCSS();

 

 

 

------------------- 3.既判断分辨率,也判断浏览器-------------------

应E.Qiang提议,重新完善代码,使之成为判断浏览器类型屏幕分辨率自动调用不同CSS的代码。
代码如下:
<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
var IE1024="";
var IE800="";
var IE1152="";
var IEother="";
ScreenWidth(IE1024,IE800,IE1152,IEother)
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
//如果浏览器为Firefox
var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";
ScreenWidth(Firefox1024,Firefox800,Firefox1152,Firefoxother)
}else{
//如果浏览器为其他
var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";
ScreenWidth(Other1024,Other800,Other1152,Otherother)
}
}
function ScreenWidth(CSS1,CSS2,CSS3,CSS4){
if ((screen.width == 1024) && (screen.height == 768)){
setActiveStyleSheet(CSS1);
}else{
if ((screen.width == 800) && (screen.height == 600)){
setActiveStyleSheet(CSS2);
}else{
if ((screen.width == 1152) && (screen.height == 864)){
setActiveStyleSheet(CSS3);
}else{
setActiveStyleSheet(CSS4);
}}}
}
function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
//-->
</SCRIPT>

解释:
var IE1024="";
var IE800="";
var IE1152="";
var IEother="";
引号里面分别填写,用户使用IE的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.
var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";
引号里面分别填写,用户使用FF的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.
var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";
引号里面分别填写,用户使用其他浏览器的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.

例子:

不判断分辨率,只判断浏览器

实现根据浏览器类型自动调用不同CSS。

<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{    
//如果浏览器为IE
setActiveStyleSheet("default.css");
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
//如果浏览器为Firefox
setActiveStyleSheet("default2.css");
}else{
//如果浏览器为其他
setActiveStyleSheet("newsky.css");
}
}
function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
//-->
</SCRIPT>

解释:
如果浏览器为IE,则调用default.css
如果浏览器为Firefox,则调用default2.css
如果浏览器为其他,则调用newsky.css

用法:
放在
</head>
前面即可。


只要求判断根据屏幕宽度选择不同的CSS样式表。

<script language=javascript>
<!--
if (screen.width == 800)
{
document.write('<link rel=stylesheet type="text/css" href="css800.css">')
}
else {document.write('<link rel=stylesheet type="text/css" href="css1024.css">')}
//-->
</script>

从0基础开始学习织梦dedecms建站,分享全套的织梦dedecms网站搭建教程、织梦安装使用教程。 汇聚了最全、最新的织梦cms建站教程。包含dedecms建站、仿站教程;dedecms系统使用常见问题;主题插件推荐下载等!欢迎学习!
回复

使用道具 举报

ffycxyw2274436 | 2021-5-29 08:50:45 | 显示全部楼层
找了好多地方,终于找到了
回复

使用道具 举报

乐态思 | 2021-12-17 13:39:14 | 显示全部楼层
厉害了悟空源码,这么多资源
回复

使用道具 举报

玉米人 | 2023-4-19 18:26:31 | 显示全部楼层
感谢悟空源码分享精品资源!
回复

使用道具 举报

勇敢的树袋熊1 | 2023-7-15 06:28:21 | 显示全部楼层
6666悟空源码资源多!
回复

使用道具 举报

极忘投 | 2023-8-4 20:41:08 | 显示全部楼层
谢谢楼主分享的资源,爱了~~~
回复

使用道具 举报

白云追月素 | 2023-8-26 17:13:55 | 显示全部楼层
我是来白嫖资源的!
回复

使用道具 举报

这个资源都有真不错
回复

使用道具 举报

lc泠辰 | 2024-6-18 15:08:11 | 显示全部楼层
加油!悟空源码,继续努力!支持你!
回复

使用道具 举报

没积分哈,谁来帮帮我啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则