欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3手動設置滾動條位置自動定位功能

 更新時間:2024年12月12日 12:15:46   作者:m0_74824592  
這篇文章介紹了vue3手動設置滾動條位置自動定位功能,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧

從B頁面進行xx操作后需要跳轉到A頁面,并定位到AA職位,上圖為A頁面。

A頁面的左側是div,內層包裹List組件

給div定義ref=leftRef,在代碼中寫如下:

? function scrollTop() {
? ? if (leftRef.value) {
? ? ? console.log('99', leftRef.value);
? ? ? nextTick(() => {
? ? ? ? leftRef.value.scrollTop = 1000;
? ? ? ? // scrollBy(0, document.body.scrollWidth);
? ? ? });
? ? }
? }
onMounted(async () => {
? ? if (router.currentRoute.value.query.id) {
? ? ? positionChooseCode.value = router.currentRoute.value.query.id;
? ? ? positionStatusValue.value = router.currentRoute.value.query.id;
? ? }
? ? const positionId = router.currentRoute.value.query.positionId;
? ? if (!!positionId) {
? ? ? cStore.setPositionId(positionId);
? ? }
? ? console.log('mounted--positionId', positionId);
? ? await getPositionDictionary(positionChooseCode.value, '');
? ? await getDictionaries();
? ? scrollTop();
? });

第一,需要等待數(shù)據(jù)渲染完成后,再調用scrollTop,設置scrollTop=1000,這樣頁面初始化滾動條位置會改變。

第二,找到當前職位的高度,也要等職位列表數(shù)據(jù)渲染完成后,獲取

? ? console.log('positionList.value', positionList.value);
? ? rowItemId.value = item.id;
? ? //找到前面有多少個元素
? ? let index = positionList.value.findIndex((it) => it.id === rowItemId.value);
? ? console.log('找到前面有多少個元素', index + 1);
? ? num.value = index - 2;
獲取當前職位,當前職位會有class==red的,通過class獲取ele;
?const sortableEles = document.querySelectorAll('.red');
? ? ? console.log(sortableEles);
? ? ? let itemHeight = 0;
? ? ? if (sortableEles.length > 0) {
? ? ? ? const firstListItem = sortableEles[0];
? ? ? ? itemHeight = firstListItem.offsetHeight; // 獲取元素的高度,包括內邊距和邊框
? ? ? ? console.log('第一個列表項的高度:', itemHeight);
? ? ? ? console.log(' num.value', num.value);
? ? ? }

完整的scrollTop方法如下

總結:

滾動條要滾動起來
選中含有滾動條的元素,定義一個const leftRef = ref(null),在數(shù)據(jù)加載完成后設置leftRef.value.scrollTop

滾動條的位置
等待數(shù)據(jù)加載完后獲取當前選中的元素,通過.offsetHeight獲取元素的高度

到此這篇關于vue3手動設置滾動條位置自動定位功能的文章就介紹到這了,更多相關vue手動設置滾動條內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論