实现页面横向滚动 && 页面中实现几个模块横向滚动

7 篇文章 0 订阅
3 篇文章 0 订阅

Screen-2024-02-28-120857

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      font-size: 40px;
    }

    .container-x {
      position: relative;
      height: 300vw;
    }

    .sticky-content {
      position: sticky;
      top: 0;
      left: 0;

    }

    .scroll-page {
      overflow-y: hidden;
      overflow-x: hidden;
    }

    .section {
      min-width: 100vw;
      height: 100vh;
      flex-shrink: 0;
      position: relative;
    }

    .section1 {
      background-color: red;
    }

    .section2 {
      background-color: blue;
    }

    .section3 {
      background-color: pink;
    }

    .section4 {
      background-color: gold;
    }

    .header {
      height: 200px;
      background-color: aqua;
    }

    .footer {
      height: 300px;
      background-color: aqua;
    }

    .d-flex {
      display: flex;
    }

    .h-full {
      height: 100%;
    }
  </style>
</head>

<body>
  <div class="as-page-container">
    <div class="header as-header">header</div>
    <horizontal-roll>
      <div class="container-x as-container-x">
        <div class="sticky-content">
          <div class="scroll-page d-flex as-scroll-page">
            <div class="section section1 as-section">section1</div>
            <div class="section d-flex section2 as-section">
              <div class="h-full" style="width:100vw;">section2</div>
              <div class="h-full" style="width:680px;">section2</div>
              <div class="h-full" style="width:280px;">section2</div>
            </div>
            <div class="section section3 as-section">section3</div>
            <div class="section section4 as-section">section4</div>
          </div>
        </div>
      </div>
    </horizontal-roll>
    <div class="footer">footer</div>
  </div>

</body>
<script>
  class HorizontalRoll extends HTMLElement {
    constructor() {
      super()
      this.$setions = this.querySelectorAll('.as-section')
      this.$containerX = this.querySelector('.as-container-x')
      this.$scrollPage = this.querySelector('.as-scroll-page')
      if (!this.$setions.length || !this.$containerX || !this.$scrollPage) return
      this.headerHeight = this.getHeaderHeight()
      this.init()
    }

    init() {
      this.setContainerXHeight()
      this.listenScrollPage()
      window.addEventListener('scroll', () => {
        this.listenScrollPage()
      })
    }

    listenScrollPage() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop,
        scrollLeft = 0
      if (scrollTop > this.headerHeight) scrollLeft = scrollTop - this.headerHeight
      this.$scrollPage.scrollLeft = scrollLeft
    }

    setContainerXHeight() {
      let totalHeight = 0;
      [...this.$setions].map(element => {
        totalHeight = totalHeight + element.offsetWidth
      })
      totalHeight = totalHeight - window.innerWidth + window.innerHeight
      this.$containerX.style.height = `${totalHeight}px`
    }

    getHeaderHeight() {
      const el = document.querySelector('.as-header')
      if (el) return el.clientHeight
      else return 0
    }
  }

  if (customElements.get('horizontal-roll') === undefined) {
    customElements.define('horizontal-roll', HorizontalRoll)
  }
</script>

</html>
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值