我目前正在使用的网站使用的是自定义CMS,并且已经使用拆分CSS文件进行移动集成,所以我有: - desktop.css - mobile.css手机和桌面CSS文件,饼干和两者之间的切换
有检测屏幕尺寸和提供相应的CSS文件中的JavaScript。
用户可以查看较小的屏幕上完整的网站,并查看较小的网站在大屏幕上,在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);
}
检测屏幕尺寸的JavaScript服务于CSS是:
<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加载之前看到完整的网站?
在此先感谢你们!
您可以使用移动查询。 – Raptor 2013-04-29 10:53:00
你是什么意思的移动查询? – steveyb 2013-04-29 12:35:27
应该是**媒体查询**。看到我的答案 – Raptor 2013-04-30 02:28:15