网站设计公司深,郑州seo推广优化,长沙银行网站建设,更合公司网站建设滑倒指定位置#xff1a;获取指定的dom#xff0c;然后用scrollIntoView使dom出现在视图区域
回到顶部#xff1a;操作父级dom的scrollTop 0#xff0c;让该父级下的列表回到顶部
代码如下
templatediv classtestDemodivel-bu…滑倒指定位置获取指定的dom然后用scrollIntoView使dom出现在视图区域
回到顶部操作父级dom的scrollTop 0让该父级下的列表回到顶部
代码如下
templatediv classtestDemodivel-button clicktoItem(id50)滑到指定位置/el-buttonel-button clicktoTop回到顶部/el-button/divdiv classboxdivv-for(item, index) in 99:keyindex:idid itemclassbox_item{{ item }}/div/div/div
/templatescript
export default {data() {return {};},methods: {// 滑到指定位置toItem(id) {this.$nextTick(() {const node document.getElementById(id); // 通过Id获取到对应的dom元素setTimeout(() {if (node) {this.$nextTick(() {node.scrollIntoView({behavior: smooth, //顺滑的滚动block: center, //容器上下的中间 start:顶部 end:底部 center:垂直方向居中inline: start, //容器左右的左边}); // 通过scrollIntoView方法将对应的dom元素定位到可见区域 【block: center】这个属性是在垂直方向居中显示});}}, 100);});},// 回到顶部toTop() {// 第一种方式// var et document.getElementsByClassName(box);// if (et et.length) {// et[0].scrollTop 0;// }// 第二种方式 较为平滑this.$nextTick(() {const node document.getElementById(id1);setTimeout(() {if (node) {this.$nextTick(() {node.scrollIntoView({behavior: smooth,block: start,inline: start,});});}}, 100);});},},
};
/script
style langscss scoped
.testDemo {display: flex;flex-direction: column;.box {flex-grow: 1;border: 1px solid #000;overflow: auto;.box_item {height: 50px;border: 1px solid #000;}}
}
/style