【问题标题】:Scroll only right section of page section on body scroll在正文滚动上仅滚动页面部分的右侧部分
【发布时间】:2021-07-30 20:59:24
【问题描述】:

我正在用 Vuejs 制作一个网页,我必须将该页面的一部分制作成非常相似的this site's landing page(查看下面的屏幕截图)。

我能够创建类似的布局,但是当用户在 body 上滚动滚动条时,我遇到了滚动此布局的右侧 <div class="col position-relative"> 的问题。

这是我在 Vuejs 中所做的

<!--Other sections of page-->
<template>
<section ref="rainbow" class="suggestion-container section-padding">
  <div class="container h-100">
    <div class="row h-100">
      <div class="col-6 d-flex align-items-center justify-content-end h-100 pull-left">
        <p class="suggestion-label">
          Create your own Stack of
        </p>
      </div>
      <div class="col position-relative">
        <div class="glass top"></div>
        <div v-view="'visible'" ref="stacks" style="transition: 0.3s all ease; transform: translateY(260px)">
          
          <p data-color="rgba(255, 116, 0, 0.7)" class="stack-name">Guitar</p>
          <p data-color="rgba(255, 170, 0, 0.7)" class="stack-name">Gaming Laptop</p>
          <p data-color="rgba(128, 42, 219, 0.7)" class="stack-name">Euro trip</p>
          <p data-color="rgba(255, 79, 123, 0.7)" class="stack-name">Phone for Dad</p>
         
        </div>
        <div class="glass bottom"></div>
      </div>
    </div>
  </div>
</section>
<!--Other sections of page-->
</template>

<script>
// Necessary imports
export default {
  mounted() {
    ...
    window.addEventListener('scroll', this.handleScroll);
  },
  destroy() {
    window.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll(e) {
      if (!this.$refs.rainbow) {
        return;
      }

      if (this.$refs.stacks && !this.$refs.stacks?.classList.contains('visible')) {
        return;
      }

      const stacks = this.$refs.stacks;
      const container = this.$refs.rainbow;
      console.log(container.getBoundingClientRect());
      stacks.style.transform = `translateY(-${(container.getBoundingClientRect().top) / 2}px)`;

      const y = stacks.style.transform;
      if (y.includes('260')) {
        // stacks.style.transform = 'translateY(170px)';
        container.style.backgroundColor = stacks.children[0].attributes[0].value;
      } else if (y.includes('170')) {
        // stacks.style.transform = 'translateY(90px)';
        container.style.backgroundColor = stacks.children[1].attributes[0].value;
      } else if (y.includes('90')) {
        // stacks.style.transform = 'translateY(10px)';
        container.style.backgroundColor = stacks.children[2].attributes[0].value;
      }
    },
  },
}
</script>

我试图在 Codepen 上制作一个普通的 CSS(没有 js)版本。如果有帮助,也请检查一下 - https://codepen.io/yashwp5/pen/rNmvLRO

过去 2 天我一直在努力完成这项工作:(。

【问题讨论】:

    标签: javascript css vue.js


    【解决方案1】:

    这里是如何在纯 CSS 中实现的基本思想。要为左侧文本添加视差滚动,您必须使用 JS。

    .row-filler {
      height: 25vh;
    }
    
    .row-main {
      position: relative;
      padding: 50vh 0;
      background-color: #f7d3d8;
      color: #e57d61;
    }
    .static {
      position: sticky;
      top: 50vh;
    }
    .list {
      list-style-type: none;
    }
    <div class="row-filler"></div>
    
    <div class="row-main">
        <div class="static">
          <div class="static-row-first">Create</div>
          <div class="static-row-last">yourDollar $</div>
        </div>
    
        <ul class="list">
            <li>ign</li>
            <li>NickelsBack</li>
            <li>ThereWillBeBud</li>
            <li>Rye&Reynolds</li>
            <li>My2Cents</li>
            <li>The6ixCents</li>
            <li>KillsBills</li>
            <li>Million$Baby</li>
            <li>ChequeMate</li>
            <li>SpendrickLamar</li>
            <li>hawnSpendez</li>
            <li>ign</li>
        </ul>
    </div>
    
    <div class="row-filler"></div>

    【讨论】:

    • 嘿@keymasterr 我试过这个解决方案但没有帮助。另外,我不想滚动该部分的左侧作为视差效果。如果您有其他想法,请告诉我。
    • 您能具体说明什么不适合您吗?
    • 粘性部分对我不起作用。我试图让左边的容器变粘。但它不起作用。
    • 你的粘滞块位置设置了吗? top 属性是这里的重要组成部分
    • 是的,我确实设置为position: sticky; top: 100px,因为导航栏大约需要 100 像素;但是块/部分仍在滚动
    猜你喜欢
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2022-01-04
    • 2023-03-26
    相关资源
    最近更新 更多