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


    直接代码:

    1. <template>
    2. <div class="demo">
    3. <div class="third-part" id="发展历程">
    4. <div class="title">发展历程div>
    5. <div class="content" id="nav" v-if="dataList&&dataList.length>0">
    6. <div class="item" v-for="(item,index) in dataList" :key="index">
    7. <div>{{ item.month }}div>
    8. <div>{{ item.content }}div>
    9. div>
    10. div>
    11. <div class="year">
    12. <span :class="{'active': active==item}" @click="active=item" v-for="(item,index) in yearList" :key="index">{{ item }}span>
    13. div>
    14. div>
    15. div>
    16. template>
    17. <script>
    18. export default {
    19. data() {
    20. return {
    21. dataList: [
    22. { month: "2月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
    23. { month: "5月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
    24. { month: "7月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"},
    25. { month: "9月",content: "先后通过CMMI3级、ISO9000、ITSS3级等多项国际、国家级认证。"} //后期内容问UI
    26. ],
    27. active: 2023,
    28. yearList:['2023','2022','2021','2020']
    29. }
    30. },
    31. mounted(){
    32. this.$nextTick(()=> {
    33. this.scrollInit()
    34. })
    35. },
    36. methods: {
    37. scrollInit() {
    38. // 获取要绑定事件的元素
    39. const nav = document.getElementById("nav")
    40. var flag;
    41. // 鼠标按下
    42. var downX;
    43. // 鼠标点击的x下标
    44. var scrollLeft;
    45. // 当前元素滚动条的偏移量
    46. nav.addEventListener("mousedown", function (event) {
    47. console.log("触发mousedown");
    48. flag = true;
    49. downX = event.clientX;
    50. // 获取到点击的x下标
    51. scrollLeft = this.scrollLeft;
    52. // 获取当前元素滚动条的偏移量
    53. });
    54. nav.addEventListener("mousemove", function (event) {
    55. if (flag) {
    56. // 判断是否是鼠标按下滚动元素区域
    57. var moveX = event.clientX;
    58. // 获取移动的x轴
    59. var scrollX = moveX - downX;
    60. // 当前移动的x轴下标减去刚点击下去的x轴下标得到鼠标滑动距离
    61. this.scrollLeft = scrollLeft - scrollX
    62. // 鼠标按下的滚动条偏移量减去当前鼠标的滑动距离
    63. console.log(scrollX)
    64. //当滑动到400位置时让2022样式变化等等
    65. if(scrollLeft == 400 ){
    66. that.active=2022
    67. console.log("到400了");
    68. }else if(scrollLeft == 600){
    69. console.log("到600了");
    70. }
    71. }
    72. });
    73. // 鼠标抬起停止拖动
    74. nav.addEventListener("mouseup", function () {flag = false;});
    75. // 鼠标离开元素停止拖动
    76. nav.addEventListener("mouseleave", function (event) {flag = false;});
    77. },
    78. },
    79. }
    80. script>
    81. <style lang="scss" scoped>
    82. .demo {
    83. user-select: none;
    84. width: 1920px;
    85. }
    86. .third-part {
    87. width: 100%;
    88. height: 800px;
    89. background-image: url('../assets/img/about/aboutusbg3.png');
    90. background-size: 100% 100%;
    91. padding-top: 100px;
    92. box-sizing: border-box;
    93. .title {
    94. height: 60px;
    95. font-size: 48px;
    96. font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 2.0-800;
    97. line-height: 60px;
    98. color: #fff;
    99. }
    100. .content {
    101. margin-left: 300px;
    102. margin-top: 100px;
    103. width: 1469px;
    104. height: 235px;
    105. overflow-x: auto;
    106. // box-sizing: border-box;
    107. white-space: nowrap;
    108. &::-webkit-scrollbar {
    109. width: 0px;
    110. height: 0px;
    111. }
    112. .item {
    113. width: 403px;
    114. height: 230px;
    115. text-align: left;
    116. margin-right: 130px;
    117. display: inline-block;
    118. white-space: wrap;
    119. div:nth-child(1){
    120. height: 89px;font-size: 60px;
    121. font-family: Anton, Anton-400;
    122. color: #fff;
    123. line-height: 60px;
    124. border-left: 3px solid #fff;
    125. padding-left: 37px;
    126. }
    127. div:nth-child(2){
    128. height: 141px;
    129. width: 365px;
    130. font-size: 28px;
    131. font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 2.0-400;
    132. color: #ffffff;
    133. line-height: 50px;
    134. padding-left: 37px;
    135. border-left: 1px solid #fff;
    136. }
    137. }
    138. }
    139. .year {
    140. width: 1700px;
    141. height: 116px;
    142. margin-top: 114px;
    143. text-align: left;
    144. padding-left: 450px;
    145. span {
    146. display: inline-block;
    147. width: 200px;
    148. height: 100%;
    149. border-bottom: 1px solid #fff;
    150. font-size: 50px;
    151. font-family: Anton, Anton-400;
    152. color: #bebef6;
    153. line-height: 70px;
    154. padding: 30px 0;
    155. text-align: center;
    156. box-sizing: border-box;
    157. }
    158. span:hover {
    159. font-size: 70px;
    160. vertical-align: top;
    161. border-bottom: 4px solid #722ed1;
    162. }
    163. .active {
    164. font-size: 70px;
    165. vertical-align: top;
    166. border-bottom: 4px solid #722ed1;
    167. }
    168. }
    169. }
    170. style>

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

  • 相关阅读:
    mysql
    飞行动力学 - 第23节-运动方程的线化 之 基础点摘要
    「图论」判环、求环、最小环
    计算机毕业设计SSM电影推荐网站【附源码数据库】
    通用/参考电路/ 连接备份
    关于并发编程与线程安全的思考与实践
    【路径规划-TSP问题】基于遗传算法求解旅行商问题附matlab代码
    c# .NET 高级编程 高并发必备技巧(二) - 分布式锁
    php 进程通信系列 (四)共享内存
    CI/CD 工具和技术:释放 DevOps 的力量
  • 原文地址:https://blog.csdn.net/weixin_51747462/article/details/132845186