VUE 轮播图+3D Model 实现

项目场景:

提示:这里简述项目相关背景:

VUE+ELEMENTUI 跑马灯(轮播图)展示3D模型

问题描述

提示:这里描述项目中遇到的问题:

3D模型使用的是 vue-3d-model 组件,问题是组件在页面上直接展示正常,放到轮播图里面就无法显示(打开F12改变了页面长宽,就显示出来了),期间尝试过 this. n e x t T i c k ( ) t h i s . nextTick() this. nextTick()this.forceUpdate() v-if 等等情况都无法直接渲染成功

原因分析:

提示:这里填写问题的分析:

分析是model-obj的组件问题导致的。


解决方案:

提示:这里填写该问题的具体解决方案:

自定义一个组件,组件里放轮播图,轮播图的Item中放组件。这样最终效果是,页面刷新不展示3D模型,使用轮播图的切换按钮后,会加载对应的3D模型。
那么要解决的问题,就是模拟轮播图组件的点击的功能即可,最后的实现代码如下:

<template>
  <el-carousel trigger="click" height="400px" :autoplay="false"
               ref="carousel1"
               @change="changeModel"
               indicator-position="outside" arrow="always">
    <el-carousel-item v-for="(item,index) in models" :key="index">
      <model-obj
        v-if="activeKey == index"
        :lights="lights"
        :src="item.src" :mtl="item.mtl"
      ></model-obj>
    </el-carousel-item>
  </el-carousel>
</template>
<script>
import {ModelObj} from 'vue-3d-model'

export default {
  name: 'MyModel',
  components: {ModelObj},
  data() {
    return {
      models: [],
      activeKey: -1,
      lights: [{
        type: 'HemisphereLight',
        position: {x: 0, y: 1, z: 0},
        skyColor: 0xffffff,
        // groundColor: 0xFF0000, // 此代码为灯光后颜色
        intensity: 1,
      },
        {
          type: 'DirectionalLight',
          position: {x: 1, y: 1, z: 1},
          color: 0xffffff,
          intensity: .8,
        }]
    }
  },
  created(){
    this.getModelInfo();
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.carousel1.next();
      this.$forceUpdate();
    });
    setTimeout(() => {
      this.$refs.carousel1.prev();
      this.$forceUpdate();
    }, 300);
  },
  methods: {
    changeModel(key) {
      this.activeKey = key;
    },
    getModelInfo() {
      this.models = [
        {
          id: 0,
          src: 'mod/1.obj',
          mtl: 'mod/1.mtl',
        },
        {
          id: 1,
          src: 'mod/2.obj',
          mtl: 'mod/2.mtl',
        }
      ];
    },
  }
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现鼠标拖拽轮播,可以结合 Vue 和第三方插件来完成。以下是一个基于 Vuevue-awesome-swiper 插件实现鼠标拖拽轮播的示例: 1. 安装 vue-awesome-swiper 插件 ``` npm install vue-awesome-swiper --save ``` 2. 在 Vue 组件中引入并初始化 swiper 组件 ``` <template> <div class="swiper-container" ref="swiper"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(item, index) in list" :key="index"> <img :src="item.imgUrl" alt=""> </div> </div> </div> </template> <script> import Vue from 'vue' import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesomeSwiper) export default { data () { return { list: [ { imgUrl: 'http://xxx.com/1.jpg' }, { imgUrl: 'http://xxx.com/2.jpg' }, { imgUrl: 'http://xxx.com/3.jpg' }, { imgUrl: 'http://xxx.com/4.jpg' } ] } }, mounted () { this.$nextTick(() => { this.swiper = new this.$swiper(this.$refs.swiper, { slidesPerView: 'auto', freeMode: true, freeModeMomentumRatio: 0.5, freeModeMomentumBounce: false, freeModeMinimumVelocity: 0.1, grabCursor: true, mousewheel: true, resistanceRatio: 0.1 }) }) }, beforeDestroy () { if (this.swiper) { this.swiper.destroy() } } } </script> <style lang="scss"> .swiper-container { width: 100%; height: 100%; .swiper-wrapper { display: flex; .swiper-slide { width: 33.33%; img { width: 100%; } } } } </style> ``` 3. 在 mounted 钩子中初始化 swiper 实例,并设置相关参数。 ``` mounted () { this.$nextTick(() => { this.swiper = new this.$swiper(this.$refs.swiper, { slidesPerView: 'auto', freeMode: true, freeModeMomentumRatio: 0.5, freeModeMomentumBounce: false, freeModeMinimumVelocity: 0.1, grabCursor: true, mousewheel: true, resistanceRatio: 0.1 }) }) } ``` 通过设置 `freeMode: true` 启用 freeMode 模式,`grabCursor: true` 启用鼠标拖拽模式,`mousewheel: true` 启用鼠标滚轮控制模式等参数,可以实现鼠标拖拽轮播效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值