【问题标题】:Scroll the content inside a scrollable div when clicking an element单击元素时滚动可滚动 div 内的内容
【发布时间】:2017-12-18 03:31:20
【问题描述】:

我有一个高度有限的 DIV,并且有一个可滚动的溢出。我想添加两个手动滚动链接,当它们被点击时,DIV 内的内容会向上或向下滚动。

CSS

#inner {
   max-height: 400px;
   overflow: scroll;
}

JS

function scrolldown() {
   document.getElementById('#inner').scrollTop -= 10;
}
function scrollup() {
   document.getElementById('#inner').scrollTop += 10;
}

HTML

<div id="inner">
    Looooong content here
</div>
<a href="#" onClick="return false" onmousedown="javascript:scrollup();">
    <img src="up.png"/>
</a>
<a href="#" onClick="return false" onmousedown="javascript:scrolldown();">
    <img src="down.png"/>
</a>

但是它并没有真正起作用......

【问题讨论】:

    标签: jquery css scroll overflow


    【解决方案1】:

    您不能在 getElementById 中使用 CSS 选择器。

    function scrolldown() {
        document.getElementById('inner').scrollTop -= 10;
    }
    function scrollup() {
        document.getElementById('inner').scrollTop += 10;
    }
    

    【讨论】:

      【解决方案2】:

      您使用了双选择器,getElementById 在其参数中不需要#。如果您将 javascript 放在 &lt;head&gt; 标记之间,它应该可以正常工作。

      function scrolldown() {
          document.getElementById('inner').scrollTop -= 10;
      }
      function scrollup() {
          document.getElementById('inner').scrollTop += 10;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-22
        • 2019-12-25
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多