pc和mobile切换方式

最近看了游戏站点https://yandex.com/games/zh/category/simulator,在他们的游戏站发现一个很有趣的现象:就是他们在pc端进行窗口变化时,界面也会跟着响应式变化。但是这时候切换到移动端,仍然是pc端的样式。如果这时候刷新页面,就会以一个新的样式展示。在不同端分别查看html的网络请求,发现预览的结果与真实页面一样,dom结构基本已经处理好了。

1、pc端和移动端切换的主要方式

  • 1、通过css媒体查询和弹性布局(弹性盒子布局、网格布局)
  • 2、常规使用rem、vh、vm、百分比等处理
  • 3、基于URL参数或者路径进行切换
  • 4、网页加载时检查用户的User-Agent字符串,然后加载不同样式、资源和其他js。或者直接通过nginx处理

上述站点大概是采用第4种和第1种方式结合处理,本文主要使用第四种方式演示。

2、基于nginx+User-Agent进行切换

使用Nginx的ngx_http_map_module模块来根据用户的设备类型进行重定向或代理到不同的站点

  • a、新建一个D:\nginx\config\nginx.conf文件
ini 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # 定义一个映射,根据User-Agent判断设备类型(同时忽略大小写匹配)
    map $http_user_agent $device_type {
        default         pc;
        ~*mobile|android|iphone        mobile;
        ~*ipad          tablet;
    }


server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html/pc;
            if ($device_type = mobile){
                 root   /usr/share/nginx/html/mobile;
            }
            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }
}
  • b、新建一个D:\nginx\html\mobile\index.html文件
xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端</title>
</head>
<body>
    移动端
</body>
</html>
  • c、新建一个D:\nginx\html\pc\index.html文件
xml 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电脑端</title>
</head>

<body>
    电脑端
</body>

</html>
  • d、执行docker指令去运行nginx测试
docker 复制代码
docker run --name some-nginx -p 8801:80 -v D:/nginx/html:/usr/share/nginx/html:ro -v D:/nginx/config/nginx.conf:/etc/nginx/nginx.conf -d nginx

3、参考方式

相关推荐
这是个栗子几秒前
前端开发中的常用工具函数(二)(持续更新中...)
开发语言·前端·javascript
苦藤新鸡4 分钟前
38.交换二叉树中所有的左右节点
开发语言·前端·javascript
2501_9445215928 分钟前
Flutter for OpenHarmony 微动漫App实战:主题配置实现
android·开发语言·前端·javascript·flutter·ecmascript
2501_944521591 小时前
Flutter for OpenHarmony 微动漫App实战:动漫卡片组件实现
android·开发语言·javascript·flutter·ecmascript
董世昌411 小时前
null和undefined的区别是什么?
java·前端·javascript
软弹1 小时前
Vue2 的数据响应式原理&&给实例新增响应式属性
前端·javascript·vue.js
晚霞的不甘1 小时前
Flutter 布局核心:构建交互式文档应用
开发语言·javascript·flutter·elasticsearch·正则表达式
倒流时光三十年2 小时前
阿里云 CentOS 7 使用 docker 安装 Nginx
nginx·阿里云·docker·centos
weixin_638303112 小时前
uniapp组合式和选项式
javascript·vue.js·uni-app
小妖6663 小时前
javascript 舍去小数(截断小数)取整方法
开发语言·前端·javascript