亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue項目回到頂部的兩種超簡單實現(xiàn)方法

 更新時間:2023年10月14日 09:14:26   作者:喵喵醬仔__  
這篇文章主要給大家介紹了關(guān)于vue項目回到頂部的兩種超簡單實現(xiàn)方法,?頁面切換回到頂部是一個很常見的功能,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

vue 中實現(xiàn)回到頂部的兩種方式:

(1)錨點方式

通過點擊錨點回到指定位置:

<template>
	<div id="topAnchor" ref="faultTree" class="wrap">
	    <a id="TOPUp" href="#topAnchor" rel="external nofollow" >
	      <img style="width: 100%;height: 100%;" src="../../../../assets/top.png" alt="">
	    </a>
	    <!--其他業(yè)務(wù)邏輯代碼-->
	    ...
	</div>
</template>

樣式:

<style>
#TOPUp{
  position: fixed;
  right: 45px;
  bottom: 100px;
  width: 40px;
  height: 40px;
  z-index: 99999999;
  box-shadow: 0px 0px 4px 4px #ecefef;
  border-radius: 600px;
}
</style>

(2)scrollTop

通過點擊事件將scrollTop重置為0,從而達到返回頂部的效果。

<template>
  <div class="hello_world">
    <button class="top" @click="toTop">^</button>
  </div>
</template>
<script>
export default {
  methods: {
    toTop() {
      document.documentElement.scrollTop = 0;
    },
  },
};
</script>
<style>
.hello_world {
  height: 5000px;
}
.top {
    position: fixed;
    width: 30px;
    height: 30px;
    bottom: 50px;
    right: 100px;
    background-color: aqua;
  }
</style>

代碼地址:https://gitcode.net/sinat_33255495/vue

附:vue實現(xiàn)刷新頁面,頁面回到頂部

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

  • vue-router中有一個滾動行為-scrollBehavior ,
const router = createRouter({
  history: createWebHashHistory(),
  routes: [...],
  scrollBehavior (to, from, savedPosition) {
    // return 期望滾動到哪個的位置
    // vue2.0  x  y  控制
    // vue3.0  left  top 控制
	return { left: 0, top: 0 }  }
})
  • 加全局守衛(wèi)

在main.js中加

router.afterEach((to,from,next)=>{
    window.scrollTo(0,0);
})

總結(jié)

到此這篇關(guān)于vue項目回到頂部的兩種超簡單實現(xiàn)方法的文章就介紹到這了,更多相關(guān)vue項目回到頂部內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論