VUE移动到指定位置(scrollIntoView)----亲测避坑

用(scrollIntoView)来实现移动到指定位置建议不要放在(mt-loadmore)里使用,不然头部会被挤上去----亲测


html

<div id="pronbit" ref="pronbit">需要移动到的位置</div>

js

//选中id
document.getElementById(e).scrollIntoView({
	behavior: "smooth",  // 平滑过渡
	block:    "start"  // 上边框与视窗顶部平齐。默认值
});
// 选中ref
this.$refs.pronbit.scrollIntoView({
	behavior: "smooth",  // 平滑过渡
	block:    "start"  // 上边框与视窗顶部平齐。默认值
});



//要是放在mounted()里执行使用
this.$refs.pronbit.scrollIntoView();//不然只执行一次刷新了也一样

//禁止scrollIntoView
this.$refs.pronbit.scrollIntoView(false);

上面介绍使用方法 下面请看效果图
在这里插入图片描述

<template>
	<div id="app">
	<!-- 轮播 -->
	<div class="planting">
	<cube-slide ref="slide" :data="items" @change="changePage">
		<cube-slide-item v-for="(item, index) in items" :key="index" @click.native="clickHandler(item, index)">
		    <img @click="imgus('aa'+index)" :src="item.image">
		</cube-slide-item>
	</cube-slide> 
	</div>
	<!-- 轮播 -->
	<img style="width: 100%;float: left;" v-for="(item,index) in items" :id="'aa'+index" :src="item.image">
	<img style="width: 100%;float: left;" v-for="(item,index) in 3" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1076778629,484203681&fm=11&gp=0.jpg">
	</div>
</template>

<script>
import { getHomeData } from "@/api/public";
import dialog from "@/utils/dialog";//弹窗
export default {
  props: {
    msg: String
  },
  data() {
    return {
		items: [
		{
		  url: '',
		  image: 'http://img3.imgtn.bdimg.com/it/u=1960330002,2943700579&fm=26&gp=0.jpg'
		},
		{
		  url: '',
		  image: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=225193914,751284870&fm=26&gp=0.jpg'
		},
		{
		  url: '',
		  image: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3019995954,636527762&fm=26&gp=0.jpg'
		}
		],
    }
  },
  mounted(){
  },
  methods: {
	  //滚动指定位置
	  imgus:function(e){
	  	document.getElementById(e).scrollIntoView({
	  	  behavior: "smooth",  // 平滑过渡
	  	  block:    "start"  // 上边框与视窗顶部平齐。默认值
	  	});
	  },
	  // 轮播
	  changePage(current) {
	    console.log('当前轮播图序号为:' + current)
	  },
	  clickHandler(item, index) {
	    console.log(item, index)
	  },
   },
}
</script>
<style>
	.cube-slide-item img{
		width: 100%;
		height: 100%;
	}
	.planting{
		width: 100%;
		height: 6rem;
		overflow: hidden;
	}
	.cube-slide-dots{
		position:absolute;
		bottom: 0.3rem;
	}
	.cube-slide-dots>span{
		height: 2px;
	}
</style>

引入了滴滴组件的可以直接使用(建议参考)


有什么问题欢迎评论留言,我会及时回复你的

原创文章 75 获赞 87 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43764578/article/details/105659082
今日推荐