javascript - ul 溢出-x : visible and overflow-y:scroll doesn't work at same time

标签 javascript html css

长话短说 参见链接 http://codepen.io/anon/pen/MKqKZe对于也解释了我的问题的代码。

我想达到图片中的效果。 Most complex CSS-HTML for me

我的代码:

HTML

<div class="some-div-for-other-purpose">
  <ul class="dynamic-height-list">
    <li>
      <label>Name1</label>
      <span class='glyphicon glyphicon-option-vertical'
            onclick="toggleGreenDiv()">
      </span>
      <div class="toggle-visibility-green-div">ANYTHING INSIDE IT</div>
    </li>
    <li>
      <label>Name2</label>
      <span class='glyphicon glyphicon-option-vertical'></span>
      <div class="toggle-visibility-green-div">ANYTHING INSIDE IT</div>
    </li>
    <li>
      <label>Name3</label>
      <span class='glyphicon glyphicon-option-vertical'></span>
      <div class="toggle-visibility-green-div">ANYTHING INSIDE IT</div>
    </li>
    <li>
      <label>Name4</label>
      <span class='glyphicon glyphicon-option-vertical'></span>
      <div class="toggle-visibility-green-div">ANYTHING INSIDE IT</div>
    </li>
  </ul>
</div>

CSS

body {
  margin-top: 20px;
  margin-left: 20px;
  background: lightblue;
}

.glyphicon {
  float: right;
  padding: 0 5px;
  cursor: pointer;
  }

.dynamic-height-list {
  max-height: 120px;
  overflow-y: scroll;
  width: 200px;

  li {
  width: 100%;
  padding: 5px;
  background-color: lightgray;
  list-style-type: none;
  position: relative;    

    &:hover {
      background-color: #BBB;
    }
  }
}

.toggle-visibility-green-div {
  display: none;
  position: absolute;
  background-color: lightgreen;
  left: 155px;
  top: 0;
}

.showGreenDiv + .toggle-visibility-green-div {
  display: block;
}

JS

$('.glyphicon').click(function() {
    $(this).toggleClass('showGreenDiv');
});

问题: 这个绿色的 div 进入主 ul 列表并使其水平滚动而不是像图像一样。

Remove overflow property from ul, then it works fine but I can't do that since the list is dynamic and it has to be made scrollable for after max-height

IMPORTANT: Also, adding overflow:visible for ul will make it work but at the same time overflow-y should also scroll since list is vertically scrollable. And overflow-x: visible and overflow-y:scroll doesn't work at same time.

NOTE: Since, I want green div to appear adjacent to whatever <'li>'s button is clicked, so it has to be a child of <'li'>. And I don't want to find the position of my <'li'> using javascript. So, please tell if solution exist by changing structure of HTML or modifying css

编辑: 此外,为什么 overflow-x:visible 与 overflow-y:visible 的工作方式不同。我的意思是,为什么不能放置 overflow-x:visible 不能让它工作。

最佳答案

overflow-y: scrollmax-height 移动到您的父级 div,以便在达到您的 width 后它不会被切断.

.some-div-for-other-purpose {
  max-height: 130px;
  overflow-y: scroll;
}

Like this

关于javascript - ul 溢出-x : visible and overflow-y:scroll doesn't work at same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180936/

相关文章:

javascript - 使用 create react app,并具有 ES6 依赖项

html - "cursor: pointer"根本不工作

javascript - jquery动态内容生成的动态内容

javascript - 这种固定的图片滚动怎么做?

jquery - 使用 Jquery 通过 SELECT 字段进行表单验证

html - 使用位置 :fixed for a particular direction 修复一个 div

javascript - 序列化/镜像 JavaScript 鼠标/触摸事件

javascript - 单击其中之一后如何交换 href 属性的位置?

javascript - 处理 HTML Fieldset 的大小

html - 如何在 Qt 中用单个表格填充 QTextBrowser?