赞
踩
element.scrollTo({
top: 100,
left: 100,
behavior: "smooth",//平滑滚动
});
sticky
粘性定位,确保位置固定,在滚动时内容不会被覆盖;<template> <div> <el-card class="top-card"> <div class="search-wrap"> <div class="btns"> <el-button type="primary" size="small">新增</el-button> </div> <div class="search-item"> <span>北极星:</span> <el-input v-model="value" placeholder="请输入" style="width: 180px"></el-input> </div> <div class="search-item"> 组织: <el-select v-model="modelVal" placeholder="请选择" style="width: 200px"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" > </el-option> </el-select> </div> </div> <div class="end"> <el-button type="primary" size="small">搜索</el-button> <el-button type="plain" size="small">重置</el-button> </div> </el-card> <el-card class="center-card" ref="centerCardRef"> <div> <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName" stripe> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> </div> <div class="pagination-wrap"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4" :page-sizes="[100, 200, 300, 400]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="400" > </el-pagination> </div> </el-card> <span class="to-top" @click="handleToTop"> <i class="el-icon-download"></i> </span> </div> </template> <script> export default { data() { return { currentPage4: 4, value: '', modelVal: '', options: [ { value: '选项1', label: '黄金糕' }, { value: '选项2', label: '双皮奶' }, { value: '选项3', label: '蚵仔煎' }, { value: '选项4', label: '龙须面' }, { value: '选项5', label: '北京烤鸭' } ], tableData: [ { date: '2016-05-02', name: 'beiid', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-01', name: '北京举行虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-03', name: '大师课烂大街', address: '上海市普陀区金沙江路 1518 弄' } ] }; }, created() { let i = 3; while (i > 0) { this.tableData = this.tableData.concat([...this.tableData]); i--; } }, methods: { handleSizeChange(val) { console.log(`每页 ${val} 条`); }, handleCurrentChange(val) { console.log(`当前页: ${val}`); }, handleToTop() { window.scrollTo({ top: 0, behavior: 'smooth' }); }, tableRowClassName({ row, rowIndex }) { if (rowIndex === 1) { return 'warning-row'; } else if (rowIndex === 3) { return 'success-row'; } return ''; } } }; </script> <style lang="less" scoped> .top-card { position: sticky; top: 0; z-index: 20; margin-bottom: 15px; .end { display: flex; justify-content: flex-end; } .search-wrap { display: flex; padding: 10px 0 10px 10px; .btns { margin-right: 20px; } .search-item { margin-right: 20px; } } } .center-card { .pagination-wrap { display: flex; justify-content: flex-end; margin-top: 15px; padding-bottom: 10px; } } .to-top { position: sticky; bottom: 15%; left: 90%; z-index: 20; font-size: 30px; color: #74b9ff; cursor: pointer; i { transform: rotate(180deg); } } </style>
handleToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
},
window.scrollTo
方法不再生效;scrollTo
方法需要作用到滚动容器上,因为上面的demo案例是单纯的一个页面,并没有像实际项目中那样中页面层层嵌套;handleToTop() {
const scrollEle = document.querySelector('.contentContrnier')
console.log(scrollEle.scrollTop);
scrollEle.scrollTo({
top: 0,
behavior: 'smooth'
});
},
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。