Vant简单H5 web app【小试牛刀】

简介: Vant简单H5 web app【小试牛刀】

index.html

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!--谷歌浏览器(手机端)顶部颜色-->
    <meta name="msapplication-TileColor" content="#4183fd">
    <meta name="theme-color" content="#4183fd">
    <!-- 如果不设置maximum-scale=1.0, user-scalable=0就会导致底部tabbar偶发性不响应transition缓动效果 -->
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <!-- 浏览器顶部标题栏图标 -->
    <link rel="shortcut icon" type="image/x-icon" href="./static/favicon.ico">
</head>
<body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
</body>
</html>

src/js/main.js

 //【基础配置】----------------------------------------------------------------
 //引入Vue框架(设置为false以阻止vue在启动时生成生产提示)
 import Vue from 'vue';
 Vue.config.productionTip = false;
 //导入路由【安装方法】cnpm i vue-router
 import VueRouter from 'vue-router';
 Vue.use(VueRouter);
 import routes from './routes';
 const router = new VueRouter({
     scrollBehavior: (to, from, savedPosition) => {
         return { x: 0, y: to.meta.scrollTop || 0 }; //进入该页面时,用之前保存的滚动位置赋值 
     },
     routes
 });
 router.isDirectAccess = false; //是否直接访问(通常是通过超链接访问)
 router.goBack = () => {
     if (router.isDirectAccess) {
         router.push('/'); //如果直接访问,返回都直接跳转到根目录
     } else {
         router.isToRight = true; //页面从左往右滑动
         history.back(); //后退+刷新(为了某些页面重新登录的时候自动回到最顶端)
         //  history.go(-1); //后退
     }
 };
 router.beforeEach((to, from, next) => {
     router.isDirectAccess = from.name === null || from.path === to.path || from.path === "/"; //是否直接访问(譬如通过网址直接访问)
     from && (from.meta.scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop); //进入该页面时,记录上一个路由页面的滚动位置
     router.title = document.title = to.meta.title || ''; //路由发生变化修改页面title
     //判断跳转到一级页面
     if (to && to.meta && to.meta.tabbarIndex !== null && to.meta.tabbarIndex !== undefined) {
         if (from && from.meta && from.meta.tabbarIndex !== null && from.meta.tabbarIndex !== undefined) { //在一级页面之间切换
             router.isFade = true; //淡入淡出
             //router.isToRight = to.meta.tabbarIndex < from.meta.tabbarIndex;//判断在一级页面的时候,点击底部菜单左右移动方向,这里的tabbarIndex是在meta里定义的索引,用于判断菜单顺序
         } else { //从内页 → 一级页面
             router.isToRight = true;
         }
     }
     if (router.isFade) {
         router.transitionName = 'fade'
     } else {
         router.transitionName = router.isToRight ?
             "slide-right" :
             "slide-left"; //朝右滑动→、←朝左滑动
     }
     router.isFade = false; //重置淡入淡出
     router.isToRight = false; //重置朝左的方向
     next();
 });
 //【第三方插件】----------------------------------------------------------------
 //引入es6-promise【安装方法】cnpm i es6-promise --save-dev
 import promise from 'es6-promise'; //使用axios对安卓或者ios低版本兼容性处理
 promise.polyfill(); //注意需要在aixo之前注册
 //引入jquery【安装方法】cnpm i jquery --save-dev
 import $ from 'jquery';
 Vue.prototype.$ = $;
 //引入Axios【安装方法】cnpm i axios -S
 //建议暂时不要大面积使用Axios,我严重怀疑手机端它的兼容性问题!!!
 /*  import axios from 'axios';
  Vue.prototype.$axios = axios; */
 //引入Vant【安装方法】cnpm i vant -S
 import Vant from 'vant';
 import 'vant/lib/index.css';
 import 'vant/lib/icon/local.css'; //解决离线无网络环境中使用icon不显示的问题
 Vue.use(Vant);
 //  引入Vant 图片懒加载模块
 import { Lazyload } from 'vant';
 Vue.use(Lazyload);
 //引入Echarts【安装方法】cnpm i echarts -S
 import echarts from 'echarts';
 Vue.prototype.$echarts = echarts;
 //【公共方法】----------------------------------------------------------------
 import sg from "./sg";
 Vue.prototype.$g = sg;
 import sd from './sd';
 Vue.prototype.$d = sd;
 //【公共变量】----------------------------------------------------------------
 import global from "./global";
 Vue.prototype.$global = global;
 //【初始化加载】----------------------------------------------------------------
 import App from '../vue/App';
 new Vue({ el: '#app', render: h => h(App), router });

