我目前正在开发的网站正在使用自定义 CMS,并且使用拆分 CSS 文件进行了移动集成,所以我有: - desktop.css - mobile.css
有一个 Javascript 可以检测屏幕大小并提供适当的 CSS 文件。
用户可以选择在较小的屏幕上查看整个站点,并在较大的屏幕上查看较小的站点,在 PHP 中设置的 cookie 如下:
$mob = '?setmob';
$mob_len = strlen($mob);
$self_end = substr($self, strlen($self) - $mob_len);
if($self_end == $mob){
$settomobile = 1;
}
else{
$full = '?setfull';
$full_len = strlen($full);
$full_end = substr($self, strlen($self) - $full_len);
if($full_end == $full){
$settofull = 1;
}
}
if($settomobile == 1)
{
setcookie("viewing", "", time()-3600);
setcookie("viewing", "mobile", time()+63113851);
}
if($settofull == 1)
{
setcookie("viewing", "", time()-3600);
setcookie("viewing", "desktop", time()+63113851);
}
检测屏幕尺寸并提供 CSS 的 Javascript 是:
<script type=\"text/javascript\">
function adjustStyle(width) {
width = parseInt(width);
if (width < 750) {
$(\"#size-stylesheet\").attr(\"href\", \"/mobile.css\");";
$mob = "Yes";
$top_contents .= "} else {
$(\"#size-stylesheet\").attr(\"href\", \"/$css_name.css\");
}
}
function setToMobile() {
var width = 200;
if (width < 750) {
$(\"#size-stylesheet\").attr(\"href\", \"/mobile.css\");";
$mob = "Yes";
$top_contents .= "} else {
$(\"#size-stylesheet\").attr(\"href\", \"/$css_name.css\");
}
}
function setToDesktop() {
var width = 1000;
if (width < 750) {
$(\"#size-stylesheet\").attr(\"href\", \"/mobile.css\");";
$mob = "Yes";
$top_contents .= "} else {
$(\"#size-stylesheet\").attr(\"href\", \"/$css_name.css\");
}
}
$(function() {";
if(isset($_COOKIE['viewing']) || $settofull || $settomobile){
$view = $_COOKIE['viewing'];
if(($view == 'mobile' || $settomobile == 1) && !$settofull){
$top_contents .= "setToMobile();";
}
elseif(($view == 'desktop' || $settofull == 1) && !$settomobile){
$top_contents .= "setToDesktop();";
}
}
else{
$top_contents .= " adjustStyle(screen.width);
";}
$top_contents .="
}); </script>";
}
$top_contents = $top_contents . "<link rel=\"stylesheet\" type=\"text/css\" href=\"/admin/css/$css_name.css\" id=\"size-stylesheet\" title=\"default\" />
我发现的问题是网站必须先启动普通 CSS,然后才能启动 Javascript 以将用户重定向到移动 CSS。
有没有一种简单的方法可以解决这个问题,这样用户就不必在加载移动 CSS 之前查看整个网站?
提前谢谢各位!