最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable horizontal scroll; but allow vertical scroll - Stack Overflow

programmeradmin0浏览0评论

I'm trying to disable horizontal scroll on a website; but allowing vertical scrolling.

I have a script which works like a slider by sliding the 'body' of the page left and revealing more contents. However this creates extra empty space on the right.

I need to disable horizontal scrolling so users don't see this empty space. I tried the following script but it disables both horizontal and vertical scrolling:

window.onscroll = function () {
     window.scrollTo(0,0);
}

I have tried overflow-x: hidden but that doesn't work when the body's width is dynamic and not static.

Question:

Is there a way to modify the above script to disable the horizontal scrolling and keep vertical scrolling?

I'm trying to disable horizontal scroll on a website; but allowing vertical scrolling.

I have a script which works like a slider by sliding the 'body' of the page left and revealing more contents. However this creates extra empty space on the right.

I need to disable horizontal scrolling so users don't see this empty space. I tried the following script but it disables both horizontal and vertical scrolling:

window.onscroll = function () {
     window.scrollTo(0,0);
}

I have tried overflow-x: hidden but that doesn't work when the body's width is dynamic and not static.

Question:

Is there a way to modify the above script to disable the horizontal scrolling and keep vertical scrolling?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 27, 2014 at 4:43 user2028856user2028856 3,1839 gold badges50 silver badges77 bronze badges 0
Add a comment  | 

7 Answers 7

Reset to default 24

You were close to get it. You need to get the documentor window— and bind the scroll: then you can check if the scrollLeft is updated to more than 0 and set the scrollLeft to 0.

The next code works well:

$(function() {

    var $body = $(document);
    $body.bind('scroll', function() {
        // "Disable" the horizontal scroll.
        if ($body.scrollLeft() !== 0) {
            $body.scrollLeft(0);
        }
    });

}); 

If you want to disable horizontal scroll for an element (like div, for instance) you only need to replace $(document) by $("#yourElementID")


JSFiddle: https://jsfiddle.net/tomloprod/zx1bvdf5/


NOTE:

The above script will prevent you scroll horizontally and, in addition, you can use the next CSS style: overflow-x:hidden; to hide the horizontal scroll.

And will be like if there were no horizontal scrolling.

This should work (CSS):

html, body {
    max-width: 100% !important;
    overflow-x: hidden !important;
}

Without JQuery:

window.onscroll = function () {
  window.scrollLeft = 0;
}

use overflow-x:hidden on your wrapper element

overflow-x:hidden;

Here is what I did to solve my problem.

 window.onscroll = function () {
     window.scrollTo(0,window.scrollY);
 };

Hope this helps.

You could also jQuery's "on", and check for the deltaX.

$("body").on("wheel", function(e) {
  var deltaX = e.originalEvent.deltaX;

  if (deltaX !== 100 && deltaX !== -100) {
    // ...do something
  }

  return false;
});

CSS:

body {
    width: 100vw;
    height: 100vh;
    overflow: hidden auto;
}

The 100vw/vh means 100% of the view-port (=window inner) width/height.

The hidden auto means never show scroll on x and show scroll on y when necessary.

发布评论

评论列表(0)

  1. 暂无评论