vue实现鼠标拖拽div左右移动的功能

直接代码:

<template>
  <div class="demo">
    <div class="third-part" id="发展历程">
      <div class="title">发展历程</div>
      <div class="content" id="nav" v-if="dataList&&dataList.length>0">
        <div class="item" v-for="(item,index) in dataList" :key="index">
          <div>{{ item.month }}</div>
          <div>{{ item.content }}</div>
        </div>
      </div>
      <div class="year">
        <span :class="{'active': active==item}" @click="active=item" v-for="(item,index) in yearList" :key="index">{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        dataList: [
        { month: "2月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
        { month: "5月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
        { month: "7月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
        { month: "9月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"} //后期内容问UI
      ],
      active: 2023,
      yearList:['2023','2022','2021','2020']
      }
    },
    mounted(){
    this.$nextTick(()=> {
      this.scrollInit()
    })
  },
  methods: {
    scrollInit() {
        // 获取要绑定事件的元素
        const nav = document.getElementById("nav")
        var flag; 
        // 鼠标按下
        var downX; 
        // 鼠标点击的x下标
        var scrollLeft; 
        // 当前元素滚动条的偏移量
        nav.addEventListener("mousedown", function (event) {
          console.log("触发mousedown");
          flag = true;
          downX = event.clientX; 
          // 获取到点击的x下标
          scrollLeft = this.scrollLeft; 
          // 获取当前元素滚动条的偏移量
        });
          nav.addEventListener("mousemove", function (event) {
            if (flag) { 
            // 判断是否是鼠标按下滚动元素区域
            var moveX = event.clientX; 
            // 获取移动的x轴
            var scrollX = moveX - downX; 
            // 当前移动的x轴下标减去刚点击下去的x轴下标得到鼠标滑动距离
            this.scrollLeft = scrollLeft - scrollX 
            // 鼠标按下的滚动条偏移量减去当前鼠标的滑动距离
            console.log(scrollX)
            //当滑动到400位置时让2022样式变化等等
            if(scrollLeft == 400 ){
              that.active=2022
              console.log("到400了");
            }else if(scrollLeft == 600){
              console.log("到600了");
            }
          }
          });
          // 鼠标抬起停止拖动
          nav.addEventListener("mouseup", function () {flag = false;});
          // 鼠标离开元素停止拖动
          nav.addEventListener("mouseleave", function (event) {flag = false;});
        },
  },
  }
</script>

<style lang="scss" scoped>
.demo {
  user-select: none;
  width: 1920px;
}
  .third-part {
    width: 100%;
    height: 800px;
    background-image: url('../assets/img/about/aboutusbg3.png');
    background-size: 100% 100%;
    padding-top: 100px;
    box-sizing: border-box;
    .title {
      height: 60px;
      font-size: 48px;
      font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 2.0-800;
      line-height: 60px;
      color: #fff;
    }
    .content {
      margin-left: 300px;
      margin-top: 100px;
      width: 1469px;
      height: 235px;
      overflow-x: auto;
      // box-sizing: border-box;
      white-space: nowrap;
      &::-webkit-scrollbar {
        width: 0px;
        height: 0px;
      }
      .item {
        width: 403px;
        height: 230px;
        text-align: left;
        margin-right: 130px;
        display: inline-block;
        white-space: wrap;
        div:nth-child(1){
          height: 89px;font-size: 60px;
          font-family: Anton, Anton-400;
          color: #fff;
          line-height: 60px;
          border-left: 3px solid #fff;
          padding-left: 37px;
        }
        div:nth-child(2){
          height: 141px;
          width: 365px;
          font-size: 28px;
          font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 2.0-400;
          color: #ffffff;
          line-height: 50px;
          padding-left: 37px;
          border-left: 1px solid #fff;
        }
      }
    }
    .year {
      width: 1700px;
      height: 116px;
      margin-top: 114px;
      text-align: left;
      padding-left: 450px;
      span {
        display: inline-block;
        width: 200px;
        height: 100%;
        border-bottom: 1px solid #fff;
        font-size: 50px;
        font-family: Anton, Anton-400;
        color: #bebef6;
        line-height: 70px;
        padding: 30px 0;
        text-align: center;
        box-sizing: border-box;
      }
      span:hover {
        font-size: 70px;
        vertical-align: top;
        border-bottom: 4px solid #722ed1;
      }
      .active {
        font-size: 70px;
        vertical-align: top;
        border-bottom: 4px solid #722ed1;
      }
    }
  }
</style>

这部分区域可以鼠标拖拽左右滚动

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现 div 横向拖动并互换位置,可以使用 Vue.js 和一些 JavaScript 库来实现。 首先,你需要在 Vue.js 中创建一个拖动事件。你可以使用 `v-on:mousemove` 来监听鼠标移动事件,并使用 `v-on:mousedown` 来监听鼠标按下事件。在鼠标按下事件中,你需要记录鼠标的当前位置。 接下来,在鼠标移动事件中,你需要计算鼠标的偏移量,并将偏移量添加到 div 的 left 样式中。这将使 div 沿着 x 轴移动。 当鼠标释放时,你需要计算 div 的位置,并使用 JavaScript 库来实现 div 位置的互换。你可以使用 Sortable.js 或者 Draggable.js 这样的库来实现拖拽和位置交换。 最后,你需要将这些逻辑封装在一个 Vue.js 组件中,以便在应用程序中使用。以下是一个简单的示例: ```html <template> <div class="container"> <div v-for="(item, index) in items" :key="index" :class="{ active: activeIndex === index }" v-bind:style="{ left: item.left + 'px' }" v-on:mousedown="startDrag(index, $event)"> {{ item.text }} </div> </div> </template> <script> import Sortable from 'sortablejs'; export default { data() { return { items: [ { text: 'Item 1', left: 0 }, { text: 'Item 2', left: 100 }, { text: 'Item 3', left: 200 }, { text: 'Item 4', left: 300 }, ], activeIndex: null, startX: 0, }; }, mounted() { const container = this.$el.querySelector('.container'); Sortable.create(container, { animation: 150, swapThreshold: 0.5, onSwap: (evt) => { const { oldIndex, newIndex } = evt; const item = this.items.splice(oldIndex, 1)[0]; this.items.splice(newIndex, 0, item); }, }); }, methods: { startDrag(index, event) { this.activeIndex = index; this.startX = event.clientX; window.addEventListener('mousemove', this.drag); window.addEventListener('mouseup', this.stopDrag); }, drag(event) { const deltaX = event.clientX - this.startX; this.items[this.activeIndex].left += deltaX; this.startX = event.clientX; }, stopDrag() { window.removeEventListener('mousemove', this.drag); window.removeEventListener('mouseup', this.stopDrag); this.activeIndex = null; }, }, }; </script> <style> .container { display: flex; position: relative; width: 100%; height: 100px; background-color: #f5f5f5; } .container > div { position: absolute; top: 0; height: 100%; width: 100px; background-color: #fff; border: 1px solid #ccc; display: flex; justify-content: center; align-items: center; cursor: move; } .container > div.active { z-index: 1; } </style> ``` 在这个示例中,我们创建了一个包含多个 div 的容器,并使用 Vue.js 的数据绑定将每个 div 的位置绑定到数据模型中。当用户按下鼠标并开始拖动 div 时,我们记录 div 的当前位置,并在每次移动时计算偏移量。当用户释放鼠标时,我们使用 Sortable.js 库来实现 div 的位置交换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值