轮播图组件更加完善版

news/2024/9/27 20:02:31 标签: 前端, javascript, 开发语言

依然是基于微博语法开发,使用时请注意标签替换

优化了滑动的效果,默认的索引,滑动距离等,

使用方式和以前一样没变,主要修改了组件内部

javascript"><template>
  <wbx-view class="" style="width: 100vw;height: 70vh;">
    <wbx-view style="margin-top: 100px; margin-left: 100px;margin-bottom: 20px;">
    <WBXswiper  @change="gaibian" :vertical="false"  :current="5" indicatorActiveColor="#fff" indicatorColor="#c0c0c0" :originalData="items"   style="width: 200px;height: 200px;border-radius: 20px;">
       <template slot="swiperItem" slot-scope="scope">
			    <wbx-image :src="scope.item.src" mode="scaleToFill" style="width:200px; height: 200px;" />
       </template>
    </WBXswiper>
    </wbx-view>
	</wbx-view>
</template>

<script>
/**
 * @type WBXAppOption
 */
import WBXswiper from "../../commpents/WBXswiper/index.vue";
const pageOptions = {
  data() {
    return {
      items: [{
            src: 'res/001.jpg',
            txt:222222
		  },
          {
            src: 'res/001.jpg',
            txt:222222
		  },{
            src: 'res/001.jpg',
            txt:222222
		  },{
            src: 'res/001.jpg',
            txt:222222
		  },{
            src: 'res/001.jpg',
            txt:222222
		  }],
      current:0
     }
  },
  computed:{
  },
  methods: {
    gaibian(e){
    },
    add(index){
      this.current=index
    }
  },
  components: {
    WBXswiper,
  },
  wbox: {
    onLoad() { },
    onShow() {
      // 页面显示/切入前台时触发
    },
    onHide() {
      // 页面隐藏时触发
    },
    onUnload() {
      // 页面退出时触发
    },
  },
  mounted() { },
};
export default pageOptions;
</script>

<style></style>

已下是组件内部

