Javascript 创建平滑的滚动效果

Javascript 创建平滑的滚动效果,javascript,smooth-scrolling,Javascript,Smooth Scrolling,我有一个代码水平滚动的内容,它的作品,但每当它滚动,它滚动项目一个一个像打破,所以我希望它滚动顺利,这是下面的示例代码,请我需要帮助 <style> .child { width: 100px; white-space: nowrap; overflow-x: scroll; } </style> <script> (function(w){ w.addEventListener('load', function(){

我有一个代码水平滚动的内容,它的作品,但每当它滚动,它滚动项目一个一个像打破,所以我希望它滚动顺利,这是下面的示例代码,请我需要帮助

 <style>
.child {
  width: 100px;
  white-space: nowrap;
  overflow-x: scroll;
}
</style>

<script>
(function(w){
    w.addEventListener('load', function(){
        const   btn_left = document.getElementById('btn-left'),
                btn_right = document.getElementById('btn-right'),
                content = document.getElementById('con');
        const content_scroll_width = content.scrollWidth;
        let content_scoll_left = content.scrollLeft;
        btn_right.addEventListener('click', () => {
            content_scoll_left += 100;
            if (content_scoll_left >= content_scroll_width) { content_scoll_left = content_scroll_width; }
            content.scrollLeft = content_scoll_left;
        });
        btn_left.addEventListener('click', () => {
            content_scoll_left -= 100;
            if (content_scoll_left <= 0) {
                content_scoll_left = 0;
            }
            content.scrollLeft = content_scoll_left;
        });
    });
})(window);
</script>

<div class="parent">
     <div class="child" id="con">
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
                Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum. Lorem Ipsum.
    </div>
</div>
<button id="btn-left">Left scroll</button>
<button id="btn-right">Right scroll</button>

.孩子{
宽度:100px;
空白:nowrap;
溢出-x:滚动;
}
(功能(w){
w、 addEventListener('load',function(){
const btn_left=document.getElementById('btn-left'),
btn_right=document.getElementById('btn-right'),
content=document.getElementById('con');
const content\u scroll\u width=content.scrollWidth;
让content\u scoll\u left=content.scrollLeft;
btn_right.addEventListener('click',()=>{
content_scoll_left+=100;
如果(content\u scoll\u left>=content\u scroll\u width){content\u scoll\u left=content\u scroll\u width;}
content.scrollLeft=content\u scoll\u left;
});
btn_left.addEventListener('click',()=>{
content_scoll_left-=100;

如果(content\u scoll\u left可能您可以尝试在css文件中使用
滚动行为:smooth
。就是一个例子

或者,如果您想使用JavaScript,请尝试

window.scroll({
  top: 2500, 
  left: 0, 
  behavior: 'smooth'
});

也许你可以在css文件中尝试
滚动行为:smooth
。就是一个例子

或者,如果您想使用JavaScript,请尝试

window.scroll({
  top: 2500, 
  left: 0, 
  behavior: 'smooth'
});

使用
scroll behavior:smooth
css规则。它可能不会影响较旧的浏览器,但谁在乎它不是一个功能上重要的功能?我做到了,它成功了使用
scroll behavior:smooth
css规则。它可能不会影响较旧的浏览器,但谁在乎它不是一个功能上重要的功能?我做到了,它成功了