Disable horizontal scroll with JavaScript

前端 未结 3 562
囚心锁ツ
囚心锁ツ 2020-12-11 17:11

Does anyone know if there is a way to disable the horizontal scrollbar using JavaScript?

I don\'t want to use overflow-x: hidden;.

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 17:35

    A way to prevent elements from scrolling down in jQuery:

    $(element).scroll(function () {
        this.scrollTop = 0;
        this.scrollLeft = 0;
    });
    

    Well, this does not actually prevent the scrolling, but it "scrolls back" to the top-left corner of an element, similar to Chris' solution which was created for the window instead of single elements. Remove the scrollTop or scrollLeft lines to suit your needs.

提交回复
热议问题