• vue2/3 tab栏切换 可左右滑动的处理


    1. <script>
    2. import { defineComponent, computed, toRefs, reactive } from 'vue'
    3. import { jsBridge } from "@/utils/jsbridge";
    4. export default defineComponent({
    5. name: 'Gooddetails',
    6. setup() {
    7. const lData = reactive({
    8. tabList: [],
    9. curTabIndx: 0
    10. })
    11. const curTab = computed(() => {
    12. return lData.tabList.find(v => v.select);
    13. })
    14. const _init = ()=> {
    15. let list = ["京东","淘宝/天猫/饿了么","美团外卖","拼多多"];
    16. list.forEach((text, idx) => {
    17. lData.tabList.push({
    18. text,
    19. id: idx, // tab标识
    20. select: idx == 0, // 是否被选择
    21. index: idx // 处于显示的位置
    22. });
    23. });
    24. }
    25. _init()
    26. const onClickTab = (tabInfo, idx) => {
    27. lData.curTabIndx = idx
    28. let curTabInfo = curTab.value;
    29. if (curTabInfo.id == tabInfo.id) return;
    30. let { index, id } = tabInfo;
    31. // 滑动控件
    32. let scroller = document.getElementById("scroller");
    33. let speed = scroller.scrollWidth / lData.tabList.length;
    34. let tab = document.getElementById(`tab-${id}`);
    35. let bWidth = document.body.clientWidth;
    36. // 点击右边
    37. if (curTabInfo.index < index && tab.clientWidth * index >= bWidth - speed) {
    38. // 滑动的距离
    39. scroller.scrollLeft = (index + 2) * speed - bWidth;
    40. } else if (curTabInfo.index > index && (tab.clientWidth * index - (scroller.scrollLeft + bWidth) < speed)) {
    41. // 滑动的距离
    42. scroller.scrollLeft = (index - 1) * speed;
    43. }
    44. curTabInfo.select = false;
    45. lData.tabList[index].select = true;
    46. }
    47. return { ...toRefs(lData), curTab, onClickTab }
    48. }
    49. })
    50. script>
    51. <style lang="scss" >
    52. @import "../../assets/styles/common.css";
    53. .lose {
    54. width: 100%;
    55. position: relative;
    56. overflow:hidden;
    57. .af{
    58. width: .48rem;
    59. height: .4rem;
    60. background-color: #f7f7f7;
    61. position: absolute;
    62. top: 0.32rem;
    63. left: 0;
    64. z-index: 2;
    65. }
    66. .tab-layout {
    67. overflow-x: scroll;
    68. display: flex;
    69. flex-wrap: nowrap;
    70. padding: .32rem 0 .28rem .48rem;
    71. position: relative;
    72. .ul:first-child{
    73. .tab-item{
    74. padding-left: 0;
    75. }
    76. }
    77. .tab-item {
    78. // min-width: 1rem;
    79. padding:0 .32rem;
    80. text-align: center;
    81. height: .4rem;
    82. font-size: .28rem;
    83. font-family: PingFangSC-Regular, PingFang SC;
    84. font-weight: 400;
    85. color: #333333;
    86. line-height: .4rem;
    87. white-space: nowrap;
    88. span.active {
    89. border-bottom:1px solid red;
    90. }
    91. }
    92. }
    93. }
    94. style>

  • 相关阅读:
    算法|图论 2
    列出使用Typescript的一些优点?
    Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
    QT QInputDialog弹出消息框用法
    Django日志配置
    网工笔记整理:策略工具Filter-policy的使用
    C. Link Cut Centroids(树的重心性质)
    Open3D(C++) 整体最小二乘拟合平面
    分享一下大转盘抽奖小程序怎么做
    Rocky Linux 工作流编排和混合云解决方案一站式资源
  • 原文地址:https://blog.csdn.net/m0_62015496/article/details/128134991