Foundation Sticky | 元素应在视口顶部触及时粘附,而不是立即粘附

huangapple go评论71阅读模式
英文:

Foundation Sticky | Element should stick when the top of viewport hits it, not immediately

问题

我不太确定Foundation中的"sticky"如何工作。部分内的元素在视口顶部触及元素时需要保持粘性,而不是立即生效。页面上有多个部分,需要保持粘性的元素可能在页面下方,位于另一个部分之下。在视口顶部触及元素之后,它应该一直保持粘性。

在点击滚动按钮后,元素会立即变为粘性,而不是在视口顶部触及它时。我尝试了许多"data-anchors"并尝试了"data-sticky-containers",但似乎无法使其工作。也许这更困难,因为需要保持粘性的元素位于一个部分内,而不是它自己的部分。但我无法更改HTML结构。

我创建了一个Codepen来显示和解决这个问题,链接在这里:https://codepen.io/vialito/pen/yLQMQZR

这是HTML,希望有人可以帮助我解决这个问题!

<main>
    <div class="nav">
        <div class="gridcontainer">
            <div class="grid-x">
                <div class="content"></div>
            </div>
        </div>
    </div>

    <section class="main">
        <div class="gridcontainer">
            <div class="grid-x">

                <div class="one cell small-12 large-8">
                    <div class="content"></div>
                </div>

                <div class="two cell small-12 large-4">
                    <div class="content"></div>
                </div>

                <div class="three cell small-12" data-sticky-container>
                    <div class="content sticky" data-sticky data-margin-top="0"></div>
                </div>

            </div>
        </div>
    </section>

    <section class="random">
        <div class="gridcontainer">
            <div class="grid-x">
                <div class="content"></div>
            </div>
        </div>
    </section>
</main>
英文:

I am not entirely sure how Foundation sticky works. An element within a section needs to be sticky when the top of the viewport hits the element, not immediately. There are multiple sections on the page and the element that needs to be sticky might be further down the page, below another section. After the top of the viewport hits the element, it should be sticky all the way down.

The element becomes sticky immediately after you hit the scroll button, not when the top of the viewport hits it. I tried many data-anchors and played around with data-sticky-containers, but I can't seem to make it work. Maybe it is more difficult because the element that needs to be sticky is inside of a section and it's not a section of its own. But I can't change the HTML structure.

I created a Codepen to display and play with the issue here: https://codepen.io/vialito/pen/yLQMQZR

This is the HTML, I hope someone can help me out!

&lt;main&gt;
    &lt;div class=&quot;nav&quot;&gt;
        &lt;div class=&quot;gridcontainer&quot;&gt;
            &lt;div class=&quot;grid-x&quot;&gt;
                &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;section class=&quot;main&quot;&gt;
        &lt;div class=&quot;gridcontainer&quot;&gt;
            &lt;div class=&quot;grid-x&quot;&gt;

                &lt;div class=&quot;one cell small-12 large-8&quot;&gt;
                    &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
                &lt;/div&gt;

                &lt;div class=&quot;two cell small-12 large-4&quot;&gt;
                    &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
                &lt;/div&gt;

                &lt;div class=&quot;three cell small-12&quot; data-sticky-container&gt;
                    &lt;div class=&quot;content sticky&quot; data-sticky data-margin-top=&quot;0&quot;&gt;&lt;/div&gt;
                &lt;/div&gt;

            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/section&gt;

    &lt;section class=&quot;random&quot;&gt;
        &lt;div class=&quot;gridcontainer&quot;&gt;
            &lt;div class=&quot;grid-x&quot;&gt;
                &lt;div class=&quot;content&quot;&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/section&gt;
&lt;/main&gt;

答案1

得分: 0

以下是您提供的内容的翻译:

首先,您需要为您的粘性元素之上的元素定义一个id(例如id=&quot;stickyElem&quot;),在您的HTML中,该元素具有class=&quot;two cell small-12 large-4&quot;

然后,在您的粘性元素中,设置以下参数:

<div class=&quot;content sticky&quot; data-sticky data-margin-top=&quot;0&quot; data-top-anchor=&quot;stickyElem:bottom&quot;></div>

下面是具有内联CSS和JavaScript的HTML结构,将其显示在一个index.html文件中:

<!DOCTYPE html>
<html lang=&quot;en&quot;>
<head>
    <meta charset=&quot;UTF-8&quot;>
    <meta name=&quot;viewport&quot; content=&quot;width=, initial-scale=1.0&quot;>
    <link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/foundation-sites@6.7.4/dist/css/foundation.min.css&quot; />
    <script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js&quot;></script>
    <script src=&quot;https://cdn.jsdelivr.net/npm/foundation-sites@6.7.4/dist/js/foundation.min.js&quot;></script>
    <title>Document</title>
    <style>
      html, body {
          height: 100%;
      }

      .nav {
        height: 100px;
        background: #cacaca;
      }

      .main .one {
        height: 500px;
        background: #1779ba;
      }

      .main .two {
        height: 500px;
        background: #0c659d;
      }

      .main .three {
        height: 200px;
      }

      .main .three .content { 
        height: 200px;
        background: #0b7a00;
      }

      .random {
        height: 2000px;
        background: red;
      }
    </style>
</head>
<body>
    <main>
        <div class=&quot;nav&quot;>
          <div class=&quot;gridcontainer&quot;>
            <div class=&quot;grid-x&quot;>
              <div class=&quot;content&quot;><h1>Header</h1></div>
            </div>
          </div>
        </div>
      
        <section class=&quot;main&quot;>
          <div class=&quot;gridcontainer&quot;>
            <div class=&quot;grid-x&quot;>
      
              <div class=&quot;one cell small-12 large-8&quot;>
                <div class=&quot;content&quot;><h1>Content1</h1></div>
              </div>
      
              <div class=&quot;two cell small-12 large-4&quot; id=&quot;abcd&quot;>
                <div class=&quot;content&quot;><h1>Content2</h1></div>
              </div>
      
              <div class=&quot;three cell small-12&quot; data-sticky-container>
                <div class=&quot;content sticky&quot; data-sticky data-margin-top=&quot;0&quot; data-top-anchor=&quot;abcd:bottom&quot;><h1>Sticky Div</h1></div>
              </div>
      
            </div>
          </div>
        </section>
      
        <section class=&quot;random&quot;>
          <div class=&quot;gridcontainer&quot;>
            <div class=&quot;grid-x&quot;>
              <div class=&quot;content&quot;>
                <h1>Some Text 1</h1><br><br><br>
                <h1>Some Text 2</h1><br><br><br>
                <h1>Some Text 3</h1><br><br><br>
                <h1>Some Text 4</h1><br><br><br>
                <h1>Some Text 5</h1><br><br><br>
                <h1>Some Text 6</h1><br><br><br>
                <h1>Some Text 7</h1><br><br><br>
              </div>
            </div>
          </div>
        </section>
      </main>


<script> $(document).foundation(); </script>

</body>
</html>

请注意,上述文本包括HTML和CSS代码以及相关的翻译。

英文:

You first need to define an id (like id=&quot;stickyElem&quot;) for the element above of your sticky element which in your html has class of class=&quot;two cell small-12 large-4&quot;.
Then in your sticky element, set this parameters:

&lt;div class=&quot;content sticky&quot; data-sticky data-margin-top=&quot;0&quot; data-top-anchor=&quot;stickyElem:bottom&quot;&gt;&lt;/div&gt;

Below html structure with inline css and javascript will show it in a one index.html file:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=, initial-scale=1.0&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/foundation-sites@6.7.4/dist/css/foundation.min.css&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/foundation-sites@6.7.4/dist/js/foundation.min.js&quot;&gt;&lt;/script&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;style&gt;
html, body {
height: 100%;
}
.nav {
height: 100px;
background: #cacaca;
}
.main .one {
height: 500px;
background: #1779ba;
}
.main .two {
height: 500px;
background: #0c659d;
}
.main .three {
height: 200px;
}
.main .three .content { 
height: 200px;
background: #0b7a00;
}
.random {
height: 2000px;
background: red;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;main&gt;
&lt;div class=&quot;nav&quot;&gt;
&lt;div class=&quot;gridcontainer&quot;&gt;
&lt;div class=&quot;grid-x&quot;&gt;
&lt;div class=&quot;content&quot;&gt;&lt;h1&gt;Header&lt;/h1&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;section class=&quot;main&quot;&gt;
&lt;div class=&quot;gridcontainer&quot;&gt;
&lt;div class=&quot;grid-x&quot;&gt;
&lt;div class=&quot;one cell small-12 large-8&quot;&gt;
&lt;div class=&quot;content&quot;&gt;&lt;h1&gt;Content1&lt;/h1&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;two cell small-12 large-4&quot; id=&quot;abcd&quot;&gt;
&lt;div class=&quot;content&quot;&gt;&lt;h1&gt;Content2&lt;/h1&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;three cell small-12&quot; data-sticky-container&gt;
&lt;div class=&quot;content sticky&quot; data-sticky data-margin-top=&quot;0&quot; data-top-anchor=&quot;abcd:bottom&quot;&gt;&lt;h1&gt;Sticky Div&lt;/h1&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;random&quot;&gt;
&lt;div class=&quot;gridcontainer&quot;&gt;
&lt;div class=&quot;grid-x&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;h1&gt;Some Text 1&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 2&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 3&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 4&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 5&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 6&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;h1&gt;Some Text 7&lt;/h1&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;/main&gt;
&lt;script&gt; $(document).foundation(); &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年6月29日 14:36:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定