javascript"><template>
    <wbx-view
      ref="objStyle"
      :style="wrapperStyle"
      @touchstart="onTouchStart"
      @touchmove="onTouchMove"
      @touchend="onTouchEnd"
    >
      <wbx-view
        class="carousel-wrapper"
        :style="carouselStyle"
        @transitionend="onTransitionEnd"
        ref="carouselWrapper"
      >
        <wbx-view :style="itemStyle">
          <slot name="swiperItem" :item="originalData[originalData.length - 1]"></slot>
        </wbx-view>
        <wbx-view v-for="(item, index) in originalData" :key="index" :style="itemStyle">
          <slot name="swiperItem" :item="item"></slot>
        </wbx-view>
        <wbx-view :style="itemStyle">
          <slot name="swiperItem" :item="originalData[0]"></slot>
        </wbx-view>
      </wbx-view>
      <wbx-view v-if="indicatorDots" :style="{ width: containerWidth + 'px' }" style="position: absolute; bottom: 10px; display: flex; flex-direction: row; justify-content: center;">
        <wbx-view
          v-for="(item, index) in originalData"
          :key="index"
          :style="{ backgroundColor: index === realIndex ? indicatorActiveColor : indicatorColor }"
          style="width: 10px; height: 10px; margin: 0 5px; cursor: pointer; border-radius: 10px;"
          @click~stop="setCurrentIndex(index)"
        ></wbx-view>
      </wbx-view>
    </wbx-view>
  </template>
  
  <script>
  /*
  originalData          数据
  autoPlay              是否自动播放
  interval              自动播放间隔时间
  indicatorDots         是否显示指示点
  indicatorColor        指示点颜色
  indicatorActiveColor  当前选中的指示点颜色
  current               当前所在滑块的index
  vertical              滑动方向是否为纵向
  @change               轮播图改变时会触发 change 事件,返回当前索引值
  */
  export default {
    props: {
      originalData: {
        type: Array,
        required: true
      },
      autoPlay: {
        type: Boolean,
        default: false
      },
      interval: {
        type: Number,
        default: 3000
      },
      indicatorDots: {
        type: Boolean,
        default: true
      },
      indicatorColor: {
        type: String,
        default: '#c0c0c0'
      },
      indicatorActiveColor: {
        type: String,
        default: '#fff'
      },
      current: {
        type: String,
        default: ''
      },
      vertical: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        currentIndex: 1,
        timer: null,
        startX: 0,
        startY: 0,
        offset: 0,
        isTransitioning: false,
        containerWidth: 0,
        containerHeight: 0,
        userCurrent:false,
        userCurrentStare:false,

      };
    },
    watch: {
      current: {
          handler(newVal) {
            this.userCurrent=true
            this.setCurrentIndex(newVal);
          },
          immediate: true
      }
    },
    computed: {
      wrapperStyle() {
        return {
          position: "relative",
          width: `${this.wrapperWidth}px`,
          height: `${this.wrapperHeight}px`,
        };
      },
      carouselStyle() {
        const baseTranslateValue = -this.currentIndex * (this.vertical ? this.containerHeight : this.containerWidth);
        const translateValue = baseTranslateValue + this.offset;
        return {
          display: 'flex',
          flexDirection: this.vertical ? 'column' : 'row',
          transform: this.vertical ? `translateY(${translateValue}px)` : `translateX(${translateValue}px)`,
          transition: this.isTransitioning ? 'transform 0.3s ease-out' : 'none',
           width: !this.vertical ? `${this.wrapperWidth}px` : `${this.containerWidth}px`,
          height: this.vertical ? `${this.wrapperHeight}px` : `${this.containerWidth}px`
        };
      },
      wrapperWidth() {
        return this.containerWidth * (this.originalData.length + 2);
      },
      wrapperHeight() {
        return this.containerHeight * (this.originalData.length + 2);
      },
      itemStyle() {
        return {
          width: !this.vertical ? `${this.containerWidth}px` : `${this.containerWidth}px`,
          height: this.vertical ? `${this.containerHeight}px` : `${this.containerWidth}px`,
          flexShrink: 0
        };
      },
      realIndex() {
        return (this.currentIndex - 1 + this.originalData.length) % this.originalData.length;
      }
    },
    mounted() {
      this.updateDimensions();
      this.$nextTick(() => {
        if (this.autoPlay) {
          this.startAutoPlay();
        }
      });
    },
    beforeDestroy() {
      this.stopAutoPlay();
    },
    methods: {
      updateDimensions() {
        if (this.$refs.objStyle) {
          const objStyle =  this.$refs.objStyle.styleObject
          this.containerWidth = parseFloat(objStyle.width);
          this.containerHeight = parseFloat(objStyle.height);
        }
      },
      startAutoPlay() {
        this.timer = setInterval(() => {
          this.next();
        }, this.interval);
      },
      stopAutoPlay() {
        if (this.timer) {
          clearInterval(this.timer);
          this.timer = null;
        }
      },
      next() {
        this.offset = 0;
        this.isTransitioning = true;
        this.currentIndex += 1;
        this.$emit('change', { current: this.currentIndex });
      },
      prev() {
        this.offset = 0;
        this.isTransitioning = true;
        this.currentIndex -= 1;
        this.$emit('change', { current: this.currentIndex });
      },
      setCurrentIndex(index) {
        this.stopAutoPlay();
        if (this.current !== '') {
          // 传值情况
          this.userCurrentStare = this.userCurrent ? true : !this.userCurrentStare;
        } else {
          // 不传值情况
          this.userCurrentStare = index !== '';
        }
        this.currentIndex = index + 1;
        if (this.autoPlay) {
          this.startAutoPlay();
        }
      },
      onTouchStart(e) {
        this.startX = e.touches[0].clientX;
        this.startY = e.touches[0].clientY;
        this.offset = 0;
        this.stopAutoPlay();
      },
      onTouchMove(e) {
        const moveX = e.touches[0].clientX;
        const moveY = e.touches[0].clientY;
        if(this.vertical){
          if(moveY - this.startY>=this.containerHeight){
            this.offset=this.containerHeight
          }else if(moveY - this.startY<= -this.containerHeight){
            this.offset=-this.containerHeight
          }else{
            this.offset= moveY - this.startY;
          }
        }else{
          if(moveX - this.startX>=this.containerWidth){
            this.offset=this.containerWidth
          }else if(moveX - this.startX<= -this.containerWidth){
            this.offset=-this.containerWidth
          }else{
            this.offset= moveX - this.startX;
          }
        }
      },
      onTouchEnd() {
        this.isTransitioning = true;
        if (Math.abs(this.offset) > (this.vertical ? this.containerHeight : this.containerWidth) /6) {
          if (this.offset > 0) {
            this.prev();
          } else {
            this.next();
          }
        } else {
          this.offset = 0;
        }
        if (this.autoPlay) {
          this.startAutoPlay();
        }
      },
      onTransitionEnd() {
        this.isTransitioning = false;
        this.offset = 0;
        if (this.currentIndex === this.originalData.length + 1) {
          this.currentIndex = 1;
        }
        if (this.currentIndex === 0) {
          this.currentIndex = this.originalData.length;
        }
      }
    }
  };
  </script>
  
  <style>
  </style>

