滚动条悬浮(可以优化)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-17 01:53   11   0
  // 滚动条
  fixTableScroller: function() {
    let tableH = $(".master-table-container").height(); // table高度
    let winH = $(window).height(); // 浏览器高度
    let ol = 0; // table左边距
    let wTop = 0; // 滚动条top
    $("#scroll-container").css({ // 悬浮scroll宽度
        width: ($(".master-table-container").width()) + "px"
    });
    if ($(".master-table").width() >$(".master-table-container").width()) {
        if ($(window).scrollLeft() == 0) {
          ol = $(".right").offset().left;
        } else {
          ol = $(".right").offset().left - $(window).scrollLeft();
        }
        // 悬浮scroll距离左边的left值
        $("#scroll-container").css({
          left: (ol+20) + "px",
          display: "block"
        });
        $(".master-scroller-content, .master-batch-operate").css({
          width: $(".master-table").width() + "px"
        });
    } else {
      $("#scroll-container").css({
        opacity: 0,
        display: "none"
      });
    }
    $(window).on("scroll", function(e) {
        wTop = $(this).scrollTop();
        if ($(this).scrollLeft() == 0) {
          ol = $(".right").offset().left;
        } else {
          ol = $(".right").offset().left - $(this).scrollLeft();
        }
         // 悬浮scroll距离左边的left值
        $("#scroll-container").css({
            left: (ol+20) + "px"
        });
        $(".master-scroller-content").css({
          width: $(".master-table").width() + "px"
        });
        // 判断浏览器的高度是否大于table表格高度
        let perNum = 0;
        console.log($(window).height(), tableH, (wTop/tableH).toFixed(2))
        if ($(window).height() >= 910) {
          perNum = 0.12;
        } else if (winH < 910 && winH>= tableH) {
          perNum = 0.74;
        } else {
          perNum = 0.42;
        }
        if ($(window).height() >tableH) {
          $("#scroll-container").css({
            opacity: 0,
            display: "none"
          });
        } else {
          if ((wTop/tableH).toFixed(2)<=perNum && tableH !=0 && $(".master-table").width() >$(".master-table-container").width()) {
            $("#scroll-container").css({
              opacity: 1,
              display: "block"
            });
          } else {
            $("#scroll-container").css({
              opacity: 0,
              display: "none"
            });
          }
        }
    });
    $("#scroll-container").on("scroll", function(e) {
        $(".master-container").scrollLeft($(this).scrollLeft());
    });
  }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP