【问题标题】:How to stick an image to the bottom of the visible part of a scrollable div如何将图像粘贴到可滚动 div 可见部分的底部
【发布时间】:2015-07-05 17:59:01
【问题描述】:

我有一个包含很多内容的下拉菜单。如果在移动设备上使用此下拉菜单,用户将需要在ul 内向下滚动(ul 具有overflow:scroll)以显示其余内容。这有点不寻常,所以我想在ul 可见部分的底部有一个向下的箭头。

我似乎无法正确放置它。

我正在使用 javascript 将ul 的高度设置为永远不会超过视口高度。我只是按预期在ul 内滚动,但在某些情况下可能很难有更多的菜单内容。

<ul>
  <li>
  <li>
  <li>
  <li>
  <li>
  ...
  <img class="moreContent-arrow"> //Should be at the bottom of the visible part of the ul
</ul>

ulposition:absolute。我试过给箭头position:absolute; bottom: 0;,但这只是把它放在正确的位置,直到你移动滚动条。

我一直在寻找答案,但我似乎找不到任何无法引导我将页脚定位到页面底部的搜索词。


编辑 1:

尝试在图像上使用 position:fixed,但我在菜单的打开和关闭上使用 jquery 的 .slideToggle(),这意味着固定的图像看起来不太好,因为它会弹出和弹出存在而不会与其余内容一起滑入视野。

【问题讨论】:

  • 你试过position: fixed; bottom: 0;吗?
  • @JoelAlmeida,我刚才试过了。虽然它让我更接近想要的位置,但菜单以 jquery 滚动打开,这使得 img 弹出和消失,而不会受到滚动动画的视觉影响。这对我来说不够干净
  • 你试过在 ul 之外放置一个 div 吗?而且,你应该尝试height: 100vh,而不是用javascript计算高度。 vh 代表视口高度。所以它将始终是视口高度的 100%。如果你想要它作为最大值只做max-height: 100vh then.
  • 抱歉,我的意思是 .slide(),而不是 .scroll(),视口 css 看起来与 IE 8 及以下版本不兼容,我需要它
  • 你能发布一个jsBin测试页面吗?还是小提琴?还是 CodePen?

标签: html css image positioning


【解决方案1】:

在尝试意识到自己想要什么之后。

试试这个:

ul {
    position: relative;
    height: 100px;
    overflow: auto;
}

.list {
    background-color:red;
    position: relative;
    height: 100px;
}

img {  
    background-color: green;
    height: 20px;
    width: 20px;
    position: absolute;
    display: block;
    bottom:0px;
 
}
<div class="list">

    <ul>
       <li>
           Stuff
      </li>
      <li>
           More Stuff
      </li>
      <li>
          Even more Stuff
      </li>
      <li>
          Stuff
      </li>
      <li>
          Last but not least, Stuff.
      </li>
      <li>
          Stuff
      </li>
      <li>
          More Stuff
      </li>
      <li>
          Even more Stuff
      </li>
      <li>
          Stuff
      </li>
      <li>
          Last but not least, Stuff.
      </li>
      <li>
          Stuff
      </li>
      <li>
          More Stuff
      </li>
      <li>
          Even more Stuff
      </li>
      <li>
          Stuff
      </li>
      <li>
      Last but not least, Stuff.
      </li>
    </ul>
    <img class="moreContent-arrow">
</div>

列表中有height,所以改变你的js中的计算来影响它。 给uloverflow: auto 赋予相同的高度。

所以,解释一下我在这里做了什么,你可以注意到滚动的是ul,它位于.list 内部。 img 相对于 .list 而不是 ul 具有 position: absolute。所以当ul滚动时,不会影响img

【讨论】:

  • 是的,您的代码确实给了我一个预期的菜单行为,但它并没有像我想的那样解决它是否可能。我想如果没有各种 .list “包装器”,它就无法解决。
【解决方案2】:

如果我猜对了,

...
position:fixed;
...

应该是你要找的。​​p>

你也可以用CSS代替JS来设置UL高度

【讨论】:

  • 我需要用 JS 设置它,因为我需要它取全高减去页眉的高度。 $(窗口).height() - 80;。固定位置不起作用,因为它在 jquery .slide() 中看起来很糟糕。我应该更清楚我的 js 使用抱歉
【解决方案3】:

我设置了一个 JSFiddle。我给图像一个position:absolutebottom:0,然后我创建了容器position:relative

编辑:我刚刚看到你说 ul 的位置是绝对的。它的位置也是绝对的。

ul {
    background-color:red;
    height: 200px;
    position: relative;
}

img {  
    background-color: green;
    height: 20px;
    width: 20px;
    position: absolute;
    bottom:0px;
 
}
<ul>
  <li>
      Stuff
      </li>
  <li>
      More Stuff
      </li>
  <li>
      Even more Stuff
      </li>
  <li>
      Stuff
      </li>
  <li>
      Last but not least, Stuff.
      </li>
  <img class="moreContent-arrow">
</ul>

希望这会有所帮助。

【讨论】:

  • 如果他有滚动条,滚动后箭头仍然会出现在div中间。
  • 是的,您的 css 可以工作,但它不能解决我的问题。我的ul是一个可滚动的容器,图片需要粘在ul可见部分的底部,而不是ul的底部。
猜你喜欢
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
相关资源
最近更新 更多