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);

上面介紹使用方法 下面請看效果圖api

在這裏插入圖片描述

<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)">
		    <!-- <a :href="item.url"> -->
		      <img @click="imgus('aa'+index)" :src="item.image">
		    <!-- </a> -->
		  </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: 14rem;
		overflow: hidden;
	}
	.cube-slide-dots{
		position:absolute;
		bottom: 0.3rem;
	}
	.cube-slide-dots>span{
		height: 2px;
	}
</style>

引入了滴滴組件的能夠直接使用(建議參考)app

相關文章
相關標籤/搜索