如何通过CSS或JavaScript为div添加平滑滚动效果?

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

How to add smooth scroll effect to div by css or javascript?

问题

使用点击事件创建一个具有左右滚动的可滚动div。我想通过CSS过渡或其他方式使滚动效果平滑化,请帮助

<div class="cardsliderblock">
  <div class="sliderBlock" id="Slidercontent">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
  </div>
  <span class="btns horizon-prev" id="left-button">&lt;</span>
  <span class="btns horizon-next" id="right-button">&gt;</span>
</div>
// JS
const rightBtn = document.querySelector('#right-button');
const leftBtn = document.querySelector('#left-button');

rightBtn.addEventListener("click", function(event) {
  const content = document.querySelector('#Slidercontent');
  content.scrollLeft += 150;
  event.preventDefault();
});

leftBtn.addEventListener("click", function(event) {
  const content = document.querySelector('#Slidercontent');
  content.scrollLeft -= 150;
  event.preventDefault();
});
// CSS
.sliderBlock {
  display: flex;
  height: 200px;
  border: solid 1px red;
  width: 800px;
  overflow: hidden;
  overflow-x: auto;
  transition: 2s all ease;
}

.sliderBlock > div {
  width: calc(100% / 5.5);
  margin-right: 2.2%;
  border: solid 1px black;
  min-width: 143px;
}

.btns {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: red;
}

查看示例

英文:

Creating a scrollable div with left and right scroll on click event. I wanted to make the scroll effect smooth with CSS transition or any other way, please help

&lt;div class=&quot;cardsliderblock&quot;&gt;
&lt;div class=&quot;sliderBlock&quot; id=&quot;Slidercontent&quot;&gt;
&lt;div&gt;1&lt;/div&gt;
&lt;div&gt;2&lt;/div&gt;
&lt;div&gt;3&lt;/div&gt;
&lt;div&gt;4&lt;/div&gt;
&lt;div&gt;5&lt;/div&gt;
&lt;div&gt;6&lt;/div&gt;
&lt;div&gt;7&lt;/div&gt;
&lt;/div&gt;
&lt;span class=&quot;btns horizon-prev&quot; id=&quot;left-button&quot;&gt;&lt;&lt;/span&gt;
&lt;span class=&quot;btns horizon-next&quot; id=&quot;right-button&quot; &gt;&gt;&lt;/span&gt;
&lt;/div&gt;

//JS
const rightBtn = document.querySelector(&#39;#right-button&#39;);
const leftBtn = document.querySelector(&#39;#left-button&#39;);

rightBtn.addEventListener(&quot;click&quot;, function(event) {
  const conent = document.querySelector(&#39;#Slidercontent&#39;);
  conent.scrollLeft += 150;
  event.preventDefault();
});

leftBtn.addEventListener(&quot;click&quot;, function(event) {
  const conent = document.querySelector(&#39;#Slidercontent&#39;);
  conent.scrollLeft -= 150;
  event.preventDefault();
});


// CSS
.sliderBlock {
  display: flex;
  height: 200px;
  border: solid 1px red;
  width: 800px;
  overflow: hidden;
  overflow-x: auto;

  transition: 2s all ease;
}

.sliderBlock&gt;div {
  width: calc(100% / 5.5);
  margin-right: 2.2%;
  border: solid 1px black;
  min-width: 143px;
}

.sliderBlock&gt;div:nth-child(5n) {}

.btns {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: red;
}

https://jsfiddle.net/anoopsuda/g0fk52ry/31/

答案1

得分: 1

你可以使用以下代码来实现平滑滚动效果:

*{
    scroll-behavior: smooth;
}
英文:

You can use

*{
    scroll-behavior: smooth;
}

to make this smooth

答案2

得分: 0

要实现平滑滚动,您需要使用 Element.scrollTo() 方法,而不是 scrollLeft 属性。使用 scrollTo 方法时,您可以传递 behavior 选项,并将其设置为 smooth

了解更多关于 Element.scrollTo()

演示:

const rightBtn = document.querySelector('#right-button');
const leftBtn = document.querySelector('#left-button');

rightBtn.addEventListener("click", function(event) {
  const content = document.querySelector('#Slidercontent');
  // 更改 1
  content.scrollTo({
    top: 0,
    left: content.scrollLeft + 150,
    behavior: "smooth",
  });

  event.preventDefault();
});

leftBtn.addEventListener("click", function(event) {
  const content = document.querySelector('#Slidercontent');
  // 更改 2
  content.scrollTo({
    top: 0,
    left: content.scrollLeft - 150,
    behavior: "smooth",
  });

  event.preventDefault();
});
.sliderBlock {
  display: flex;
  height: 200px;
  border: solid 1px red;
  width: 800px;
  overflow: hidden;
  overflow-x: auto;
  transition: 2s all ease;
}

.sliderBlock>div {
  width: calc(100% / 5.5);
  margin-right: 2.2%;
  border: solid 1px black;
  min-width: 143px;
}

.btns {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: red;
}
<div class="cardsliderblock">
  <div class="sliderBlock" id="Slidercontent">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
  </div>
  <span class="btns horizon-prev" id="left-button">&lt;&lt;</span>
  <span class="btns horizon-next" id="right-button">&gt;&gt;</span>
</div>
英文:

For Smooth Scroll you need to use Element.scrollTo() method instead of scrollLeft property. with scrollTo you can pass the behavior option, setting it to smooth.

Learn more about Element.scrollTo()

Demo:

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

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

const rightBtn = document.querySelector(&#39;#right-button&#39;);
const leftBtn = document.querySelector(&#39;#left-button&#39;);

rightBtn.addEventListener(&quot;click&quot;, function(event) {
  const conent = document.querySelector(&#39;#Slidercontent&#39;);
  // Change 1
  conent.scrollTo({
    top: 0,
    left: conent.scrollLeft + 150,
    behavior: &quot;smooth&quot;,
  });

  event.preventDefault();
});

leftBtn.addEventListener(&quot;click&quot;, function(event) {
  const conent = document.querySelector(&#39;#Slidercontent&#39;);
  // Change 2
  conent.scrollTo({
    top: 0,
    left: conent.scrollLeft - 150,
    behavior: &quot;smooth&quot;,
  });

  event.preventDefault();
});

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

.sliderBlock {
  display: flex;
  height: 200px;
  border: solid 1px red;
  width: 800px;
  overflow: hidden;
  overflow-x: auto;
  transition: 2s all ease;
}

.sliderBlock&gt;div {
  width: calc(100% / 5.5);
  margin-right: 2.2%;
  border: solid 1px black;
  min-width: 143px;
}

.sliderBlock&gt;div:nth-child(5n) {}

.btns {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: red;
}

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

&lt;div class=&quot;cardsliderblock&quot;&gt;
  &lt;div class=&quot;sliderBlock&quot; id=&quot;Slidercontent&quot;&gt;
    &lt;div&gt;1&lt;/div&gt;
    &lt;div&gt;2&lt;/div&gt;
    &lt;div&gt;3&lt;/div&gt;
    &lt;div&gt;4&lt;/div&gt;
    &lt;div&gt;5&lt;/div&gt;
    &lt;div&gt;6&lt;/div&gt;
    &lt;div&gt;7&lt;/div&gt;
  &lt;/div&gt;
  &lt;span class=&quot;btns horizon-prev&quot; id=&quot;left-button&quot;&gt;&lt;&lt;/span&gt;
  &lt;span class=&quot;btns horizon-next&quot; id=&quot;right-button&quot;&gt;&gt;&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月24日 17:55:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753313.html
匿名

发表评论

匿名网友

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

确定