您好,登錄后才能下訂單哦!
本篇內容主要講解“vue實現錨點定位的代碼怎么寫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“vue實現錨點定位的代碼怎么寫”吧!
具體代碼如下:
vue
<el-row :gutter="20"> <el-col :span="3"> <!--導航選擇事件--> <el-menu :default-active="activeStep" @select="jump"> <el-menu-item v-for="(item,index) in menuData" :index="`${index}`" :key="item.subjectId"> <i class="el-icon-menu"></i> <span slot="title">{{item.subjectName}}</span> </el-menu-item> </el-menu> </el-col> <!--綁定scroll事件需要監聽--> <el-col :span="21" class="scroll_cls" @scroll="onScroll"> <div v-for="(item,index) in tableObject" :key="index" > <div class="title scroll-item" :id="item.name">{{item.name}}</div> <el-table :data="item.rows" :key="index"> <el-table-column label="序號" type="index" width="50"></el-table-column> <el-table-column prop="channelId" label="渠道/團隊id"></el-table-column> <el-table-column prop="channelName" label="渠道/團隊"></el-table-column> <el-table-column prop="ruleCode" label="分配方案id"></el-table-column> <el-table-column prop="ruleName" label="分配方案名稱"></el-table-column> <el-table-column prop="ruleVersion" label="版本號"></el-table-column> <el-table-column prop="hierarchy" label="級別"> <template slot-scope="scope"> <span>{{scope.row.hierarchy == 1 ? '項目' : '渠道團隊'}}</span> </template> </el-table-column> <el-table-column label="有效期"> <template slot-scope="scope"> <span>{{scope.row.beginTime + '-' + scope.row.endTime}}</span> </template> </el-table-column> <el-table-column prop="creatorName" label="操作人"></el-table-column> <el-table-column prop="createTime" label="操作時間"></el-table-column> <el-table-column prop="orderCnt" label="關聯訂單"> <template slot-scope="scope"> <el-button @click="orderHandleClick(scope.row.orderCnt)" type="text" size="small" >{{scope.row.orderCnt}}</el-button> </template> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="settingHandleClick(scope.row)" type="text" size="small">設置分配方案</el-button> </template> </el-table-column> </el-table> <el-pagination v-if="item.total > 5" size="small" @size-change="handleSizeChange($event,index)" @current-change="handleCurrentChange($event,index)" :current-page="ruleForm.ageNum" :page-sizes="[10, 30, 50, 100]" :page-size="ruleForm.pageSize" layout="total, sizes, prev, pager, next" :total="item.total" ></el-pagination> </div> </el-col> </el-row>
js
// 滾動觸發按鈕高亮 methods: { onScroll(e) { let scrollItems = document.querySelectorAll(".scroll-item"); console.log(scrollItems) console.log(e) for (let i = scrollItems.length - 1; i >= 0; i--) { // 判斷滾動條滾動距離是否大于當前滾動項可滾動距離 let judge = e.target.scrollTop >= scrollItems[i].offsetTop - scrollItems[0].offsetTop; if (judge) { console.log(i) this.activeStep = i.toString(); break; } } }, jump(index) { console.log(index) let target = document.querySelector(".scroll_cls"); let scrollItems = document.querySelectorAll(".scroll-item"); // 判斷滾動條是否滾動到底部 if (target.scrollHeight <= target.scrollTop + target.clientHeight) { console.log(index) console.log(typeof index) this.activeStep = index; } let total = scrollItems[index].offsetTop - scrollItems[0].offsetTop; // 錨點元素距離其offsetParent(這里是body)頂部的距離(待滾動的距離) console.log(total) let distance = document.querySelector(".scroll_cls").scrollTop; // 滾動條距離滾動區域頂部的距離 console.log(distance) // let distance = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset // 滾動條距離滾動區域頂部的距離(滾動區域為窗口) // 滾動動畫實現, 使用setTimeout的遞歸實現平滑滾動,將距離細分為50小段,10ms滾動一次 // 計算每一小段的距離 let step = total / 50; if (total > distance) { smoothDown(document.querySelector(".scroll_cls")); } else { let newTotal = distance - total; step = newTotal / 50; smoothUp(document.querySelector(".scroll_cls")); } // 參數element為滾動區域 function smoothDown(element) { if (distance < total) { distance += step; element.scrollTop = distance; setTimeout(smoothDown.bind(this, element), 10); } else { element.scrollTop = total; } } // 參數element為滾動區域 function smoothUp(element) { if (distance > total) { distance -= step; element.scrollTop = distance; setTimeout(smoothUp.bind(this, element), 10); } else { element.scrollTop = total; } } document.querySelectorAll('.scroll-item').forEach((item, index1) => { if (index === index1) { item.scrollIntoView({ block: 'start', behavior: 'smooth' }) } }) }, }, mounted() { this.$nextTick(() => { console.log(1) window.addEventListener('scroll', this.onScroll,true) }) },
css
.scroll_cls { height: 500px; overflow: auto; }
Vue具體輕量級框架、簡單易學、雙向數據綁定、組件化、數據和結構的分離、虛擬DOM、運行速度快等優勢,Vue中頁面使用的是局部刷新,不用每次跳轉頁面都要請求所有數據和dom,可以大大提升訪問速度和用戶體驗。
到此,相信大家對“vue實現錨點定位的代碼怎么寫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。