如何在HTML/CSS中创建两行图像,使其随滚动按钮滑动。

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

How do you create two lines of images in html/css that slide with the scrolling button

问题

像这样 -->>(https://i.stack.imgur.com/Z3QTV.png)

尝试过但没有成功,我试过

英文:

Like this -->>(https://i.stack.imgur.com/Z3QTV.png)

tried didnt work i tried

答案1

得分: 1

你需要的不仅仅是html和css,还需要一点点js。非常简单的滚动效果,你可以在2分钟内完成:

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

    const wrapper = document.querySelector(".wrapper");
    const left = document.querySelector(".left");
    const right = document.querySelector(".right");

    left.addEventListener("click", () => {
      wrapper.scrollBy({ left: -100, behavior: "smooth" });
    });

    right.addEventListener("click", () => {
      wrapper.scrollBy({ left: 100, behavior: "smooth" });
    });

<!-- 语言: lang-css -->

    .wrapper {
      margin: auto;
      overflow-x: scroll;
      max-width: 500px;
    }

    .image-group {
      display: flex;
      flex-wrap: wrap;
      width: 1500px;
      gap: 20px;
    }


<!-- 语言: lang-html -->

    <div id="app">
      <div class="wrapper">
        <div class="image-group">
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
          <img src="https://cataas.com/cat?width=300&height=300" />
      </div>
    </div>

    <button class="left">left</button>
    <button class="right">right</button>

<!-- 结束代码段 -->

或者你可以使用现成的解决方案以获得一些高级变体
https://freefrontend.com/javascript-carousels/
英文:

You need not only html and css but a little bit js. Very simple scroll you can make in 2 minutes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const wrapper = document.querySelector(&quot;.wrapper&quot;);
const left = document.querySelector(&quot;.left&quot;);
const right = document.querySelector(&quot;.right&quot;);

left.addEventListener(&quot;click&quot;, () =&gt; {
  wrapper.scrollBy({ left: -100, behavior: &quot;smooth&quot; });
});

right.addEventListener(&quot;click&quot;, () =&gt; {
  wrapper.scrollBy({ left: 100, behavior: &quot;smooth&quot; });
});

<!-- language: lang-css -->

.wrapper {
  margin: auto;
  overflow-x: scroll;
  max-width: 500px;
}

.image-group {
  display: flex;
  flex-wrap: wrap;
  width: 1500px;
  gap: 20px;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;div class=&quot;wrapper&quot;&gt;
    &lt;div class=&quot;image-group&quot;&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
      &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;button class=&quot;left&quot;&gt;left&lt;/button&gt;
&lt;button class=&quot;right&quot;&gt;right&lt;/button&gt;

<!-- end snippet -->

Or for some advanced variants you can use ready-made solutions
https://freefrontend.com/javascript-carousels/

huangapple
  • 本文由 发表于 2023年2月19日 18:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499498.html
匿名

发表评论

匿名网友

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

确定