最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端滾動錨點三個常用方案(點擊后頁面滾動到指定位置)

 更新時間:2025年01月10日 09:42:39   作者:Gaga??  
這篇文章主要給大家介紹了關(guān)于前端滾動錨點的三個常用方案,實現(xiàn)的效果就是點擊后頁面滾動到指定位置,三種方法分別是scrollIntoView、scrollTo和scrollBy,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

三個常用方案:

1.scrollintoView

把調(diào)用該方法的元素滾動到屏幕的指定位置,中間,底部,或者頂部

優(yōu)點:方便,只需要獲取元素然后調(diào)用

缺點:不好精確控制,只能讓元素指定滾動到中間,頂部,底部,沒法設(shè)置偏移

block可以設(shè)置為center,end,start等來控制在頁面顯示的位置。

注意的就是

<script setup>
import {onMounted, ref } from 'vue'
import {useRoute }from 'vue-router'
const { query }= useRoute();
const target = query.target
onMounted(()=>{
   document.getElementById(target)?.scrollIntoView({
   block:'center'
  })
})
</script>
 
<template>
  <button>美妝</button>
  <button>數(shù)碼</button>
  <button>美食</button>
  <button>家居</button>
  <button>黃金</button>
 <div class="home" id="home">
   // 五個樓層
   <div class="part" id="part1">美妝</div>
   <div class="part" id="part2">數(shù)碼</div>
   <div class="part" id="part3">美食</div>
   <div class="part" id="part4">家居</div>
   <div class="part" id="part5">黃金</div>
 </div>
</template>
 
<style>
.home {
  width: 1000px;
  margin: 0 auto;
  height: 90vh;
  overflow-y: scroll;
}
.part {
  margin-top: 20px;
  border: 1px solid red;
  height: 600px;
}
.needData {
  height: 600px;
}
</style>

2.scrollTo

把可滾動元素滾動到指定x,y坐標(biāo)優(yōu)點:可以精確控制到具體多少px

缺點:得先獲取到x,y坐標(biāo)

需要注意的是,這個方法的調(diào)用者并不是要看到的元素,而是可滾動的元素。像方法一,調(diào)用scrollintoView方法的是五個樓層本身,但是如果是調(diào)

scrollTo的話是調(diào)用他們的父級元素(id="home"的div盒子)它才是可滾動的元素.

<script setup>
import {onMounted, ref } from 'vue'
import {useRoute }from 'vue-router'
const { query }= useRoute();
const target = query.target
onMounted(()=>{
   const targetDom = document.getElementById(target);
   const targetRect = targetDom.getBoundingClientRect();
   document.getElementById(home)?.scrollTo(targetRect.x, targetRect.y - 100);
})
</script>

<template>
  <button>美妝</button>
  <button>數(shù)碼</button>
  <button>美食</button>
  <button>家居</button>
  <button>黃金</button>
 <div class="home" id="home">
   <div class="part" id="part1">美妝</div>
   <div class="part" id="part2">數(shù)碼</div>
   <div class="part" id="part3">美食</div>
   <div class="part" id="part4">家居</div>
   <div class="part" id="part5">黃金</div>
 </div>
</template>

3.scrollBy 

滾動指定的x,y距離。也就是讓他滾動一定距離,不指定坐標(biāo)

優(yōu)點:當(dāng)我們只是做移動,而不是滾到某個指定的元素時

缺點:得自己算出多少距離

這個方法用的少就不舉例了

最后:要注意的點,記住這么幾個操作

1,如果是打開就滾動到指定位置,vue/react項目一定要在你頁面數(shù)據(jù)渲染完成后滾動

2,如果不含有請求,記得在onMounted(vue),或者useEffect(react)中進行滾動操作,如果是頁面滾動,可能還要在加一個小延遲(setTimeout)

3,如果頁面的數(shù)據(jù)是請求來的,請確保數(shù)據(jù)渲染完成后進行滾動

頁面滾動是會記錄上一次滾動狀態(tài)的,在頁面渲染好后又會滾回去。記錄頁面滾動這個現(xiàn)象一般只限于同步引入的組件,異步的不會(沒有滾動記錄)

總結(jié) 

到此這篇關(guān)于前端滾動錨點三個常用方案的文章就介紹到這了,更多相關(guān)前端滾動到指定位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

阳泉市| 永和县| 大姚县| 临沂市| 从化市| 鄯善县| 北票市| 荔波县| 柳江县| 电白县| 巴南区| 镇原县| 香河县| 平陆县| 永和县| 隆德县| 尚志市| 牡丹江市| 庄浪县| 海伦市| 顺昌县| 平利县| 连城县| 永宁县| 古蔺县| 天等县| 义马市| 临泉县| 洛宁县| 曲沃县| 云阳县| 淮阳县| 正宁县| 嘉荫县| 伊川县| 阿克苏市| 鄂州市| 大荔县| 年辖:市辖区| 射阳县| 越西县|