Скрыть HTML горизонтальную, но не вертикальную полосу прокрутки


279

У меня есть текстовая область HTML, которая имеет фиксированную ширину, но переменную высоту. Я хотел бы установить overflow:scrollи иметь возможность показывать вертикальную полосу прокрутки, но не горизонтальную. Я не могу использовать overflow:autoиз-за других вещей, специфичных для моей ситуации.

Я знаю, что нет способа использовать CSS2 для отображения только вертикальных, но не горизонтальных полос прокрутки. Могу ли я что-нибудь сделать с JavaScript, чтобы скрыть горизонтальную полосу прокрутки?


3
Нет, используя CSS2, и overflow:scroll;нет способа показать одну полосу, а другую нет. Это присуще overflow:scroll; JavaScript может делать только то, что позволяет CSS. Тем не менее, я думаю, что вы можете использовать переполнение: авто, вы просто не знаете, как. Не могли бы вы рассказать о «других вещах, характерных для вашей ситуации»?
Тлофлин

Ответы:


575

Вы можете использовать CSS так:

overflow-y: scroll;
overflow-x: hidden;

2
Я вижу это как CSS3, и он не работает в Firefox, когда я тестирую его. Я также вижу, что это доступно как свойство только для IE с давних времен.
Уильям Джонс

@wiliamjones - Это работает в Firefox ... У вас есть пример страницы? Возможно, это какая-то другая причина, по которой он не работает.
Ник Крейвер

@william - Вот полный пример того, как это работает, тест в Firefox :) jsfiddle.net/qpZ8k
Ник

Вы правы, это работает с Firefox, это библиотека javascript Prototype, которая не совместима с этим атрибутом, что было достаточно легко обойти. Является ли это свойство в целом надежным во всех браузерах?
Уильям Джонс

@williamjones - Да, обычно это так, браузеры реализуют это, тогда это стандарт позже, как запоздалая мысль.
Ник Крейвер

30

Используйте CSS. Это проще и быстрее, чем JavaScript.

overflow-x: hidden;
overflow-y: scroll;

22

Полностью отключите горизонтальную полосу прокрутки, добавив этот код.

body{
  overflow-x: hidden;
  overflow-y: scroll;
}

9

Использование wrap=virtualв вашей форме HTML-блоков избавляет от горизонтальной полосы прокрутки в нижней части окна:

  <textarea name= "enquiry" rows="4" cols="30" wrap="virtual"></textarea>

Смотрите пример здесь: http://jsbin.com/opube3/2 (протестировано на FF и IE)



2
selector{
 overflow-y: scroll;
 overflow-x: hidden;
}

Рабочий пример с фрагментом и ссылкой jsfiddle https://jsfiddle.net/sx8u82xp/3/

введите описание изображения здесь

.container{
  height:100vh;
  overflow-y:scroll;
  overflow-x: hidden;
  background:yellow;
}
<div class="container">

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>

</div>


1
.combobox_selector ul {
    padding: 0;
    margin: 0;
    list-style: none;
    border:1px solid #CCC;
    height: 200px;
    overflow: auto;
    overflow-x: hidden;
}

устанавливает размер overflow-xпрокрутки 200px, скрывает любую горизонтальную полосу прокрутки.


0

Для меня:

.ui-jqgrid .ui-jqgrid-bdiv {
   position: relative;
   margin: 0;
   padding: 0;
   overflow-y: auto;  <------
   overflow-x: hidden; <-----
   text-align: left;
}

Конечно убери стрелки

Используя наш сайт, вы подтверждаете, что прочитали и поняли нашу Политику в отношении файлов cookie и Политику конфиденциальности.
Licensed under cc by-sa 3.0 with attribution required.