vue移动端原生小球滑块

效果

 

 

 

用到的一些事件

      阻止默认事件   : ev.preventDefault && ev.preventDefault();

      获取宽度:getBoundingClientRect().width

      点击位置在元素的位置:ev.changedTouches[0].pageX

<template>
  <div id="app">
    <div class="slider">
      <div class="line"></div>
      <div class="line ac"></div>
      <div class="box" @touchstart="fnStart"></div>
      <div class="box as" @touchstart="fnStart"></div>
    </div>
  </div>
</template>

js

<script>
export default {
  methods: {
    fnStart(ev) {
      // 计算点击位置在元素的坐标
      this.disX = ev.changedTouches[0].pageX - ev.target.offsetLeft;
      // 获取父节点
      this.parent = ev.target.parentNode;
      // 获取元素的宽
      this.ow = this.parent.getBoundingClientRect().width;
      // 计算除了元素的宽盒子还剩下多宽
      this.clienw = this.ow - ev.target.getBoundingClientRect().width;

      // 获取左边小圆球
      this.lcircle = this.parent.children[2];
      // 获取右边小圆球
      this.rcircle = this.parent.children[3];

      // 获取变色条
      this.line = this.parent.children[1];

      document.ontouchmove = this.fnmove;
      document.ontouchend = this.fnend;
    },
    fnmove(ev) {
      // 计算移动的距离
      this.ll = ev.changedTouches[0].pageX - this.disX;
      // 判断不能让小圆球到盒子外面
      if (this.ll < 0) this.ll = 0;
      if (this.ll > this.clienw) this.ll = this.clienw;
      // 右边线条
      if (ev.target.className.indexOf("as") != -1) {
        this.line.style.width =this.ll - this.parent.children[2].offsetLeft + "px";
        // 右边推动左边小圆球
        // 判断如果右边小球移动到左边小于左边小球offsetLeft的距离 如果当前为0 加一个小圆球的宽他们就不会重叠
        console.log(this.ll)
        if (this.ll < this.lcircle.offsetLeft + 30) {
          // 如果this.ll大于左边小球的值 当前this.ll-30就是左边小球left的值
          this.ind = this.ll - 30;
          
          console.log(this.ind)
          // 判断当前左边位置过等于0 就让他左边的位置等于0 就不会到盒子外面
          if (this.ind <= 0) {
            this.ind = 0;
          }
          // 如果this.ll的值小于小圆球的宽 两个圆就会重叠  所以让右边圆的left值为30
          if (this.ll < 30) {
            this.ll = 30;
          }

          this.line.style.left = this.ind + "px";
          this.lcircle.style.left = this.ind + "px";
        }
      } else {
        // 左线条
        // 获取左边的距离
        this.line.style.left = this.ll + "px";
        // 当前this.ll就是line多出来的宽   如果左边不动 offsetleft的值是300   this.ll是移动的距离
        this.line.style.width =
          this.parent.children[3].offsetLeft - this.ll + "px";
        // 左边推动右边小圆球  要把右边小球+30 不然两个小球就会重合到一起
        if (this.ll + 30 > this.rcircle.offsetLeft) {
          this.indX = this.ll + 30;

          if (this.indX > this.clienw) {
            this.indX = this.clienw;
          }

              // 判断当前移动的距离不能超过 this.clienw-30如果超过就会重叠
          if(this.ll>this.clienw-30){
            this.ll=this.clienw-30
          }



          this.line.style.left=this.indX+'px'
          this.rcircle.style.left=this.indX+'px'
        }
      }

      ev.target.style.left = this.ll + "px";
    },
    fnend() {
      document.ontouchmove = null;
      document.ontouchend = null;
    },
  },
};
</script>

css样式 


<style scoped lang="less">
.slider {
  height: 30px;
  width: 300px;
  background: #999;
  position: relative;
  margin: auto;
  .box {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: pink;
    position: absolute;
  }
  .box.as {
    background: blueviolet;
    right: 0;
  }
  // 线条
  .line {
    width: 300px;
    height: 5px;
    background: #eee;
    position: absolute;
  }
  .line.ac {
    background: rgb(247, 151, 25);
  }
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue.js 是一个流行的前端 JavaScript 框架,主要用于构建用户界面。在 Vue2 中,虽然原生的 API 不直接支持 3D 动画效果,但你可以借助第三方库如 `vue-rotate3d` 或 `@vue/composition-api` 结合 CSS 或 Web Animations API 来实现这种功能。 `vue-rotate3d` 是一个插件,它简化了在 Vue 组件中创建 3D 转动动画的过程。要在 Vue2 中实现一个元素围绕圆心进行 3D 旋转,你可以这样做: 1. 安装依赖: ```bash npm install vue-rotate3d ``` 2. 在组件中引入并使用旋转: ```html <template> <div class="rotation-animation" @mouseenter="startRotation" @mouseleave="stopRotation"> <!-- 你的内容 --> </div> </template> <script> import { useRotate3D } from 'vue-rotate3d'; export default { methods: { startRotation() { this.$rotate3d = useRotate3D({ element: this.$refs.yourElement, // 需要旋转的元素 rotation: { x: 0, y: 0, // 设置初始旋转角度 z: 0, // 可以设置围绕的轴 duration: 1000, // 旋转时间 easing: 'easeInOutQuart', // 动画速度曲线 onEnd: () => {} // 动画结束后执行的回调 }, autoReverse: false, // 是否每次旋转后反转方向 center: { x: 0.5, y: 0.5, z: 0 }, // 圆心坐标 }); this.$rotate3d.start(); }, stopRotation() { this.$rotate3d.stop(); } }, mounted() { // 如果元素在页面加载时就需旋转,可以在 mounted 钩子函数中调用 startRotation }, destroyed() { // 在组件销毁时停止旋转 this.$rotate3d.destroy(); }, }; </script> ``` 记得在样式中添加必要的CSS来使旋转效果可见,例如: ```css .rotation-animation { /* 添加宽高和定位以便动画生效 */ width: 100px; height: 100px; position: relative; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

至_臻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值