http://www.niftyadmin.cn/n/5679431.html

相关文章

【企业微信】群机器人自动消息配置

0、群聊机器人 内部企微群聊可以添加一个机器人&#xff0c;这个机器人其实是个消息接口&#xff0c;可以外部脚本来自动定时发送消息到群里&#xff0c;打工人最有用的提醒就是每周提醒发周报了。 1、创建机器人 一般公司都没有人使用&#xff0c;我们可以手动创建一个。 …

观测云链路追踪分析最佳实践

背景 如果要在开发、运维和工程层面持续改进一个涉及多服务的应用&#xff0c;以链路追踪、日志检索、指标收集、用户体验监测、性能剖析、关联分析等作为代表性技术的可观测性必不可少&#xff0c;这一看法已成为共识&#xff0c;但在采用这项技术的过程中&#xff0c;如何分…

【数据结构】什么是二叉搜索(排序)树?

&#x1f984;个人主页:修修修也 &#x1f38f;所属专栏:数据结构 ⚙️操作环境:Visual Studio 2022 目录 &#x1f4cc;二叉搜索(排序)树的概念 &#x1f4cc;二叉搜索(排序)树的操作 &#x1f38f;二叉搜索树的查找 &#x1f38f;二叉搜索树的插入 &#x1f38f;二叉搜索树的…

Linux相关概念和重要知识点(8)(操作系统、进程的概念)

1.操作系统&#xff08;OS&#xff09; &#xff08;1&#xff09;基本结构的认识 任何计算机系统都包含一个基本的程序集合&#xff0c;用于实现计算机最基本最底层的操作&#xff0c;这个软件称为操作系统。操作系统大部分使用C语言编写&#xff0c;少量使用汇编语言。 从…

宝塔面板部署雷池社区版教程

宝塔面板部署雷池社区版教程 简单介绍一下宝塔面板&#xff0c;安全高效的服务器运维面板&#xff0c;使用宝塔面板的人非常多 在网站管理上&#xff0c;许多用户都是通过宝塔面板进行管理&#xff0c;宝塔面板的Nginx默认监听端口为80和443&#xff0c;这就导致共存部署时雷池…

【linux】轻松掌握文件管理:安装Ranger并设置Micro为默认编辑器

在Linux系统中&#xff0c;高效的文件管理和文本编辑是日常工作的重要组成部分。今天&#xff0c;我们将介绍如何安装强大的命令行文件管理器Ranger&#xff0c;并将现代化的终端文本编辑器Micro设置为默认编辑器。这个组合不仅能提高您的工作效率&#xff0c;还能让您的终端操…

vue echarts tooltip使用动态模板

先上代码 tooltip: {// 这里是车辆iconshow: true,// trigger: "item",// backgroundColor: "transparent",appendToBody: true,textStyle: {color: "#ffffff" //设置文字颜色},formatter: (params) > {return formatHtml(params.data)},}, …

三十二、领域驱动设计DDD(战术设计)

DDD 战术设计详解与实例 DDD 的战术设计关注如何在具体实现中应用领域模型的原则&#xff0c;包括聚合、服务、工厂等设计模式。以下是每个核心概念的详细介绍和实例。 1 聚合 (Aggregate) 定义&#xff1a;聚合是一组相关联的对象&#xff0c;它们共同表现一个一致的业务规…