整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          vue項目頁面切換到默認顯示頂部

          vue項目頁面切換到默認顯示頂部

          頁面切換,默認顯示頂部是一個在移動端很常見的功能。整理了三個現實方法如下:

          方法一:在路由配置中使用scrollBehavior

          使用前端路由,當切換到新路由時,想要頁面滾到頂部,或者是保持原先的滾動位置,就像重新加載頁面那樣。

          vue-router 能做到,而且更好,它讓你可以自定義路由切換時頁面如何滾動。

          在路由配置中使用scrollBehavior

           scrollBehavior (to, from, savedPosition) { if (savedPosition) { return savedPosition
           } else { return { x: 0, y: 0 }
           }
           }
          

          如下例子, 使得每次進入頁面都在頁面頂部。

          import Vue from 'vue'import Router from 'vue-router'import {wechatAuth,wechatOauth} from '@/utils/auth.js'import store from '@/store'Vue.use(Router)
          const routes={
           mode: 'hash',
           routes: [
           { path: '/', redirect: '/introduce' },
           {
           path: '/introduce',
           name:'introduce',
           component: ()=> import('@/pages/introduce')
           },
           {
           path: '/register',
           component: ()=> import('@/pages/register')
           }, /* {
           path: '/auth',
           component: ()=> import('@/pages/auth')
           }, */
           {
           path: '/businessCard',
           component: ()=> import('@/pages/businessCard')
           },
           {
           path: '/category',
           component: ()=> import('@/pages/category')
           },
           {
           path: '/index',
           name:'index',
           component: ()=> import('@/pages/index')
           },
           {
           path: '/orderList',
           name:'orderList',
           component: ()=> import('@/pages/orderList')
           },
           {
           path: '/orderDetail',
           name:'orderDetail',
           component: ()=> import('@/pages/orderDetail')
           },
           {
           path: '/acceptOrder',
           name:'acceptOrder',
           component: ()=> import('@/pages/acceptOrder')
           },
           {
           path: '/feedBackDetail',
           name:'feedBackDetail',
           component: ()=> import('@/pages/feedBackDetail')
           },
           ], scrollBehavior (to, from, savedPosition) { if (savedPosition) {
           return savedPosition
           } else {
           return { x: 0, y: 0 }
           }
           }}
          const route=new Router(
           routes
          )
          route.beforeEach((to, from, next)=> { //獲取當前頁面鏈接進入路由簽名
           let link=route.resolve({
           path: window.location.href
           }) //判斷有沒有code
           if(link.href.includes('/?code')){
           let {code}=link.route.query
           let uri=to.fullPath
           wechatOauth(code,uri)
           }else{ //沒有code再進入判斷有沒有用戶資料
           if(store.state.userInfo.userId===null){ //用戶數據初始化請求
           store.dispatch('getUserData').then(res=> { //這里是已授權
           //已授權進入正常的判斷跳轉流程
           //獲取用戶id及綁定手機號碼
           let {userId,regTel}=res if(userId==-1){ //判斷用戶是否關注了公眾號
           next('/introduce')
           }else{ //判斷有沒有綁定手機號碼
           if(regTel==''){
           next('/register')
           }else{ //這里是請求到用戶資料進入的正常流程 next()
           }
           }
           }).catch(err=> { //這里是未授權
           //未授權就請求微信接口進行授權 wechatAuth(to.fullPath)
           })
           }else{ //這里是沒有問題進入的正常流程 next()
           }
           }
          })
          export default route
          

          方法二:vue里面寫法如下,至于updated生命周期里面

           updated() {
           window.scroll(0, 0);
           }
          

          方法三:router攔截控制

          現按鈕點擊快速回到頁面頂部,有多種方法。

          在這里我介紹一下jQuery的方法。

          jQuery核心代碼:

          $(".top").click(function() {

          $("body,html").animate({

          scrollTop: 0

          }

          回頂部:

          1. <div id="top"></div><a herf="#top">top</a>

          2. <button class="gotop">top</button>$(".gotop").click(function(){$(document).scrollTop(0);})

          3. <div id="top"></div><button class="gotop">top</button>

            var top=document.getElementById('top');

          var btn=document.getElementsByClassName('btn')[0];

          btn.onclick=function(){

          top.scrollIntoView(0)

          }

          4.<button class="gotop">top</button>

          var btn=document.getElementsByClassName('btn')[0];

          btn.onclick=function(){

          document.body.scrollTop=document.documentElement.scrollTop=0;

          }

          返回上一頁的html寫法:

          1. <a href="#" onClick="javascript :history.back(-1);">返回上一頁</a>

          2. <a href="#" onClick="javascript :history.go(-1);">返回上一頁</a>


          主站蜘蛛池模板: 鲁丝丝国产一区二区| 无码少妇丰满熟妇一区二区 | 亚洲综合一区二区精品导航| 毛片一区二区三区| 国产成人精品一区二区三区免费 | 精品视频在线观看你懂的一区| 精品一区二区三区自拍图片区| 国产乱码精品一区三上| 国精产品一区一区三区MBA下载 | 日本精品一区二区三本中文| 一区二区在线免费观看| 色欲AV无码一区二区三区| 色精品一区二区三区| 一区二区三区在线播放视频| 国产伦精品一区二区三区视频金莲| 美女AV一区二区三区| 国产一区二区三区高清视频| 中文字幕亚洲一区| 日韩一区二区视频在线观看| 免费一本色道久久一区| 三上悠亚国产精品一区| 日韩精品无码一区二区三区不卡| 无码一区二区三区免费视频| 三级韩国一区久久二区综合| 91久久精一区二区三区大全| 丝袜美腿高跟呻吟高潮一区| 日韩一区在线视频| 久久精品视频一区二区三区| 精品国产一区二区三区www| 国产成人精品无码一区二区三区| 亚洲综合无码一区二区三区| 无码囯产精品一区二区免费 | 一区二区视频在线播放| 亚洲Av永久无码精品一区二区| 国产精品一区二区无线| 在线播放偷拍一区精品| 亚洲av鲁丝一区二区三区| 中文字幕一区二区免费| 一区二区三区四区国产| 亚州国产AV一区二区三区伊在| 亚洲av无码一区二区三区人妖 |