html代码,通过识别pc端和移动端,跳转到不同的页面

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

    <title></title>

  <!--引入需要的插件放置自己的样式等-->

</head>
<body>
//主体部分,写入自己需要的内容

</body>

//重点看下面哦。。。。。。。。。。。。。。。。

<script language="JavaScript">

//写一个判断函数

    function ScreenWidth() {      //获取屏幕尺寸,判断PC端或移动端

      if (/phone|pad|pod|iPhone|iPod|ios|iPad|Android|Fennec|BlackBerry|Mobile|IEMobile|MQQBrowser|JUC|Fennec|WosBrowser|BrowserNG|WebOS|Symbian|Windows Phone/i.test(navigator.userAgent)) {

//如果是移动端,则跳转到移动端对应的页面;否则,跳转到PC端对应的页面

            window.location.href = './mobile/html/index.html';
        } else {
            window.location.href = './pc/static/template/index.html';
        }
    }
 //第一种方式,通过点击屏幕的方式,调用函数,进行页面跳转
    document.body.onclick = function() {
        ScreenWidth()
    }

 //第二种方式,等待页面加载完成,调用函数,进行自动跳转(但是在网速较慢的情况下,可能动画或者内容还没加载完成,就自动跳转咯)

 window.οnlοad=function(){
        ScreenWidth()
    }


</script>
</html>
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Nuxt.js 中,我们可以使用插件判断是 PC 端还是移动端,然后根据不同设备跳转不同的路由。 下面的代码演示了如何编写一个插件来实现这个功能: ```javascript // plugins/device.js export default ({ app }, inject) => { const userAgent = process.server ? app.$ua.get('User-Agent') : navigator.userAgent const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( userAgent ) inject('device', { isMobile, redirect(path) { const url = isMobile ? `/m${path}` : path if (process.client) { window.location.href = url } else { app.context.redirect(url) } } }) } ``` 在插件中,我们首先获取了用户代理(userAgent),然后使用正则表达式来判断是否是移动设备。如果是移动设备,我们就将路径前缀设置为 "/m",否则就保持原样。 接着,我们定义了一个名为 "redirect" 的方法,用于根据设备类型跳转路由。如果在客户端中调用该方法,我们就直接跳转到指定的 URL;如果在服务器端中调用该方法,我们就使用上下文对象(context)的 redirect 方法来跳转路由。 最后,我们将插件作为 Nuxt.js 的插件进行注册: ```javascript // nuxt.config.js export default { plugins: ['~/plugins/device.js'] } ``` 现在,我们就可以在页面中使用该插件了。例如,我们可以编写一个页面组件来测试: ```html <!-- pages/index.vue --> <template> <div> <h1>首页</h1> <button @click="goToAbout">跳转到关于页</button> </div> </template> <script> export default { methods: { goToAbout() { this.$device.redirect('/about') } } } </script> ``` 在上面的代码中,我们定义了一个按钮,当用户点击该按钮时,就会调用 $device.redirect 方法来跳转到 "/about" 路由。如果是在移动设备上访问,就会跳转到 "/m/about" 路由。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值