日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue 点击div 获取位置_vue 点击元素滚动到指定位置

發布時間:2023/12/15 vue 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue 点击div 获取位置_vue 点击元素滚动到指定位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • {{item}}

data() {return{

navgator: ['列表A','列表B','列表C','列表D',

],

navgatorIndex:0,

listBox: ['A','B','C','D'],

listBoxState:true,//點擊導航欄時,暫時停止監聽頁面滾動

};

},

created() {

},

mounted() {

let timeId;

window.addEventListener('scroll', () =>{//頁面滾動停止100毫秒后才會執行下面的函數。

clearTimeout(timeId);

timeId= setTimeout(() =>{this.scrollToTop();

console.log('執行完了哦');

},100);

} ,true);

},

methods: {//點擊導航菜單,頁面滾動到指定位置

handleLeft(index) {this.navgatorIndex =index;this.$el.querySelector(`#id${index}`).scrollIntoView({

behavior:"smooth", //平滑過渡

block: "start" //上邊框與視窗頂部平齊。默認值

});this.listBoxState=false;

let timeId;

clearTimeout(timeId);

timeId= setTimeout(() =>{this.listBoxState=true;

},200);

},//監聽頁面元素滾動,改變導航欄選中

scrollToTop() {//獲取視窗高度

var domHight =document.body.offsetHeight;//dom滾動位置

var scrollTop = window.pageYOffset || document.documentElement.scrollTop ||document.body.scrollTop;if (this.listBoxState) {//作用是點擊導航欄時,延遲這里執行。

this.listBox.map((v,i) =>{//獲取監聽元素距離視窗頂部距離

var offsetTop =document.getElementById(`id${i}`).offsetTop;//獲取監聽元素本身高度

var scrollHeight =document.getElementById(`id${i}`).scrollHeight;//如果 dom滾動位置 >= 元素距離視窗距離 && dom滾動位置 <= 元素距離視窗距離+元素本身高度//則表示頁面已經滾動到可視區了。

if (scrollTop >= offsetTop && scrollTop<=(offsetTop+scrollHeight)) {//導航欄背景色選中

this.navgatorIndex =i;

}

})

}

},

},

}

width:100%;

background: #ededed;

}

.navgator {

width: 200px;

position:fixed;

top:0;

.navgatorLi {

width:100%;

height: 1rem;

display: flex;

justify-content: center;

align-items: center;

border: 1px solid #ccc;

border-top: none;

}

.isActive {

color: #fff;

background: darkseagreen;

}

}

.rightList {

width: 560px;

margin-left : 200px;

li {

width:100%;

height: 10rem;

display: flex;

justify-content: center;

align-items: center;

border: 1px solid #ccc;

}

}

總結

以上是生活随笔為你收集整理的vue 点击div 获取位置_vue 点击元素滚动到指定位置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。