javascript实现内容溢出手动滚动

HTML代码:

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8">
    <title>溢出滚动</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      
      .body {
        width: 300px;
        height: 500px;
        background: red;
        margin: 100px auto;
        padding: 20px;
        overflow: hidden;
      }
      
      .list {
        list-style: none;
      }
      
      .item {
        background: blue;
        margin-bottom: 15px;
      }
      
      .item:nth-child(1) {
        height: 200px;
      }

      .item:nth-child(2) {
        height: 100px;
      }

      .item:nth-child(3) {
        height: 130px;
      }

      .item:nth-child(4) {
        height: 450px;
      }
    </style>
  </head>
  <body>
    <div class="body">
      <ul class="list">
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
        <li class="item"></li>
      </ul>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  </body>
</html>


方法一:getBoundingClientRect()

$(function () {
        var $parent = $('.body');
        var $list = $('.list');
        var $childern = $list.children();
        var parentPadding = parseInt($parent.css('padding'));
        var parentBottom = Math.abs($parent[0].getBoundingClientRect().bottom);
        var listBottom = Math.abs($list[0].getBoundingClientRect().bottom);
        
        for (let i = 0, len = $childern.length; i < len; i++) {
          if (Math.abs($childern[i].getBoundingClientRect().bottom) > parentBottom) {
            scrollTop($childern.eq(i));
            break;
          }
        }
        
        function scrollTop(el) {
          el.on('mouseover', function () {
            $parent.animate({ scrollTop: parentPadding + listBottom - parentBottom + 'px'}, 300);
          })
        }
      });


方法二:通过一系列计算

$(function () {
        var $parent = $('.body');
        var $list = $('.list');
        var $childern = $list.children();
        var parentPadding = parseInt($parent.css('padding'));
        var itemBottom = parseInt($childern.css('margin-bottom'));
       
        var parentHeight = $parent[0].offsetHeight;
        var listHeight = $list[0].offsetHeight;
        
        for (let i = 0, len = $childern.length, sum = 0; i < len; i++) {
          sum += $childern[i].offsetHeight + itemBottom;
          if (listHeight - sum < listHeight - parentHeight + parentPadding * 2) {
            scrollTop($childern.eq(i));
            break;
          }
        }
        
        function scrollTop(el) {
          el.on('mouseover', function () {
            $parent.animate({ scrollTop: (listHeight - parentHeight + parentPadding * 2) + 'px'}, 300);
          })
        }
      });





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值