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

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

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

  1. 你需要的不仅仅是htmlcss,还需要一点点js。非常简单的滚动效果,你可以在2分钟内完成:
  2. <!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->
  3. <!-- 语言: lang-js -->
  4. const wrapper = document.querySelector(".wrapper");
  5. const left = document.querySelector(".left");
  6. const right = document.querySelector(".right");
  7. left.addEventListener("click", () => {
  8. wrapper.scrollBy({ left: -100, behavior: "smooth" });
  9. });
  10. right.addEventListener("click", () => {
  11. wrapper.scrollBy({ left: 100, behavior: "smooth" });
  12. });
  13. <!-- 语言: lang-css -->
  14. .wrapper {
  15. margin: auto;
  16. overflow-x: scroll;
  17. max-width: 500px;
  18. }
  19. .image-group {
  20. display: flex;
  21. flex-wrap: wrap;
  22. width: 1500px;
  23. gap: 20px;
  24. }
  25. <!-- 语言: lang-html -->
  26. <div id="app">
  27. <div class="wrapper">
  28. <div class="image-group">
  29. <img src="https://cataas.com/cat?width=300&height=300" />
  30. <img src="https://cataas.com/cat?width=300&height=300" />
  31. <img src="https://cataas.com/cat?width=300&height=300" />
  32. <img src="https://cataas.com/cat?width=300&height=300" />
  33. <img src="https://cataas.com/cat?width=300&height=300" />
  34. <img src="https://cataas.com/cat?width=300&height=300" />
  35. <img src="https://cataas.com/cat?width=300&height=300" />
  36. <img src="https://cataas.com/cat?width=300&height=300" />
  37. </div>
  38. </div>
  39. <button class="left">left</button>
  40. <button class="right">right</button>
  41. <!-- 结束代码段 -->
  42. 或者你可以使用现成的解决方案以获得一些高级变体
  43. 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 -->

  1. const wrapper = document.querySelector(&quot;.wrapper&quot;);
  2. const left = document.querySelector(&quot;.left&quot;);
  3. const right = document.querySelector(&quot;.right&quot;);
  4. left.addEventListener(&quot;click&quot;, () =&gt; {
  5. wrapper.scrollBy({ left: -100, behavior: &quot;smooth&quot; });
  6. });
  7. right.addEventListener(&quot;click&quot;, () =&gt; {
  8. wrapper.scrollBy({ left: 100, behavior: &quot;smooth&quot; });
  9. });

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

  1. .wrapper {
  2. margin: auto;
  3. overflow-x: scroll;
  4. max-width: 500px;
  5. }
  6. .image-group {
  7. display: flex;
  8. flex-wrap: wrap;
  9. width: 1500px;
  10. gap: 20px;
  11. }

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

  1. &lt;div id=&quot;app&quot;&gt;
  2. &lt;div class=&quot;wrapper&quot;&gt;
  3. &lt;div class=&quot;image-group&quot;&gt;
  4. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  5. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  6. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  7. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  8. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  9. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  10. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  11. &lt;img src=&quot;https://cataas.com/cat?width=300&amp;height=300&quot; /&gt;
  12. &lt;/div&gt;
  13. &lt;/div&gt;
  14. &lt;button class=&quot;left&quot;&gt;left&lt;/button&gt;
  15. &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:

确定