src/vue/App.vue

<template>
  <div id="sg-app" :class="tabbars.includes($route.path)?'sg-home':'sg-inner'">
    <!-- 内页 -->
    <transition :name="$router.transitionName">
      <router-view class="sg-router"></router-view>
    </transition>
    <van-tabbar v-model="active" active-color="#4183fd" inactive-color="#666">
      <van-tabbar-item icon="wap-home-o" to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="medal-o" to="/by">毕业</van-tabbar-item>
      <van-tabbar-item icon="records" to="/jy">就业</van-tabbar-item>
      <van-tabbar-item icon="contact" to="/wd" info="8">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      active: 0,
      tabbars: []
    };
  },
  watch: {
    $route(to, from) {
      this.active = this.tabbars.indexOf(this.$route.path); //根据路由判断高亮显示的底部菜单
    }
  },
  created() {
    // 从全局路由配置里面提出底部一级菜单的路由
    var arr = this.$router.options.routes;
    for (var i = 0, len = arr.length; i < len; i++) {
      var a = arr[i];
      a.meta &&
        a.meta.index !== null &&
        a.meta.index !== undefined &&
        this.tabbars.push(a.path);
    }
  }
};
</script>
<style lang='scss' scoped>
@import "~@/css/common";
@mixin transformXY($x: 0, $y: 0) {
  -webkit-transform: translate($x, $y);
  transform: translate($x, $y);
}
#sg-app,
.sg-router {
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.sg-router,
.van-tabbar {
  transition: 0.2s ease-out;
  -moz-transition: 0.2s ease-out;
  -webkit-transition: 0.2s ease-out;
  -o-transition: 0.2s ease-out;
  -khtml-transition: 0.2s ease-out;
}
.van-tabbar:after {
  border: none;
  box-shadow: 0 -20px 40px rgba(0, 0, 0, 0.05);
}
//在首页(底部有tabBar)的时候
.sg-router {
  background: #f2f2f2; //内页背景色浅灰
}
.sg-home {
  .sg-router {
    background: white; //首页背景色白色
  }
  .van-tabbar {
    @include transformXY(); //显示底部菜单
  }
  // 在首页的时候切换效果变成 淡入淡出 透明度
  .fade {
    &-enter,
    &-leave-active {
      opacity: 0;
    }
  }
}
//在内页的时候
.sg-inner {
  .van-tabbar {
    @include transformXY(0, 100%); //隐藏底部菜单
  }
}
.slide-left {
  &-enter {
    @include transformXY(100%, 0); //←朝左移入时的起始位置
  }
  &-leave-active {
    @include transformXY(-30%, 0); //←朝左移出时的结束位置
  }
}
.slide-right {
  &-enter {
    @include transformXY(-100%, 0); //朝右→移入时的起始位置
  }
  &-leave-active {
    @include transformXY(30%, 0); //朝右→移出时的结束位置
  }
}
// 自定义全局控件样式----------------------------------------------------------------
// 顶部navbar蓝色背景+白色文字
>>> .van-nav-bar {
  background-color: #4586fe;
  .van-icon,
  .van-nav-bar__title {
    color: white;
  }
}
// 标签默认浅灰色背景
>>> .van-tag--default {
  background: #f4f5f7;
  color: #999;
}
// 定义卡片的按下样式
>>> .van-card {
  @extend %transition;
  &:active {
    background: #e8ecf2 !important;
  }
}
// 定义swipe下面的轮播点点
>>> .van-swipe__indicators {
  .van-swipe__indicator {
    background: #b2b2b2;
    &--active {
      background: #7f7f7f;
      width: 12px;
      border-radius: 6px;
    }
  }
}
>>> [class*="van-hairline"]::after {
  border: none;
}
</style>

src/js/routes.js

export default [{
        path: "/",
        redirect: "/home",
    },
    {
        path: "/home",
        meta: { title: '空中招聘', index: 0 },
        component: () =>
            import ('../vue/page/home'),
    },
    {
        path: "/xxtz",
        meta: { title: '消息通知' },
        component: () =>
            import ('../vue/page/home/xxtz'),
    },
    {
        path: "/sp",
        meta: { title: '视频面试' },
        component: () =>
            import ('../vue/page/home/xxtz/sp'),
    },
    {
        path: "/detail",
        meta: { title: '消息' },
        component: () =>
            import ('../vue/page/home/xxtz/detail'),
    },
    {
        path: "/tzgg",
        meta: { title: '通知公告' },
        component: () =>
            import ('../vue/page/home/tzgg'),
    },
    {
        path: "/xjh",
        meta: { title: '宣讲会' },
        component: () =>
            import ('../vue/page/home/xjh'),
    },
    {
        path: "/by",
        meta: { title: '毕业', index: 1 },
        component: () =>
            import ('../vue/page/by'),
    },
    {
        path: "/jy",
        meta: { title: '就业', index: 2 },
        component: () =>
            import ('../vue/page/jy'),
    },
    {
        path: "/wd",
        meta: { title: '我的', index: 3 },
        component: () =>
            import ('../vue/page/wd'),
    },
    {
        path: "/search/*",
        meta: { title: '搜索结果' },
        component: () =>
            import ('../vue/page/search'),
    }, {
        path: "*",
        meta: { title: '没有找到您想要的页面' },
        component: () =>
            import ('../vue/page/notFound')
    } //404页面,一定要写在最后
];

src/vue/page/home.vue、src/vue/page/home/tzgg.vue和src/vue/page/home/xjh.vue

<template>
  <div :class="$route.path.replace(/\//g, '')">
    <van-sticky>
      <van-nav-bar
        :title="$router.title"
        left-text
        right-text
        left-arrow
        @click-left="$router.goBack"
      />
    </van-sticky>
    <br />
    <br />
    <br />
    <br />
    <h1>{{$router.title}}</h1>
    <van-button type="warning" to="/home">回首页</van-button>
    <van-button type="primary" to="/tzgg">通知公告</van-button>
    <van-button type="info" to="/xjh">宣讲会</van-button>
  </div>
</template>


相关文章
|
4月前
|
存储 移动开发 JSON
H5学习之路之Web存储解决方案
H5学习之路之Web存储解决方案
19 0
|
5月前
|
Web App开发 移动开发 小程序
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,显示“网络不给力,请稍后再试”,预发内网版本不能使用,线上版本可以正常使用,这个是什么原因啊,是某些参数没有配置吗,还是说是一些参数改错了?
63 2
|
5月前
|
移动开发
钉钉H5微应用配置IP,应用首页地址报错:app url exceeds max length limit,这个怎么处理?
钉钉H5微应用配置IP,应用首页地址报错:app url exceeds max length limit,这个怎么处理?
307 0
|
4月前
|
移动开发 小程序 JavaScript
Uniapp 中,能够同时兼容H5、web、app、微信小程序的引入高德地图的语法格式
Uniapp 中,能够同时兼容H5、web、app、微信小程序的引入高德地图的语法格式
|
19小时前
|
缓存 移动开发 前端开发
【专栏:HTML与CSS前端技术趋势篇】HTML与CSS在PWA(Progressive Web Apps)中的应用
【4月更文挑战第30天】PWA(Progressive Web Apps)结合现代Web技术,提供接近原生应用的体验。HTML在PWA中构建页面结构和内容,响应式设计、语义化标签、Manifest文件和离线页面的创建都离不开HTML。CSS则用于定制主题样式、实现动画效果、响应式布局和管理字体图标。两者协同工作,保证PWA在不同设备和网络环境下的快速、可靠和一致性体验。随着前端技术进步,HTML与CSS在PWA中的应用将更广泛。
|
23天前
|
移动开发 小程序 前端开发
使用uni-app开发(h5、小程序、app)步骤
使用uni-app开发(h5、小程序、app)步骤
|
2月前
|
移动开发 负载均衡 小程序
代驾app开发丨代驾系统开发玩法详情丨代驾系统开发网页版/H5/小程序及源码部署
**司机/代驾员端**:司机可以通过APP接收订单,查看订单详情、路线和导航,提供现场服务并进行确认。
|
4月前
|
移动开发 小程序 UED
科普:App、H5和小程序是什么?
科普:App、H5和小程序是什么?
45 0
|
4月前
|
移动开发 前端开发 开发工具
智慧校园信息化H5端App的软件开发设计文档
智慧校园信息化H5端App的软件开发设计文档
52 0
|
Web App开发 JavaScript iOS开发
何为Web App,何为Hybird App
这些概念听起来很火,当下也很流行,真正理解起来却并非易事。如果让我来全面的解释Web App和Hybird App,我觉得还有些困难。 这篇文章只是我深入了解移动领域开发过程中的不断整理和总结,其中涉及到很多概念,观点,个人的看法,有不确切的地方,欢迎指正。
1507 0